docker redis集群安装和配置

Redis 集群配置

密码:redis123456

备份:未开启

保护模式(protected-mode):未开启。如果开启,需要指定bind: 0.0.0.0,否则应用连接集群会报错

网络规划

IP PORT 描述
192.123.2.1 6380、16380 6380端口:用于客户端连接。16380集群总线端口:用于集群进行故障检测、配置更新、故障转移授权等
192.123.2.1 6381、16381 同上
192.123.2.2 6380、16380 同上
192.123.2.2 6381、16381 同上
192.123.2.3 6380、16380 同上
192.123.2.3 6381、16381 同上

搭建Redis节点服务

以下操作分别在192.123.2.1、192.123.2.2、192.123.2.3机器上都操作一遍。

注意: cluster-announce-ip需要改成对应机器的IP

redis-cluster.tmpl

用于自动生成redis.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
# 节点端口
port ${PORT}
###############################################################
# 集群相关配置
###############################################################
# 是否开启集群模式,默认 no
cluster-enabled yes
# 集群节点信息文件
cluster-config-file nodes.conf
# 集群节点连接超时时间
cluster-node-timeout 15000
# 集群节点 IP,填写宿主机的 IP,防止NAT。不指定的话,cluster nodes查询节点信息有时候会看到映射到了容器IP
cluster-announce-ip 192.123.2.1
# 集群节点映射端口
cluster-announce-port ${PORT}
# 集群节点总线端口,默认:1${PORT}
cluster-announce-bus-port 1${PORT}

# 是否以守护线程的方式启动(后台启动),默认 no
daemonize no
# 保护模式,默认值 yes。开启保护模式以后,需配置 bind ip 或者设置访问密码;关闭保护模式,外部网络可以直接访问
protected-mode no
# 添加访问认证:访问密码
# requirepass 1234
# 如果主节点开启了访问认证,从节点访问主节点需要认证
# masterauth 1234


###############################################################
# 备份相关配置
###############################################################
# 是否开启 AOF 持久化模式,默认 no
appendonly no
# 每秒钟备份
#appendfsync everysec
# 对aof文件进行压缩时,是否执行同步操作
#no-appendfsync-on-rewrite no
# 当目前aof文件大小超过上一次重写时的aof文件大小的100%时会再次进行重写
#auto-aof-rewrite-percentage 100
# 重写前AOF文件的大小最小值 默认 64mb
#auto-aof-rewrite-min-size 64mb

redids-cluster.sh

还行本脚本,会自动根据redis-cluster.tmpl模板文件生产Redis集群节点的redis.conf文件。其中6380 6381为redis节点的端口

1
2
3
4
5
6
for port in `seq 6380 6381`; do \
mkdir -p ./${port}/conf \
&& PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \
&& mkdir -p ./${port}/data \
&& chmod -R 777 ./${port}/
done

生成文件

执行redis-cluster.sh脚本,生成文件树如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[grg@i-8A5B7366 redis-cluster]$ tree
.
├── 6380
│ ├── conf
│ │ └── redis.conf
│ └── data
├── 6381
│ ├── conf
│ │ └── redis.conf
│ └── data
├── docker-compose.yml
├── redis-cluster.sh
└── redis-cluster.tmpl

6 directories, 5 files

docker-compose.yml

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
version: '3.8'

services:
redis-6380:
image: redis:6.2.5-alpine
container_name: redis-6380
ports:
- "6380:6380"
- "16380:16380"
volumes:
- "./6380/conf/redis.conf:/usr/local/etc/redis/redis.conf"
- "./6380/data:/data"
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
environment:
- "TZ=Asia/Shanghai"
restart: always
redis-6381:
image: redis:6.2.5-alpine
container_name: redis-6381
ports:
- "6381:6381"
- "16381:16381"
volumes:
- "./6381/conf/redis.conf:/usr/local/etc/redis/redis.conf"
- "./6381/data:/data"
command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
environment:
- "TZ=Asia/Shanghai"
restart: always

