堕落不振功业废,勤耕不辍日月新

redis学习(8)—redis集群主从切换

综合 hailen 13℃

1、客户端验证
192.168.1.138:6381> set user:100 agan
-> Redirected to slot [9308] located at 192.168.1.138:6382
OK
192.168.1.138:6382> set user:200 alex
-> Redirected to slot [15628] located at 192.168.1.138:6383
OK
192.168.1.138:6383>

说明:
第一次执行,落到了第9308个槽号,节点是192.168.1.138:6381
第二次执行,落到了第15628个槽号,节点是192.168.1.138:6382
在集群环境里面,首先会去找槽号,然后再去找ip地址
并且执行完一个后,会轮询到下一个主节点

2、查看集群信息
redis-cli --cluster check 192.168.1.138:6381
说明:

192.168.1.138:6381 (c790b5f9...) -> 0 keys | 5461 slots | 1 slaves.
192.168.1.138:6382 (691e5855...) -> 1 keys | 5462 slots | 1 slaves.
192.168.1.138:6383 (f9200d36...) -> 1 keys | 5461 slots | 1 slaves.
[OK] 2 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.1.138:6381)
M: c790b5f9584777ba8d9f96be5fe42b2efe646635 192.168.1.138:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 33add5fdf6d9cc3eb8656c865403a34b237b4b73 192.168.1.138:6384
   slots: (0 slots) slave
   replicates 691e5855e8f274a6ea61dc95dbed89da87ed6053
S: d7bbc7bb0b501b421d527f6c302a8abe00aa535b 192.168.1.138:6386
   slots: (0 slots) slave
   replicates c790b5f9584777ba8d9f96be5fe42b2efe646635
S: b264b25c716863096d0c2fe39483130cbfeba894 192.168.1.138:6385
   slots: (0 slots) slave
   replicates f9200d36046d8325817c8014155e8f9950760da4
M: 691e5855e8f274a6ea61dc95dbed89da87ed6053 192.168.1.138:6382
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: f9200d36046d8325817c8014155e8f9950760da4 192.168.1.138:6383
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

3、主从切换
先查看节点信息
redis-cli -h 192.168.1.138 -p 6381 -c
192.168.1.138:6381> cluster nodes
33add5fdf6d9cc3eb8656c865403a34b237b4b73 192.168.1.138:6384@16384 slave 691e5855e8f274a6ea61dc95dbed89da87ed6053 0 1626945270000 4 connected
d7bbc7bb0b501b421d527f6c302a8abe00aa535b 192.168.1.138:6386@16386 slave c790b5f9584777ba8d9f96be5fe42b2efe646635 0 1626945269000 6 connected
b264b25c716863096d0c2fe39483130cbfeba894 192.168.1.138:6385@16385 slave f9200d36046d8325817c8014155e8f9950760da4 0 1626945268673 5 connected
691e5855e8f274a6ea61dc95dbed89da87ed6053 192.168.1.138:6382@16382 master - 0 1626945270679 2 connected 5461-10922
f9200d36046d8325817c8014155e8f9950760da4 192.168.1.138:6383@16383 master - 0 1626945269677 3 connected 10923-16383
c790b5f9584777ba8d9f96be5fe42b2efe646635 192.168.1.138:6381@16381 myself,master - 0 1626945267000 1 connected 0-5460

从以上可以看出,6381是主,6386是从,我们把6381停掉看效果

4、停止node1节点
docker stop redis-node-1

5、查看集群信息
进入容器2:
docker exec -it redis-node-2 /bin/bash
进入集群:
root@server:/data# redis-cli -h 192.168.1.138 -p 6382 -c
查看:
192.168.1.138:6382> cluster nodes
f9200d36046d8325817c8014155e8f9950760da4 192.168.1.138:6383@16383 master - 0 1626946245282 3 connected 10923-16383
d7bbc7bb0b501b421d527f6c302a8abe00aa535b 192.168.1.138:6386@16386 master - 0 1626946243273 7 connected 0-5460
c790b5f9584777ba8d9f96be5fe42b2efe646635 192.168.1.138:6381@16381 master,fail - 1626945979547 1626945979144 1 disconnected
b264b25c716863096d0c2fe39483130cbfeba894 192.168.1.138:6385@16385 slave f9200d36046d8325817c8014155e8f9950760da4 0 1626946244276 5 connected
691e5855e8f274a6ea61dc95dbed89da87ed6053 192.168.1.138:6382@16382 myself,master - 0 1626946244000 2 connected 5461-10922
33add5fdf6d9cc3eb8656c865403a34b237b4b73 192.168.1.138:6384@16384 slave 691e5855e8f274a6ea61dc95dbed89da87ed6053 0 1626946244000 4 connected
192.168.1.138:6382>

说明:
从以上可以看出,6381已经fail,6386变成了master

6、我们再把node1起起来
docker start redis-node-1

7、查看集群信息
192.168.1.138:6382> cluster nodes
f9200d36046d8325817c8014155e8f9950760da4 192.168.1.138:6383@16383 master - 0 1626946710261 3 connected 10923-16383
d7bbc7bb0b501b421d527f6c302a8abe00aa535b 192.168.1.138:6386@16386 master - 0 1626946708000 7 connected 0-5460
c790b5f9584777ba8d9f96be5fe42b2efe646635 192.168.1.138:6381@16381 slave d7bbc7bb0b501b421d527f6c302a8abe00aa535b 0 1626946708000 7 connected
b264b25c716863096d0c2fe39483130cbfeba894 192.168.1.138:6385@16385 slave f9200d36046d8325817c8014155e8f9950760da4 0 1626946709258 5 connected
691e5855e8f274a6ea61dc95dbed89da87ed6053 192.168.1.138:6382@16382 myself,master - 0 1626946709000 2 connected 5461-10922
33add5fdf6d9cc3eb8656c865403a34b237b4b73 192.168.1.138:6384@16384 slave 691e5855e8f274a6ea61dc95dbed89da87ed6053 0 1626946709000 4 connected

说明:
6381成为了slave

转载请注明:我是IT » redis学习(8)—redis集群主从切换

喜欢 (0)or分享 (0)