0%

使用Docker配置Redis哨兵

接上文使用Docker配置Redis主从复制完成之后,这篇文章主要介绍如何使用Docker在本机搭建Redis的哨兵,内容包括涉及的目录结构、docker-compose.yml的编写。

目录结构

本文将采用如下的目录结构,其中data目录将用于存放各个容器的数据,server目录存放docker-compose.yml以及针对masterslave节点的配置文件,sentinel目录存放哨兵的配置文件和docker-compose.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.
├── data
│   ├── redis-master
│   │   └── dump.rdb
│   ├── redis-slave-1
│   │   └── dump.rdb
│   └── redis-slave-2
│   └── dump.rdb
├── sentinel
│   ├── docker-compose.yml
│   └── redis-sentinel.conf
└── server
├── docker-compose.yml
├── redis-master.conf
└── redis-slave.conf

配置哨兵

编辑redis-sentinel.conf,修改下列配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 接受来自外部的连接
bind 0.0.0.0

# 哨兵的端口号
# 因为各个哨兵节点会运行在单独的Docker容器中
# 所以无需担心端口重复使用
port 26379

# 配置哨兵的监控参数
# 格式:sentinel monitor <master-name> <ip> <redis-port> <quorum>
# master-name是为这个被监控的master起的名字
# ip是被监控的master的IP或主机名。因为Docker容器之间可以使用容器名访问,所以这里写master节点的容器名
# redis-port是被监控节点所监听的端口号
# quorom设定了当几个哨兵判定这个节点失效后,才认为这个节点真的失效了
sentinel monitor local-master redis-server-master 6379 2

# 连接主节点的密码
# 格式:sentinel auth-pass <master-name> <password>
sentinel auth-pass local-master redis

配置及启动容器

编写docker-compose.yml

这里继续使用docker-compose管理容器。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
---

version: '3'

services:
# 哨兵的数量应是奇数,以便于哨兵通过投票来作出决策
redis-sentinel-1:
image: redis
container_name: redis-sentinel-1
restart: always
ports:
# 向外暴露26379端口
- 26379:26379
networks:
default:
ipv4_address: 10.1.0.5
volumes:
# 将哨兵配置文件和存放数据的文件夹挂载到容器内
- ./redis-sentinel.conf:/usr/local/etc/redis/redis-sentinel.conf
- ../data/redis-sentinel-1:/data
sysctls:
# 设定容器的内核参数,以消除Redis启动过程中的一个warning
net.core.somaxconn: '511'
# 根据指定的配置文件来启动Redis哨兵
command: ["redis-sentinel", "/usr/local/etc/redis/redis-sentinel.conf"]
redis-sentinel-2:
image: redis
container_name: redis-sentinel-2
restart: always
ports:
- 26380:26379
networks:
default:
ipv4_address: 10.1.0.6
volumes:
- ./redis-sentinel.conf:/usr/local/etc/redis/redis-sentinel.conf
- ../data/redis-sentinel-2:/data
sysctls:
net.core.somaxconn: '511'
command: ["redis-sentinel", "/usr/local/etc/redis/redis-sentinel.conf"]
redis-sentinel-3:
image: redis
container_name: redis-sentinel-3
restart: always
ports:
- 26381:26379
networks:
default:
ipv4_address: 10.1.0.7
volumes:
- ./redis-sentinel.conf:/usr/local/etc/redis/redis-sentinel.conf
- ../data/redis-sentinel-3:/data
sysctls:
net.core.somaxconn: '511'
command: ["redis-sentinel", "/usr/local/etc/redis/redis-sentinel.conf"]

networks:
default:
external:
# 因为哨兵容器需要与服务端容器运行在同一网络内
# 所以这里指定了一个外部的网络
# 网络名参考《使用Docker配置Redis主从复制》中的注释
name: server_redis-cluster

启动容器

这里同样使用docker-compose up -d启动容器,然后使用redis-cli连接到哨兵节点,连接成功后,可以使用info sentinel检查哨兵的信息。

1
2
3
4
5
6
7
8
127.0.0.1:26379> info sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=local-master,status=ok,address=10.1.0.2:6379,slaves=2,sentinels=1

其中,sentinel_masters:1说明这个哨兵在监控一个master,最后一行中写明了master0这个节点别名为local-master,状态为OK,地址是10.1.0.2:6379,有2个从节点。

测试一下

哨兵光是启动了还是不够的,还需要测试一下当被监控节点下线之后,哨兵是否能作出反应。

首先,停掉masterslave-1slave-2,然后观察哨兵的日志,在经过一段时间之后,哨兵宣布有节点下线:

