半日闲

因过竹院逢僧话,又得浮生半日闲

0%

Docker部署Redis哨兵模式

一、基础配置

节点 IP 端口 备注
sentinel1 127.0.0.1 26379 哨兵1
sentinel2 127.0.0.1 26380 哨兵2
sentinel3 127.0.0.1 26381 哨兵3
redis-master 127.0.0.1 6379 默认主redis
redis-slave1 127.0.0.1 6380 默认从redis1
redis-slave2 127.0.0.1 6381 默认从redis2

二、配置准备

2.1 redis-master.conf

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
64
65
66
67
68
69
70
71
72
73
74
75
76
bind 0.0.0.0

# 启用保护模式
# 即在没有使用bind指令绑定具体地址时
# 或在没有设定密码时
# Redis将拒绝来自外部的连接
# protected-mode yes

# 监听端口
port 6379

# 启动时不打印logo
# 这个不重要,想看logo就打开它
always-show-logo no

# 设定密码认证
requirepass "123456"

# 禁用KEYS命令
# 一方面 KEYS * 命令可以列出所有的键,会影响数据安全
# 另一方面 KEYS 命令会阻塞数据库,在数据库中存储了大量数据时,该命令会消耗很长时间
# 期间对Redis的访问也会被阻塞,而当锁释放的一瞬间,大量请求涌入Redis,会造成Redis直接崩溃
rename-command KEYS ""
# 设定连接主节点所使用的密码
masterauth "123456"

# 此外还应禁止 FLUSHALL 和 FLUSHDB 命令
# 这两个命令会清空数据,并且不会失败
# Generated by CONFIG REWRITE
dir "/data"

replica-announce-ip "192.168.0.35"

```
### 2.2 redis-slave1.conf
```conf
bind 0.0.0.0

# 启用保护模式
# 即在没有使用bind指令绑定具体地址时
# 或在没有设定密码时
# Redis将拒绝来自外部的连接
# protected-mode yes

# 监听端口
port 6380

# 启动时不打印logo
# 这个不重要,想看logo就打开它
always-show-logo no

# 设定密码认证
requirepass "123456"

# 禁用KEYS命令
# 一方面 KEYS * 命令可以列出所有的键,会影响数据安全
# 另一方面 KEYS 命令会阻塞数据库,在数据库中存储了大量数据时,该命令会消耗很长时间
# 期间对Redis的访问也会被阻塞,而当锁释放的一瞬间,大量请求涌入Redis,会造成Redis直接崩溃
rename-command KEYS ""

# 此外还应禁止 FLUSHALL 和 FLUSHDB 命令
# 这两个命令会清空数据,并且不会失败

# 配置master节点信息
# 格式:
#slaveof <masterip> <masterport>
# 此处masterip所指定的redis-server-master是运行master节点的容器名
# Docker容器间可以使用容器名代替实际的IP地址来通信

# 设定连接主节点所使用的密码
masterauth "123456"
# Generated by CONFIG REWRITE
dir "/data"

replica-announce-ip "192.168.0.35"
replicaof 192.168.0.35 6379

2.3 redis-slave2.conf

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
bind 0.0.0.0

# 启用保护模式
# 即在没有使用bind指令绑定具体地址时
# 或在没有设定密码时
# Redis将拒绝来自外部的连接
# protected-mode yes

# 监听端口
port 6381

# 启动时不打印logo
# 这个不重要,想看logo就打开它
always-show-logo no

# 设定密码认证
requirepass "123456"

# 禁用KEYS命令
# 一方面 KEYS * 命令可以列出所有的键,会影响数据安全
# 另一方面 KEYS 命令会阻塞数据库,在数据库中存储了大量数据时,该命令会消耗很长时间
# 期间对Redis的访问也会被阻塞,而当锁释放的一瞬间,大量请求涌入Redis,会造成Redis直接崩溃
rename-command KEYS ""

# 此外还应禁止 FLUSHALL 和 FLUSHDB 命令
# 这两个命令会清空数据,并且不会失败

# 配置master节点信息
# 格式:
#slaveof <masterip> <masterport>
# 此处masterip所指定的redis-server-master是运行master节点的容器名
# Docker容器间可以使用容器名代替实际的IP地址来通信

# 设定连接主节点所使用的密码
masterauth "123456"
# Generated by CONFIG REWRITE
dir "/data"

replica-announce-ip "192.168.0.35"

replicaof 192.168.0.35 6379

2.4 redis-sentinel-1.conf

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
# bind 127.0.0.1

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

# 设定密码认证
#requirepass 123456

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

# 连接主节点的密码
# 格式:sentinel auth-pass <master-name> <password>
sentinel deny-scripts-reconfig yes

# master在连续多长时间无法响应PING指令后,就会主观判定节点下线,默认是30秒
# 格式:sentinel down-after-milliseconds <master-name> <milliseconds>
sentinel monitor mymaster 192.168.0.35 6379 2
# Generated by CONFIG REWRITE
dir "/data"
sentinel down-after-milliseconds mymaster 3000
sentinel auth-pass mymaster 123456
sentinel config-epoch mymaster 3
sentinel leader-epoch mymaster 3
sentinel known-replica mymaster 192.168.0.35 6380
sentinel known-replica mymaster 192.168.0.35 6381
sentinel known-sentinel mymaster 172.18.0.6 26381 97440eacb711c0621ec8e17a1a34072371f7faa4
sentinel known-sentinel mymaster 172.18.0.7 26380 81481f55c39428d75f62fe78719114eda3fd9967
sentinel current-epoch 3

