redis 3.2+ 集群实践

redis集群实践
  • 1、创建集群
[root@test04 7000]# redis-trib.rb create --replicas 1 10.9.251.104:7000 10.9.251.104:7001 10.9.251.104:7002 10.9.251.103:7000 10.9.251.103:7001 10.9.251.103:7002
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
10.9.251.104:7000
10.9.251.103:7000
10.9.251.104:7001
Adding replica 10.9.251.103:7001 to 10.9.251.104:7000
Adding replica 10.9.251.104:7002 to 10.9.251.103:7000
Adding replica 10.9.251.103:7002 to 10.9.251.104:7001
M: 23719618cb3996b776a5926061fce31be398d68b 10.9.251.104:7000
slots:0-5460 (5461 slots) master
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
slots:10923-16383 (5461 slots) master
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
replicates 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7000
slots:5461-10922 (5462 slots) master
S: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251.103:7001
replicates 23719618cb3996b776a5926061fce31be398d68b
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
29813:M 11 Jan 13:45:08.977 # configEpoch set to 1 via CLUSTER SET-CONFIG-EPOCH
29801:M 11 Jan 13:45:08.978 # configEpoch set to 2 via CLUSTER SET-CONFIG-EPOCH
29645:M 11 Jan 13:45:08.978 # configEpoch set to 3 via CLUSTER SET-CONFIG-EPOCH
>>> Sending CLUSTER MEET messages to join the cluster
29813:M 11 Jan 13:45:08.990 # IP address for this node updated to 10.9.251.104
29645:M 11 Jan 13:45:09.064 # IP address for this node updated to 10.9.251.104
29801:M 11 Jan 13:45:09.064 # IP address for this node updated to 10.9.251.104
Waiting for the cluster to join....29813:M 11 Jan 13:45:13.876 # Cluster state changed: ok
29801:M 11 Jan 13:45:13.973 # Cluster state changed: ok

29645:S 11 Jan 13:45:14.004 # Cluster state changed: ok
>>> Performing Cluster Check (using node 10.9.251.104:7000)
M: 23719618cb3996b776a5926061fce31be398d68b 10.9.251.104:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7000
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251.103:7001
slots: (0 slots) slave
replicates 23719618cb3996b776a5926061fce31be398d68b
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

2、连接集群,设置值
[root@test04 7000]# redis-cli -c -p 7000 -h 10.9.251.104
10.9.251.104:7000> get name
-> Redirected to slot [5798] located at 10.9.251.103:7000
"zhaojiwei"
10.9.251.103:7000> get age
-> Redirected to slot [741] located at 10.9.251.104:7000
"12"
10.9.251.104:7000> get key:001
-> Redirected to slot [13270] located at 10.9.251.104:7001
"val:001"
10.9.251.104:7001>

3、模拟集群某一主机挂掉情况
以10.9.251.104:7000 挂掉,看数据是否异常。
[root@test04 7000]# redis-trib.rb check 10.9.251.104:7000
>>> Performing Cluster Check (using node 10.9.251.104:7000)
M: 23719618cb3996b776a5926061fce31be398d68b 10.9.251.104:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7000
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251.103:7001
slots: (0 slots) slave
replicates 23719618cb3996b776a5926061fce31be398d68b
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@test04 7000]# ps -ef|grep redis
root 29645 28360 0 13:28 pts/1 00:00:04 redis-server 10.9.251.104:7002 [cluster]
root 29801 28360 0 13:44 pts/1 00:00:01 redis-server 10.9.251.104:7001 [cluster]
root 29813 28360 0 13:44 pts/1 00:00:03 redis-server 10.9.251.104:7000 [cluster]
root 30407 28360 0 14:52 pts/1 00:00:00 grep redis
[root@test04 7000]# kill 29813
29813:signal-handler (1484117582) Received SIGTERM scheduling shutdown...
[root@test04 7000]# 29813:M 11 Jan 14:53:02.322 # User requested shutdown...
29813:M 11 Jan 14:53:02.322 * Calling fsync() on the AOF file.
29813:M 11 Jan 14:53:02.322 * Saving the final RDB snapshot before exiting.
29813:M 11 Jan 14:53:02.338 * DB saved on disk
29813:M 11 Jan 14:53:02.338 * Removing the pid file.
29813:M 11 Jan 14:53:02.338 # Redis is now ready to exit, bye bye...

