3主3从redis集群扩缩容配置案例说明
[toc]
关闭防火墙+启动docker后台服务
systemctl strt docker
新建6个docker实例
大规模的扩缩容,如何快速同时启动,docker提供了瞬间可达
docker run -d --name redis-node-1 --net host --privileged=true \
-v /data/redis/share/redis-node-1:/data \
redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6381
docker run -d --name redis-node-2 --net host --privileged=true \
-v /data/redis/share/redis-node-2:/data \
redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6382
docker run -d --name redis-node-3 --net host --privileged=true \
-v /data/redis/share/redis-node-3:/data \
redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6383
docker run -d --name redis-node-4 --net host --privileged=true \
-v /data/redis/share/redis-node-4:/data \
redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6384
docker run -d --name redis-node-5 --net host --privileged=true \
-v /data/redis/share/redis-node-5:/data \
redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6385
docker run -d --name redis-node-6 --net host --privileged=true \
-v /data/redis/share/redis-node-6:/data \
redis:6.0.8 --cluster-enabled yes --appendonly yes --port 6386
docker ps
进入容器redis-node-1并为6台机器构建集群关系
docker exec -it redis-node-1 /bin/bash
ifconfig #查看ip 192.168.121.129
redis-cli --cluster create 192.168.121.129:6381 192.168.121.129:6382 192.168.121.129:6383 192.168.121.129:6384 192.168.121.129:6385 192.168.121.129:6386 --cluster-replicas 1
# --cluster-replicas 1表示为每一个master创建一个slave结点
Performing hash slots allocation on 6 nodes...哈希槽对六个结点进行分配
M:表示主机器(master主人)
S:表示从机器 (slaves奴隶)
yes接受分配~分配成功
以6381作为切入点,查看集群状态
redis-cli -p 6381
cluster info
- cluster info查看槽位
root@ubuntu:/data# redis-cli -p 6381
127.0.0.1:6381> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:415
cluster_stats_messages_pong_sent:422
cluster_stats_messages_sent:837
cluster_stats_messages_ping_received:417
cluster_stats_messages_pong_received:415
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:837
127.0.0.1:6381> cluster nodes
964043841e4c5519d3cde93cae6b32ebe5ba1830 192.168.121.129:6381@16381 myself,master - 0 1652346428000 1 connected 0-5460
702a95b6eeb02a9d01d38152d599fdf57c70cf0c 192.168.121.129:6386@16386 slave 447dba4f4813f9178d67e62488a4c4c5aba87635 0 1652346430721 3 connected
125e88db573553be475175cd604eb9887506a121 192.168.121.129:6385@16385 slave cc788412e2b72c5a3ea3ba5cd9c80197a554382f 0 1652346429000 2 connected
cc788412e2b72c5a3ea3ba5cd9c80197a554382f 192.168.121.129:6382@16382 master - 0 1652346430000 2 connected 5461-10922
187c17cec8177135b6203328156bb7a7e73714d0 192.168.121.129:6384@16384 slave 964043841e4c5519d3cde93cae6b32ebe5ba1830 0 1652346429715 1 connected
447dba4f4813f9178d67e62488a4c4c5aba87635 192.168.121.129:6383@16383 master - 0 1652346428707 3 connected 10923-16383
127.0.0.1:6381>
1,2,3三台机器是master;4,5,6三台机器是slave
1号主机挂4号,2号主机挂5号,3号挂6号,这个是随机分配的(为什么我的这莫巧我也不知道)
主从容错切换迁移案例
127.0.0.1:6381> set k1 vi
(error) MOVED 12706 192.168.121.129:6383
127.0.0.1:6381> set k1 v1
(error) MOVED 12706 192.168.121.129:6383
127.0.0.1:6381> set k2 v2
OK
127.0.0.1:6381> set k3 v3
OK
127.0.0.1:6381> set k4 v4
(error) MOVED 8455 192.168.121.129:6382
127.0.0.1:6381> set k5 v5
(error) MOVED 12582 192.168.121.129:6383
127.0.0.1:6381> set k6 v6
OK
在集群中这样用的话会失效,不能用单机版连接命令
防止路由失效加参数-c
并新增两个key
redis-cli -p 6381 -c
演示
root@ubuntu:/data# redis-cli -p 8381 -c
Could not connect to Redis at 127.0.0.1:8381: Connection refused
not connected> exit
root@ubuntu:/data# redis-cli -p 6381 -c
127.0.0.1:6381> flushall
OK
127.0.0.1:6381> set k1 v1
-> Redirected to slot [12706] located at 192.168.121.129:6383
OK
192.168.121.129:6383> set k2 v2
-> Redirected to slot [449] located at 192.168.121.129:6381
OK
192.168.121.129:6381> set k3 v3
OK
192.168.121.129:6381> set k4 v4
-> Redirected to slot [8455] located at 192.168.121.129:6382
OK
192.168.121.129:6382> set k5 v5
-> Redirected to slot [12582] located at 192.168.121.129:6383
OK
192.168.121.129:6383> set k6 v6
-> Redirected to slot [325] located at 192.168.121.129:6381
OK
即5号和6号slave不是在1号机,越界了,可以用-c
集群化。会跳转到对应的redis
查看集群信息cluster check
redis-cli --cluster check 192.168.121.129:6381
将第一台机器停掉
docker stop redis-node-1
此时1号宕机了,我们从二号开始作为突破
我们查看集群状态
127.0.0.1:6382> cluster nodes
187c17cec8177135b6203328156bb7a7e73714d0 192.168.121.129:6384@16384 master - 0 1652349890155 7 connected 0-5460
125e88db573553be475175cd604eb9887506a121 192.168.121.129:6385@16385 slave cc788412e2b72c5a3ea3ba5cd9c80197a554382f 0 1652349891169 2 connected
cc788412e2b72c5a3ea3ba5cd9c80197a554382f 192.168.121.129:6382@16382 myself,master - 0 1652349890000 2 connected 5461-10922
702a95b6eeb02a9d01d38152d599fdf57c70cf0c 192.168.121.129:6386@16386 slave 447dba4f4813f9178d67e62488a4c4c5aba87635 0 1652349892186 3 connected
447dba4f4813f9178d67e62488a4c4c5aba87635 192.168.121.129:6383@16383 master - 0 1652349889000 3 connected 10923-16383
964043841e4c5519d3cde93cae6b32ebe5ba1830 192.168.121.129:6381@16381 master,fail - 1652349736906 1652349733000 1 disconnected
结点1还活着,但是显示得是fail,此时6384上位,显示master,完成了集群迁移
我们将6381恢复过来,再看看集群信息
root@ubuntu:/mysql/mydata/mysql-slave/conf# docker start redis-node-1
redis-node-1
root@ubuntu:/mysql/mydata/mysql-slave/conf# docker ps |grep redis
3b80844ec9cd redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up About an hour redis-node-6
1ed04c474cbf redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up About an hour redis-node-5
fa6e8cac6376 redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up About an hour redis-node-4
de3446725228 redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up About an hour redis-node-3
c6cdf1fbc619 redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up About an hour redis-node-2
b6924b43bca4 redis:6.0.8 "docker-entrypoint.s…" About an hour ago Up 8 seconds redis-node-1
此时已经恢复过来了,我们查看节点状态
root@ubuntu:/data# redis-cli -p 6381 -c
127.0.0.1:6381> cluster nodes
125e88db573553be475175cd604eb9887506a121 192.168.121.129:6385@16385 slave cc788412e2b72c5a3ea3ba5cd9c80197a554382f 0 1652350295591 2 connected
187c17cec8177135b6203328156bb7a7e73714d0 192.168.121.129:6384@16384 master - 0 1652350294577 7 connected 0-5460
964043841e4c5519d3cde93cae6b32ebe5ba1830 192.168.121.129:6381@16381 myself,slave 187c17cec8177135b6203328156bb7a7e73714d0 0 1652350293000 7 connected
cc788412e2b72c5a3ea3ba5cd9c80197a554382f 192.168.121.129:6382@16382 master - 0 1652350293000 2 connected 5461-10922
447dba4f4813f9178d67e62488a4c4c5aba87635 192.168.121.129:6383@16383 master - 0 1652350292548 3 connected 10923-16383
702a95b6eeb02a9d01d38152d599fdf57c70cf0c 192.168.121.129:6386@16386 slave 447dba4f4813f9178d67e62488a4c4c5aba87635 0 1652350296602 3 connected
可见1号机器虽然活过来了,但是成为从结点,奴隶结点了
如果我们再把4号机停掉,给1号机器上位得机会,此时6381得1好机器依旧能回到主结点