1
2
3
4
5
6
7
8
9
redis-sentinel-3    | 1:X 19 Aug 2019 11:43:13.487 # +sdown slave 10.1.0.4:6381 10.1.0.4 6381 @ local-master 10.1.0.2 6379
redis-sentinel-3 | 1:X 19 Aug 2019 11:43:13.487 # +sdown slave 10.1.0.3:6380 10.1.0.3 6380 @ local-master 10.1.0.2 6379
redis-sentinel-1 | 1:X 19 Aug 2019 11:43:13.502 # +sdown slave 10.1.0.3:6380 10.1.0.3 6380 @ local-master 10.1.0.2 6379
redis-sentinel-1 | 1:X 19 Aug 2019 11:43:13.502 # +sdown slave 10.1.0.4:6381 10.1.0.4 6381 @ local-master 10.1.0.2 6379
redis-sentinel-2 | 1:X 19 Aug 2019 11:43:13.581 # +sdown slave 10.1.0.4:6381 10.1.0.4 6381 @ local-master 10.1.0.2 6379
redis-sentinel-2 | 1:X 19 Aug 2019 11:43:13.582 # +sdown slave 10.1.0.3:6380 10.1.0.3 6380 @ local-master 10.1.0.2 6379
redis-sentinel-3 | 1:X 19 Aug 2019 11:43:14.429 # +sdown master local-master 10.1.0.2 6379
redis-sentinel-2 | 1:X 19 Aug 2019 11:43:14.439 # +sdown master local-master 10.1.0.2 6379
redis-sentinel-1 | 1:X 19 Aug 2019 11:43:14.470 # +sdown master local-master 10.1.0.2 6379

然后再启动masterslave-1slave-2,并观察哨兵的日志,在节点上线之后,哨兵宣布了节点重启,并解除了节点的下线状态:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
redis-sentinel-2    | 1:X 19 Aug 2019 11:49:45.040 * +reboot slave 10.1.0.3:6380 10.1.0.3 6380 @ local-master 10.1.0.2 6379
redis-sentinel-1 | 1:X 19 Aug 2019 11:49:45.081 * +reboot slave 10.1.0.4:6381 10.1.0.4 6381 @ local-master 10.1.0.2 6379
redis-sentinel-3 | 1:X 19 Aug 2019 11:49:45.081 * +reboot slave 10.1.0.3:6380 10.1.0.3 6380 @ local-master 10.1.0.2 6379
redis-sentinel-3 | 1:X 19 Aug 2019 11:49:45.081 * +reboot slave 10.1.0.4:6381 10.1.0.4 6381 @ local-master 10.1.0.2 6379
redis-sentinel-1 | 1:X 19 Aug 2019 11:49:45.082 * +reboot slave 10.1.0.3:6380 10.1.0.3 6380 @ local-master 10.1.0.2 6379
redis-sentinel-1 | 1:X 19 Aug 2019 11:49:45.132 # -sdown slave 10.1.0.3:6380 10.1.0.3 6380 @ local-master 10.1.0.2 6379
redis-sentinel-3 | 1:X 19 Aug 2019 11:49:45.132 # -sdown slave 10.1.0.4:6381 10.1.0.4 6381 @ local-master 10.1.0.2 6379
redis-sentinel-3 | 1:X 19 Aug 2019 11:49:45.133 # -sdown slave 10.1.0.3:6380 10.1.0.3 6380 @ local-master 10.1.0.2 6379
redis-sentinel-1 | 1:X 19 Aug 2019 11:49:45.133 # -sdown slave 10.1.0.4:6381 10.1.0.4 6381 @ local-master 10.1.0.2 6379
redis-sentinel-2 | 1:X 19 Aug 2019 11:49:45.140 # -sdown slave 10.1.0.3:6380 10.1.0.3 6380 @ local-master 10.1.0.2 6379
redis-sentinel-2 | 1:X 19 Aug 2019 11:49:45.141 * +reboot slave 10.1.0.4:6381 10.1.0.4 6381 @ local-master 10.1.0.2 6379
redis-sentinel-2 | 1:X 19 Aug 2019 11:49:45.231 # -sdown slave 10.1.0.4:6381 10.1.0.4 6381 @ local-master 10.1.0.2 6379
redis-sentinel-1 | 1:X 19 Aug 2019 11:49:45.960 * +reboot master local-master 10.1.0.2 6379
redis-sentinel-3 | 1:X 19 Aug 2019 11:49:45.963 * +reboot master local-master 10.1.0.2 6379
redis-sentinel-1 | 1:X 19 Aug 2019 11:49:46.043 # -sdown master local-master 10.1.0.2 6379
redis-sentinel-3 | 1:X 19 Aug 2019 11:49:46.046 # -sdown master local-master 10.1.0.2 6379
redis-sentinel-2 | 1:X 19 Aug 2019 11:49:46.054 * +reboot master local-master 10.1.0.2 6379
redis-sentinel-2 | 1:X 19 Aug 2019 11:49:46.144 # -sdown master local-master 10.1.0.2 6379

系列博文