3.1现在查看集群状态:
可以看到原集群10.9.251.104:7000的副节点10.9.251.103:7001变更为主节点,顶替104:7000。集群继续运行。现在是3个主节点,2个副本节点如下:
[root@test03 7002]# redis-trib.rb check 10.9.251.103:7000
>>> Performing Cluster Check (using node 10.9.251.103:7000)
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7000
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251.103:7001
slots:0-5460 (5461 slots) master
0 additional replica(s)
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
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.2 查看此时集群数据
[root@test03 7002]# redis-cli -c -p 7000 -h 10.9.251.103
10.9.251.103:7000> get name
"zhaojiwei"
10.9.251.103:7000> get age
-> Redirected to slot [741] located at 10.9.251.103:7001
"12"
10.9.251.103:7001> get addres
"\xe6\x9c\x9d\xe9\x98\xb3\xe5\x8c\xba"
10.9.251.103:7001> get key:001
-> Redirected to slot [13270] located at 10.9.251.104:7001
"val:001"
10.9.251.104:7001>
这与原集群数据对比下:可以发现原来key为age的数据位于104:7000上,现在位于103:7001上。数据并没有丢失。

3.3 如果实在是太不幸,刚顶替的主节点10.9.251.103:7001也挂掉了,这时候集群会怎么样?
too bad!集群哈希槽16348个不能被完全覆盖。集群挂了,读和写都不可以。
也即是说集群需要至少有3个主节点在运行。