2.5 redis-sentinel-2.conf

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
# bind 127.0.0.1

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

# 设定密码认证
#requirepass 123456

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

# 连接主节点的密码
# 格式:sentinel auth-pass <master-name> <password>
sentinel deny-scripts-reconfig yes

# master在连续多长时间无法响应PING指令后,就会主观判定节点下线,默认是30秒
# 格式:sentinel down-after-milliseconds <master-name> <milliseconds>
sentinel monitor mymaster 192.168.0.35 6379 2
# Generated by CONFIG REWRITE
dir "/data"
sentinel down-after-milliseconds mymaster 3000
sentinel auth-pass mymaster 123456
sentinel config-epoch mymaster 3
sentinel leader-epoch mymaster 3
sentinel known-replica mymaster 192.168.0.35 6380
sentinel known-replica mymaster 192.168.0.35 6381
sentinel known-sentinel mymaster 172.18.0.6 26381 97440eacb711c0621ec8e17a1a34072371f7faa4
sentinel known-sentinel mymaster 172.18.0.7 26380 81481f55c39428d75f62fe78719114eda3fd9967
sentinel current-epoch 3

2.6 redis-sentinel-3.conf

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
# bind 127.0.0.1

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

# 设定密码认证
#requirepass 123456

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

# 连接主节点的密码
# 格式:sentinel auth-pass <master-name> <password>
sentinel deny-scripts-reconfig yes

# master在连续多长时间无法响应PING指令后,就会主观判定节点下线,默认是30秒
# 格式:sentinel down-after-milliseconds <master-name> <milliseconds>
sentinel monitor mymaster 192.168.0.35 6379 2
# Generated by CONFIG REWRITE
dir "/data"
sentinel down-after-milliseconds mymaster 3000
sentinel auth-pass mymaster 123456
sentinel config-epoch mymaster 3
sentinel leader-epoch mymaster 3
sentinel known-replica mymaster 192.168.0.35 6380
sentinel known-replica mymaster 192.168.0.35 6381
sentinel known-sentinel mymaster 172.18.0.6 26381 97440eacb711c0621ec8e17a1a34072371f7faa4
sentinel known-sentinel mymaster 172.18.0.7 26380 81481f55c39428d75f62fe78719114eda3fd9967
sentinel current-epoch 3

2.7 docker-compose.yml(redis)

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
version: '3.5'
services:
redis-master:
container_name: redis-master
image: redis
restart: always
ports:
- 6379:6379
volumes:
- ./redis-master.conf:/usr/local/etc/redis/redis.conf
- ./data/redis-master:/data
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
redis-slave1:
container_name: redis-slave1
image: redis
restart: always
ports:
- 6380:6380
volumes:
- ./redis-slave1.conf:/usr/local/etc/redis/redis.conf
- ./data/redis-slave1:/data
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
redis-slave2:
container_name: redis-slave2
image: redis
restart: always
ports:
- 6381:6381
volumes:
- ./redis-slave2.conf:/usr/local/etc/redis/redis.conf
- ./data/redis-slave2:/data
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
networks:
default:
name: mybridge
external: true

2.8 docker-compose.yml(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
version: '3.5'
services:
sentinel1:
container_name: sentinel1
image: redis
restart: always
ports:
- 26379:26379
volumes:
- ./redis-sentinel-1.conf:/usr/local/etc/redis/redis-sentinel.conf
command: ["redis-sentinel", "/usr/local/etc/redis/redis-sentinel.conf"]
sentinel2:
container_name: sentinel2
image: redis
restart: always
ports:
- 26380:26380
volumes:
- ./redis-sentinel-2.conf:/usr/local/etc/redis/redis-sentinel.conf
command: ["redis-sentinel", "/usr/local/etc/redis/redis-sentinel.conf"]
sentinel3:
container_name: sentinel3
image: redis
restart: always
ports:
- 26381:26381
volumes:
- ./redis-sentinel-3.conf:/usr/local/etc/redis/redis-sentinel.conf
command: ["redis-sentinel", "/usr/local/etc/redis/redis-sentinel.conf"]
networks:
default:
name: mybridge
external: true

三、注意

1、在使用docker-compose进行容器构建时,如果不指定网络时,docker会为此容器组统一创建一个网络,如需指定网络,配置如下

1
2
3
4
networks:
default:
name: mybridge
external: true

注: 根据参考资料1进行容器配置时,采用network-mode: host方式创建时,没有将端口映射到宿主机,导致应用无法连接
2、在使用docker进行部署时,如果采用默认配置,会导致哨兵提供容器内部ip,导致应用无法访问。
所以在进行redis配置的时候,需要加上以下一个配置

replica-announce-ip “192.168.0.35”

三、参考资料