0%

Docker-Compose-Redis-Sentinel

上次编写了Docker-Compose-Redis-Master-Slave的文档,完成了简单的主从配置。这次在上次的基础上增加哨兵监控。

源码地址

编写docker-compose配置文件

  • 这里和上次的使用同一个网桥
  • Docker-compose 文件和 sentinel配置文件放在同一个目录下。
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
version : '3'

networks:
network-redis:
driver: bridge

services:
sentinel1:
image: redis:5.0.5
container_name: redis5.0.6-sentinel-1
networks:
- network-redis
ports:
- '26379:26379'
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel.conf:/usr/local/etc/redis/sentinel.conf
sentinel2:
image: redis:5.0.5
container_name: redis5.0.6-sentinel-2
networks:
- network-redis
ports:
- '26380:26379'
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel1.conf:/usr/local/etc/redis/sentinel.conf
sentinel3:
image: redis:5.0.5
container_name: redis5.0.6-sentinel-3
networks:
- network-redis
ports:
- '26381:26379'
command: redis-sentinel /usr/local/etc/redis/sentinel.conf
volumes:
- ./sentinel2.conf:/usr/local/etc/redis/sentinel.conf
  • 编写sentinel配置文件,
1
2
3
4
port 26379
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 172.21.0.2 6379 2
sentinel auth-pass mymaster redis_pwd
  • port 这里使用了三个哨兵,端口号需要单独设置 26379 26380 26381 并且复制出三份。按照要求修改端口号信息。

  • sentinel monitor mymaster 172.21.0.2 6379 1

    • master-name 表示给监视的主节点起一个名称;
    • ip 表示主节点的 IP;这个ip填写正确这里docker演示使用docker inspect [id] 查看master ip
    • port 表示主节点的端口;
    • quorum 表示确认主节点下线的 Sentinel 数量,如果 quorum 设置为 1 表示只要有一台 Sentinel 判断它下线了,就可以确认它真的下线了。
  • sentinel auth-pass mymaster redis_pwd

    • 所以如果 Redis 有密码,也需要设置。
1
2
cp sentinel.conf sentinel1.conf
cp sentinel.conf sentinel2.conf

启动检查

  • 启动docker-compose文件
1
docker-compose -f docker-sentinel-compose.yml up -d
  • Console

  • 进入sentinel容器检查
1
docker -it [id]
  • 进入sentinel
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
redis-cli -p 26379
127.0.0.1:26379> sentinel master mymaster
1) "name"
2) "mymaster"
3) "ip"
4) "172.21.0.2" # 主库的ip
5) "port"
6) "6379"
7) "runid"
8) "c0a0e5fa85c6fc61cf0670d374eac9c35f1b440c"
9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "508"
19) "last-ping-reply"
20) "508"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "6070"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "106612"
29) "config-epoch"
30) "3"
31) "num-slaves" # 两个从库
32) "2"
33) "num-other-sentinels" # //还有两个哨兵
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1"
127.0.0.1:26379>

高可用检查

停止 master容器,等待10s,进入任意sentinel容器,使用sentinel master mymaster命令观察主节点发生变化,观察外挂的Sentinel*.conf 主节点IP发生变化

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
127.0.0.1:26379> sentinel master mymaster
1) "name"
2) "mymaster"
3) "ip"
4) "172.21.0.4" # 这里发现已经发生变化了
5) "port"
6) "6379"
7) "runid"
8) "5114e2ca2ea3eb15a865f2cd5d7c5101e2bf0d34"
9) "flags"
10) "master"
11) "link-pending-commands"
12) "0"
13) "link-refcount"
14) "1"
15) "last-ping-sent"
16) "0"
17) "last-ok-ping-reply"
18) "520"
19) "last-ping-reply"
20) "520"
21) "down-after-milliseconds"
22) "30000"
23) "info-refresh"
24) "1529"
25) "role-reported"
26) "master"
27) "role-reported-time"
28) "1604"
29) "config-epoch"
30) "4"
31) "num-slaves"
32) "2"
33) "num-other-sentinels"
34) "2"
35) "quorum"
36) "2"
37) "failover-timeout"
38) "180000"
39) "parallel-syncs"
40) "1"

  • 还可以进入Redis容器检查Redis运行角色
1
2
3
4
5
6
7
8
9
10
11
reids-cli
127.0.0.1:6379> auth redis_pwd
OK
127.0.0.1:6379> role
1) "slave"
2) "172.21.0.4"
3) (integer) 6379
4) "connected"
5) (integer) 227725
127.0.0.1:6379>

END