启动节点

1
docker-compose up -d

搭建集群

创建集群

在任意一台服务器上执行以下命令

1
2
3
docker exec -it redis-6380 redis-cli -p 6380 -a redis123456 --cluster create 192.123.2.1:6380 192.123.2.1:6381 192.123.2.2:6380 192.123.2.2:6381 192.123.2.3:6380 192.123.2.3:6381 --cluster-replicas 1

docker exec -it redis-6381 redis-cli -p 6380 -a redis123456 --cluster create 192.123.39.2:6380 192.123.39.2:6381 192.123.39.2:6382 192.123.39.2:6383 192.123.39.2:6384 192.123.39.2:6385 --cluster-replicas 1

出现以下信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.123.2.2:6381 to 192.123.2.1:6380
Adding replica 192.123.2.3:6381 to 192.123.2.2:6380
Adding replica 192.123.2.1:6381 to 192.123.2.3:6380
M: 49c8faffc5613412ccaeb4179467b7051d4884e0 192.123.2.1:6380
slots:[0-5460] (5461 slots) master
S: 31c90ce6d3799d1bf03ba66829eeafbb93272afe 192.123.2.1:6381
replicates 0295ff1c975064083fcb8123f0bb6d7bd5117176
M: 7fb3bb141f217bf3e251d578464b77b1d24ca9b6 192.123.2.2:6380
slots:[5461-10922] (5462 slots) master
S: 98022bc20d35ad63709ee390d4a46c40ded3f4b3 192.123.2.2:6381
replicates 49c8faffc5613412ccaeb4179467b7051d4884e0
M: 0295ff1c975064083fcb8123f0bb6d7bd5117176 192.123.2.3:6380
slots:[10923-16383] (5461 slots) master
S: 061b969acd227ce48691ea6b5cbb73781e7a232b 192.123.2.3:6381
replicates 7fb3bb141f217bf3e251d578464b77b1d24ca9b6
Can I set the above configuration? (type 'yes' to accept):

输入“yes”

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
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 192.123.2.1:6380)
M: 49c8faffc5613412ccaeb4179467b7051d4884e0 192.123.2.1:6380
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 061b969acd227ce48691ea6b5cbb73781e7a232b 192.123.2.3:6381
slots: (0 slots) slave
replicates 7fb3bb141f217bf3e251d578464b77b1d24ca9b6
M: 7fb3bb141f217bf3e251d578464b77b1d24ca9b6 192.123.2.2:6380
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 98022bc20d35ad63709ee390d4a46c40ded3f4b3 192.123.2.2:6381
slots: (0 slots) slave
replicates 49c8faffc5613412ccaeb4179467b7051d4884e0
M: 0295ff1c975064083fcb8123f0bb6d7bd5117176 192.123.2.3:6380
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 31c90ce6d3799d1bf03ba66829eeafbb93272afe 192.123.2.1:6381
slots: (0 slots) slave
replicates 0295ff1c975064083fcb8123f0bb6d7bd5117176
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

出现本信息说明集群创建成功。

集群验证

连接至集群任意节点

1
2
3
4
5
6
# 登录容器
docker exec -it redis-6380 sh
# 切换目录
cd /usr/local/bin/
# 连接基础任意节点
redis-cli -c -h 192.123.2.1 -p 6380 -a redis123456

查看集群信息

1
cluster info

查看集群节点信息

1
cluster nodes

容灾演练

原理,根据集群节点信息,docker stop掉任意一个master节点,看看它的slave节点是否会被选举为master节点。然后再重新重启stop掉的节点,看看它是否会变成slave节点。