[root@test03 7002]# ps -ef|grep redis
root 17012 16261 0 13:28 pts/3 00:00:02 redis-server 10.9.251.103:7000 [cluster]
root 17020 16261 0 13:29 pts/3 00:00:05 redis-server 10.9.251.103:7001 [cluster]
root 17076 16261 0 13:35 pts/3 00:00:08 redis-server 10.9.251.103:7002 [cluster]
root 17662 16261 0 15:07 pts/3 00:00:00 grep redis
[root@test03 7002]# kill 17020
17020:signal-handler (1484118485) Received SIGTERM scheduling shutdown...
[root@test03 7002]# 17020:M 11 Jan 15:08:05.200 # User requested shutdown...
17020:M 11 Jan 15:08:05.200 * Calling fsync() on the AOF file.
17020:M 11 Jan 15:08:05.200 * Saving the final RDB snapshot before exiting.
17020:M 11 Jan 15:08:05.219 * DB saved on disk
17020:M 11 Jan 15:08:05.219 * Removing the pid file.
17020:M 11 Jan 15:08:05.219 # Redis is now ready to exit, bye bye...
[root@test04 7000]# redis-trib.rb check 10.9.251.104:7001
>>> Performing Cluster Check (using node 10.9.251.104:7001)
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7000
slots:5461-10922 (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[ERR] Not all 16384 slots are covered by nodes.
[root@test04 7000]# redis-cli -c -p 7001 -h 10.9.251.104
10.9.251.104:7001> get name
(error) CLUSTERDOWN The cluster is down
10.9.251.104:7001> get age
(error) CLUSTERDOWN The cluster is down
10.9.251.104:7001> get addres
(error) CLUSTERDOWN The cluster is down
10.9.251.104:7001> get key:001
(error) CLUSTERDOWN The cluster is down
10.9.251.104:7001> set newkey newval
(error) CLUSTERDOWN The cluster is down
10.9.251.104:7001>

这种情况还能恢复吗?答案是肯定的。启动103:7001。
[root@test03 7001]# redis-server redis.conf &
[1] 17783
[root@test03 7001]# 17783:M 11 Jan 15:22:07.368 * Node configuration loaded, I'm dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 3.2.4 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in cluster mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 7001
| `-._ `._ / _.-' | PID: 17783
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'

17783:M 11 Jan 15:22:07.369 # Server started, Redis version 3.2.4
17783:M 11 Jan 15:22:07.369 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
17783:M 11 Jan 15:22:07.369 * DB loaded from append only file: 0.000 seconds
17783:M 11 Jan 15:22:07.370 * The server is now ready to accept connections on port 7001
17783:M 11 Jan 15:22:09.370 # Cluster state changed: ok
通过启动103:7001后,我们再看看集群中该服务的状态,103:7001处于master主节点,含有5461个哈希槽,但无副本。我们启动104:7000服务后,它会自动变为103:7001的副本。
[root@test03 7001]# redis-trib.rb check 10.9.251.103:7000
>>> Performing Cluster Check (using node 10.9.251.103:7000)
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7000
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251.103:7001
slots:0-5460 (5461 slots) master
0 additional replica(s)
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
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.
启动104:7000服务:
[root@test04 7000]# redis-server redis.conf &
...
[root@test03 7002]# redis-trib.rb check 10.9.251.103:7000
>>> Performing Cluster Check (using node 10.9.251.103:7000)
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7000
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251.103:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 23719618cb3996b776a5926061fce31be398d68b 10.9.251. 104:7000
slots: (0 slots) slave
replicates dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
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.

4、模拟新添加节点
我们在104服务器上同样创建服务104:7003。
[root@test04 redis-cluster-test]# pwd
/usr/local/redis-cluster-test
[root@test04 redis-cluster-test]# ll
total 60
drwxr-xr-x. 2 root root 4096 Jan 11 15:29 7000
drwxr-xr-x. 2 root root 4096 Jan 11 14:43 7001
drwxr-xr-x. 2 root root 4096 Jan 11 14:41 7002
[root@test04 redis-cluster-test]# mkdir 7003&cp 7000/redis.conf 7003
[4] 30805
[4]+ Done mkdir 7003
[root@test04 redis-cluster-test]# ll
total 64
drwxr-xr-x. 2 root root 4096 Jan 11 15:29 7000
drwxr-xr-x. 2 root root 4096 Jan 11 14:43 7001
drwxr-xr-x. 2 root root 4096 Jan 11 14:41 7002
drwxr-xr-x. 2 root root 4096 Jan 11 15:37 7003
[root@test04 redis-cluster-test]# sed -i "s/7000/7003/g" 7003/redis.conf
[root@test04 redis-cluster-test]# cd 7003
[root@test04 7003]# redis-server redis.conf &
...
[root@test04 7003]# ps -ef|grep redis
root 29645 28360 0 13:28 pts/1 00:00:05 redis-server 10.9.251.104:7002 [cluster]
root 29801 28360 0 13:44 pts/1 00:00:02 redis-server 10.9.251.104:7001 [cluster]
root 30736 28360 0 15:29 pts/1 00:00:01 redis-server 10.9.251.104:7000 [cluster]
root 30883 28360 0 15:43 pts/1 00:00:00 redis-server 10.9.251.104:7003 [cluster]
root 30887 28360 0 15:43 pts/1 00:00:00 grep redis
[root@test04 7003]# redis-trib.rb check 10.9.251.104:7001
>>> Performing Cluster Check (using node 10.9.251.104:7001)
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 23719618cb3996b776a5926061fce31be398d68b 10.9.251.104:7000
slots: (0 slots) slave
replicates dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
M: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251.103:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7000
slots:5461-10922 (5462 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.
可以看104:7003已启动,但是它现在还不是集群的一部分,怎办?
add-node 命令:添加新节点104:7003 到集群。
[root@test04 7003]# redis-trib.rb add-node 10.9.251.104:7003 10.9.251.104:7000
>>> Adding node 10.9.251.104:7003 to cluster 10.9.251.104:7000
>>> Performing Cluster Check (using node 10.9.251.104:7000)
S: 23719618cb3996b776a5926061fce31be398d68b 10.9.251.104:7000
slots: (0 slots) slave
replicates dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2
M: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251.103:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7000
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
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.
>>> Send CLUSTER MEET to node 10.9.251.104:7003 to make it join the cluster.
[OK] New node added correctly.
[root@test04 7003]# 30883:M 11 Jan 15:50:28.187 # IP address for this node updated to 10.9.251.104
30883:M 11 Jan 15:50:33.212 # Cluster state changed: ok
[root@test04 7003]# redis-trib.rb check 10.9.251.104:7001
>>> Performing Cluster Check (using node 10.9.251.104:7001)
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 624ef83bad5df4c7091c547afc336e6674a82f92 10.9.251. 104:7003
slots: (0 slots) master
0 additional replica(s)
S: 23719618cb3996b776a5926061fce31be398d68b 10.9.251.104:7000
slots: (0 slots) slave
replicates dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
M: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251.103:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7000
slots:5461-10922 (5462 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.
我们看下集群状态可知,现在新加的节点104:7003作为master主节点,但其下并没有分配哈希槽也没有副本。好吧,让我们先搞下哈希槽。
4.1 为新增节点分配哈希槽
4.1.1 给节点分配指定大小哈希槽(reshard)
[root@test04 7003]# redis-trib.rb reshard 10.9.251.104:7000
>>> Performing Cluster Check (using node 10.9.251.104:7000)
S: 23719618cb3996b776a5926061fce31be398d68b 10.9.251.104:7000
slots: (0 slots) slave
replicates dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2
M: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251.103:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7000
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
slots:10923-16383 (5461 slots) master
1 additional replica(s)
M: 624ef83bad5df4c7091c547afc336e6674a82f92 10.9.251.104:7003
slots: (0 slots) master
0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
询问划分多大哈希槽给目标节点:
How many slots do you want to move (from 1 to 16384) ? 4096
询问哪个节点接收划分的哈希槽:
What is the receiving node ID? 624ef83bad5df4c7091c547afc336e6674a82f92
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
询问划分的哈希槽来源的节点:(即源节点)
Source node #1:all
...
Do you want to proceed with the proposed reshard plan (yes/no)? yes
...

ok,现在新节点有了哈希槽了,可用于接收存储集群的数据了。
[root@test04 7003]# redis-trib.rb check 10.9.251.104:7001
>>> Performing Cluster Check (using node 10.9.251.104:7001)
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
slots:12288-16383 (4096 slots) master
1 additional replica(s)
M: 624ef83bad5df4c7091c547afc336e6674a82f92 10.9.251. 104:7003
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
0 additional replica(s)
S: 23719618cb3996b776a5926061fce31be398d68b 10.9.251.104:7000
slots: (0 slots) slave
replicates dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
M: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251.103:7001
slots:1365-5460 (4096 slots) master
1 additional replica(s)
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7000
slots:6827-10922 (4096 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.

假如 新增的节点104:7003处理能力强些,可以将处理能力较弱的103:7000上的槽多分些给它。如下:
[root@test04 7003]# redis-trib.rb reshard 10.9.251.104:7000
>>> Performing Cluster Check (using node 10.9.251.104:7000)
S: 23719618cb3996b776a5926061fce31be398d68b 10.9.251.104:7000
slots: (0 slots) slave
replicates dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2
M: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251.103:7001
slots:1365-5460 (4096 slots) master
1 additional replica(s)
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7000
slots:6827-10922 (4096 slots) master
1 additional replica(s)
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
slots:12288-16383 (4096 slots) master
1 additional replica(s)
M: 624ef83bad5df4c7091c547afc336e6674a82f92 10.9.251. 104:7003
slots:0-1364,5461-6826,10923-12287 (4096 slots) master
0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 5120
What is the receiving node ID? 624ef83bad5df4c7091c547afc336e6674a82f92
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.

来源于 103:7000节点id的槽:
Source node #1: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2
Source node #2:done
...
Do you want to proceed with the proposed reshard plan (yes/no)? yes
...
[root@test04 7003]# redis-trib.rb check 10.9.251.104:7001
>>> Performing Cluster Check (using node 10.9.251.104:7001)
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
slots:12288-16383 (4096 slots) master
1 additional replica(s)
M: 624ef83bad5df4c7091c547afc336e6674a82f92 10.9.251. 104:7003
slots:0-1364,5461-12287 (8192 slots) master
1 additional replica(s)
S: 23719618cb3996b776a5926061fce31be398d68b 10.9.251.104:7000
slots: (0 slots) slave
replicates dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
M: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251.103:7001
slots:1365-5460 (4096 slots) master
1 additional replica(s)
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates 624ef83bad5df4c7091c547afc336e6674a82f92
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251. 103:7000
slots: (0 slots) master
0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

我擦, 有没有发现103:7000上所有槽被移到 104:7003上!! 看来从103:7000上移动5120个槽太多了,没注意到它原有才4096个【slots:6827-10922 (4096 slots) master】。不过更有意外的是103:7000上也没有副本了,其副本也被分配给了104:7003上了!!

可见当节点被剥夺哈希槽时,存在副本也没意义了,副本会被一起剥夺!

4.1.2 均分哈希槽 rebalance
问题1:主节点哈希槽为0时能不能用rebalance来均分集群下的哈希槽呢?不能
拿上面的例子103:7000被我们剥夺了所有的槽。

[root@test04 7003]# redis-trib.rb rebalance 10.9.251.103:7000
>>> Performing Cluster Check (using node 10.9.251.103:7000)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Rebalancing across 3 nodes. Total weight = 3
Moving 1366 slots from 10.9.251.104:7003 to 10.9.251.103:7001
...
[root@test04 7003]# redis-trib.rb check 10.9.251.104:7001
>>> Performing Cluster Check (using node 10.9.251.104:7001)
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
slots:5462-6826,12288-16383 (5461 slots) master
1 additional replica(s)
M: 624ef83bad5df4c7091c547afc336e6674a82f92 10.9.251.104:7003
slots:6827-12287 (5461 slots) master
1 additional replica(s)
S: 23719618cb3996b776a5926061fce31be398d68b 10.9.251.104:7000
slots: (0 slots) slave
replicates dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
M: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251.103:7001
slots:0-5461 (5462 slots) master
1 additional replica(s)
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates 624ef83bad5df4c7091c547afc336e6674a82f92
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251. 103:7000
slots: (0 slots) master
0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
吼吼,多么悲催,103:7000上依旧没有分配到哈希槽。

我们先用reshard分给2个槽给103:7000上,然后再rebalance呢?
[root@test04 7003]# redis-trib.rb reshard 10.9.251.104:7000
>>> Performing Cluster Check (using node 10.9.251.104:7000)
S: 23719618cb3996b776a5926061fce31be398d68b 10.9.251.104:7000
slots: (0 slots) slave
replicates dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates 624ef83bad5df4c7091c547afc336e6674a82f92
M: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251.103:7001
slots:0-5461 (5462 slots) master
1 additional replica(s)
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7000
slots: (0 slots) master
0 additional replica(s)
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
slots:5462-6826,12288-16383 (5461 slots) master
1 additional replica(s)
M: 624ef83bad5df4c7091c547afc336e6674a82f92 10.9.251.104:7003
slots:6827-12287 (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.
How many slots do you want to move (from 1 to 16384)? 2
What is the receiving node ID? 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node #1:624ef83bad5df4c7091c547afc336e6674a82f92
Source node #2:done

Ready to move 2 slots.
Source nodes:
M: 624ef83bad5df4c7091c547afc336e6674a82f92 10.9.251.104:7003
slots:6827-12287 (5461 slots) master
1 additional replica(s)
Destination node:
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7000
slots: (0 slots) master
0 additional replica(s)
Resharding plan:
Moving slot 6827 from 624ef83bad5df4c7091c547afc336e6674a82f92
Moving slot 6828 from 624ef83bad5df4c7091c547afc336e6674a82f92
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 6827 from 10.9.251.104:7003 to 10.9.251.103:7000:
Moving slot 6828 from 10.9.251.104:7003 to 10.9.251.103:7000:
[root@test04 7003]# redis-trib.rb rebalance 10.9.251.103:7000
>>> Performing Cluster Check (using node 10.9.251.103:7000)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Rebalancing across 4 nodes. Total weight = 4
Moving 1366 slots from 10.9.251.103:7001 to 10.9.251.103:7000
[root@test04 7003]# redis-trib.rb check 10.9.251.104:7001 再check下状态
>>> Performing Cluster Check (using node 10.9.251.104:7001)
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
slots:12288-16383 (4096 slots) master
1 additional replica(s)
M: 624ef83bad5df4c7091c547afc336e6674a82f92 10.9.251.104:7003
slots:8192-12287 (4096 slots) master
1 additional replica(s)
S: 23719618cb3996b776a5926061fce31be398d68b 10.9.251.104:7000
slots: (0 slots) slave
replicates dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
M: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251.103:7001
slots:1366-5461 (4096 slots) master
1 additional replica(s)
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates 624ef83bad5df4c7091c547afc336e6674a82f92
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251. 103:7000
slots:0-1365,5462-8191 (4096 slots) master
0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
吼吼, 103:7000上有了均分的4096个槽了!!!

看来只有有槽(即能接收存储数据的节点)节点才有被rebalance的机会!!!

4.2 为新增节点分配副本(或者说指定节点作为)
103:7000被剥夺槽时其副本也被剥脱了!赔了夫人又折兵!现在为止,为其分配了4096个槽,但依旧没有副本。
为其添加副本节点:在103上创建服务节点7003,并将103:7003分给103:7000作为副本(仅作为测试用,为数据安全实际应该是将slave与master分配到不同的服务器上)。

redis-trib.rb add-node --slave --master-id 目标主节点id 副本节点(ip:port) 集群节点(ip:port)

[root@test03 7003]# redis-trib.rb add-node --slave --master-id 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7003 10.9.251.104:7000
>>> Adding node 10.9.251.103:7003 to cluster 10.9.251.104:7000
>>> Performing Cluster Check (using node 10.9.251.104:7000)
S: 23719618cb3996b776a5926061fce31be398d68b 10.9.251.104:7000
slots: (0 slots) slave
replicates dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates 624ef83bad5df4c7091c547afc336e6674a82f92
M: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251.103:7001
slots:1366-5461 (4096 slots) master
1 additional replica(s)
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7000
slots:0-1365,5462-8191 (4096 slots) master
0 additional replica(s)
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
slots:12288-16383 (4096 slots) master
1 additional replica(s)
M: 624ef83bad5df4c7091c547afc336e6674a82f92 10.9.251.104:7003
slots:8192-12287 (4096 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.
>>> Send CLUSTER MEET to node 10.9.251.103:7003 to make it join the cluster.
Waiting for the cluster to join.18578:M 11 Jan 17:33:36.566 # IP address for this node updated to 10.9.251.103
>>> Configure node as replica of 10.9.251.103:7000.
18578:S 11 Jan 17:33:37.519 # Cluster state changed: ok
[OK] New node added correctly.

5、 删除节点
5.1删除副节点
[root@test04 7003]# redis-trib.rb check 10.9.251.104:7000
>>> Performing Cluster Check (using node 10.9.251.104:7000)
S: 23719618cb3996b776a5926061fce31be398d68b 10.9.251. 104:7000
slots: (0 slots) slave
replicates dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates 624ef83bad5df4c7091c547afc336e6674a82f92
M: 624ef83bad5df4c7091c547afc336e6674a82f92 10.9.251.104:7003
slots:8192-12287 (4096 slots) master
1 additional replica(s)
M: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251.103:7001
slots:1366-5461 (4096 slots) master
1 additional replica(s)
S: 83d3de5543b7c79630b774001742e115ea5806bd 10.9.251.103:7003
slots: (0 slots) slave
replicates 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7000
slots:0-1365,5462-8191 (4096 slots) master
1 additional replica(s)
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@test04 7003]# redis-trib.rb del-node 10.9.251.104:7000 23719618cb3996b776a5926061fce31be398d68b
>>> Removing node 23719618cb3996b776a5926061fce31be398d68b from cluster 10.9.251.104:7000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
删除副节点,直接可以直接被集群 forget掉。

恢复该节点:使用命令 redis-server redis.config &
即可将节点加入集群。
5.1删除master节点
[root@test04 7000]# redis-trib.rb check 10.9.251.104:7000
>>> Performing Cluster Check (using node 10.9.251.104:7000)
S: 23719618cb3996b776a5926061fce31be398d68b 10.9.251.104:7000
slots: (0 slots) slave
replicates dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates 624ef83bad5df4c7091c547afc336e6674a82f92
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7000
slots:0-1365,5462-8191 (4096 slots) master
1 additional replica(s)
S: 83d3de5543b7c79630b774001742e115ea5806bd 10.9.251.103:7003
slots: (0 slots) slave
replicates 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
slots:12288-16383 (4096 slots) master
1 additional replica(s)
M: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251.103:7001
slots:1366-5461 (4096 slots) master
1 additional replica(s)
M: 624ef83bad5df4c7091c547afc336e6674a82f92 10.9.251.104:7003
slots:8192-12287 (4096 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.
[root@test04 7000]# redis-trib.rb del-node 10.9.251.103:7000 624ef83bad5df4c7091c547afc336e6674a82f92
>>> Removing node 624ef83bad5df4c7091c547afc336e6674a82f92 from cluster 10.9.251.103:7000
[ERR] Node 10.9.251.104:7003 is not empty! Reshard data away and try again.
[root@test04 7000]#
提示该节点数据不为空。需要reshard掉该节点哈希槽。 do it! 将 104:7003 上的哈希槽转移到 103:7001上
[root@test04 7000]# redis-trib.rb reshard 10.9.251.103:7000
>>> Performing Cluster Check (using node 10.9.251.103:7000)
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7000
slots:0-1365,5462-8191 (4096 slots) master
1 additional replica(s)
M: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251.103:7001
slots:1366-5461 (4096 slots) master
0 additional replica(s)
M: 624ef83bad5df4c7091c547afc336e6674a82f92 10.9.251.104:7003
slots:8192-12287 (4096 slots) master
1 additional replica(s)
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
slots:12288-16383 (4096 slots) master
1 additional replica(s)
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates 624ef83bad5df4c7091c547afc336e6674a82f92
S: 83d3de5543b7c79630b774001742e115ea5806bd 10.9.251.103:7003
slots: (0 slots) slave
replicates 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 4096
What is the receiving node ID? dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node #1: 624ef83bad5df4c7091c547afc336e6674a82f92
Source node #2:done
...
Moving slot 9792 from 10.9.251.104:7003 to 10.9.251.103:7001:
...
[root@test04 7000]# 5236:M 12 Jan 16:08:58.792 # Connection with slave 10.9.251.104:7002 lost.
可以看到104:7003在移除哈希槽时也丢掉了其副本节点104:7002
[root@test04 7000]# redis-trib.rb check 10.9.251.104:7000
>>> Performing Cluster Check (using node 10.9.251.104:7000)
S: 23719618cb3996b776a5926061fce31be398d68b 10.9.251.104:7000
slots: (0 slots) slave
replicates dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7000
slots:0-1365,5462-8191 (4096 slots) master
1 additional replica(s)
S: 83d3de5543b7c79630b774001742e115ea5806bd 10.9.251.103:7003
slots: (0 slots) slave
replicates 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
slots:12288-16383 (4096 slots) master
1 additional replica(s)
M: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251. 103:7001
slots:1366-5461,8192-12287 ( 8192 slots) master
2 additional replica(s)
M: 624ef83bad5df4c7091c547afc336e6674a82f92 10.9.251. 104:7003
slots: (0 slots) master
0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@test04 7000]# redis-trib.rb del-node 10.9.251.103:7000 624ef83bad5df4c7091c547afc336e6674a82f92
>>> Removing node 624ef83bad5df4c7091c547afc336e6674a82f92 from cluster 10.9.251.103:7000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
5236:M 12 Jan 16:14:34.170 # User requested shutdown...
5236:M 12 Jan 16:14:34.170 * Calling fsync() on the AOF file.
5236:M 12 Jan 16:14:34.170 * Saving the final RDB snapshot before exiting.
5236:M 12 Jan 16:14:34.191 * DB saved on disk
5236:M 12 Jan 16:14:34.191 * Removing the pid file.
5236:M 12 Jan 16:14:34.191 # Redis is now ready to exit, bye bye...
[1]- Done redis-server redis.conf (wd: /usr/local/redis-cluster-test/7003)
(wd now: /usr/local/redis-cluster-test/7000)
移除主节点后,查看集群状态:已不见 104:7003
[root@test04 7000]# redis-trib.rb check 10.9.251.104:7000
[ERR] Sorry, can't connect to node 10.9.251.104:7003
>>> Performing Cluster Check (using node 10.9.251.104:7000)
S: 23719618cb3996b776a5926061fce31be398d68b 10.9.251.104:7000
slots: (0 slots) slave
replicates dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba
S: 88645c07a73eb3df4b8598365b0bc07278c31929 10.9.251.104:7002
slots: (0 slots) slave
replicates dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba
M: 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2 10.9.251.103:7000
slots:0-1365,5462-8191 (4096 slots) master
1 additional replica(s)
S: 83d3de5543b7c79630b774001742e115ea5806bd 10.9.251.103:7003
slots: (0 slots) slave
replicates 4b0a1103f4ede0b7c2b77fb0f25f1d95afd80fb2
S: 832cb8bf04047db470e67db4656d992d1df8c1b2 10.9.251.103:7002
slots: (0 slots) slave
replicates bf01d4e7da106eaeaa52ccda12b310a59cf3c655
M: bf01d4e7da106eaeaa52ccda12b310a59cf3c655 10.9.251.104:7001
slots:12288-16383 (4096 slots) master
1 additional replica(s)
M: dc663f990787a87ae12b97a0b1e3c9ee2e2e65ba 10.9.251.103:7001
slots:1366-5461,8192-12287 (8192 slots) master
2 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
如果此时再重启(比如 104:7003服务器修复完毕后重新上线 ),会如何?
[root@test04 7003]# redis-server redis.conf &
...
M: 624ef83bad5df4c7091c547afc336e6674a82f92 10.9.251.104:7003
slots: (0 slots) master
0 additional replica(s)
...

猜你喜欢

转载自blog.csdn.net/manbufenglin/article/details/54379194