1
2
3
4
5
6
7
192.123.2.1:6380> cluster nodes
061b969acd227ce48691ea6b5cbb73781e7a232b 192.123.2.3:6381@16381 master - 0 1630306419000 7 connected 5461-10922
7fb3bb141f217bf3e251d578464b77b1d24ca9b6 192.123.2.2:6380@16380 slave 061b969acd227ce48691ea6b5cbb73781e7a232b 0 1630306420000 7 connected
98022bc20d35ad63709ee390d4a46c40ded3f4b3 192.123.2.2:6381@16381 slave 49c8faffc5613412ccaeb4179467b7051d4884e0 0 1630306421000 1 connected
0295ff1c975064083fcb8123f0bb6d7bd5117176 192.123.2.3:6380@16380 slave 31c90ce6d3799d1bf03ba66829eeafbb93272afe 0 1630306422517 8 connected
49c8faffc5613412ccaeb4179467b7051d4884e0 192.123.2.1:6380@16380 myself,master - 0 1630306420000 1 connected 0-5460
31c90ce6d3799d1bf03ba66829eeafbb93272afe 192.123.2.1:6381@16381 master - 0 1630306421513 8 connected 10923-16383

根据以上信息,可以知道192.123.2.3:6381是master节点,192.123.2.2:6380是它的slave节点。

登录192.123.2.3服务器,执行

1
docker stop redis-6381

在查询节点信息,可以发现 192.123.2.3:6381已经是fail状态,而 192.123.2.2:6380已经变成master节点。

1
2
3
4
5
6
7
192.123.2.1:6380> cluster nodes
061b969acd227ce48691ea6b5cbb73781e7a232b 192.123.2.3:6381@16381 master,fail - 1630308472995 1630308467000 7 disconnected
7fb3bb141f217bf3e251d578464b77b1d24ca9b6 192.123.2.2:6380@16380 master - 0 1630308503375 9 connected 5461-10922
98022bc20d35ad63709ee390d4a46c40ded3f4b3 192.123.2.2:6381@16381 slave 49c8faffc5613412ccaeb4179467b7051d4884e0 0 1630308503000 1 connected
0295ff1c975064083fcb8123f0bb6d7bd5117176 192.123.2.3:6380@16380 slave 31c90ce6d3799d1bf03ba66829eeafbb93272afe 0 1630308505395 8 connected
49c8faffc5613412ccaeb4179467b7051d4884e0 192.123.2.1:6380@16380 myself,master - 0 1630308505000 1 connected 0-5460
31c90ce6d3799d1bf03ba66829eeafbb93272afe 192.123.2.1:6381@16381 master - 0 1630308504385 8 connected 10923-16383

重启fail节点

1
docker start redis-6381

可以发现 192.123.2.3:6381变成了 192.123.2.2:6380 master节点的slave节点。

1
2
3
4
5
6
7
192.123.2.1:6380> cluster nodes
061b969acd227ce48691ea6b5cbb73781e7a232b 192.123.2.3:6381@16381 slave 7fb3bb141f217bf3e251d578464b77b1d24ca9b6 0 1630308672732 9 connected
7fb3bb141f217bf3e251d578464b77b1d24ca9b6 192.123.2.2:6380@16380 master - 0 1630308673000 9 connected 5461-10922
98022bc20d35ad63709ee390d4a46c40ded3f4b3 192.123.2.2:6381@16381 slave 49c8faffc5613412ccaeb4179467b7051d4884e0 0 1630308670721 1 connected
0295ff1c975064083fcb8123f0bb6d7bd5117176 192.123.2.3:6380@16380 slave 31c90ce6d3799d1bf03ba66829eeafbb93272afe 0 1630308673737 8 connected
49c8faffc5613412ccaeb4179467b7051d4884e0 192.123.2.1:6380@16380 myself,master - 0 1630308670000 1 connected 0-5460
31c90ce6d3799d1bf03ba66829eeafbb93272afe 192.123.2.1:6381@16381 master - 0 1630308671726 8 connected 10923-16383

简单压测

1
redis-benchmark -h 192.123.2.1 -p 6380 -t set -c 100 -n 50000 -d 20
  • 本文作者: forever杨
  • 本文链接: https://blog.yl-online.top/posts/7515690f.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。如果文章内容对你有用,请记录到你的笔记中。本博客站点随时会停止服务,请不要收藏、转载!