集群维护:
1>集群添加节点
1、创建两个redis服务7006和7007,配置文件与前述一致,注意端口号即可
2、启动两个节点,并且检查redis服务
[root@apec-008 r7007]# chmod 775 redis-server
[root@apec-008 r7007]# chmod 775 redis-cli
[root@apec-008 r7007]# ./redis-server redis.conf
[root@apec-008 r7007]# ps -ef|grep redis
root 5160 1 0 10月19 ? 00:23:58 ./redis-server 192.168.8.28:7000 [cluster]
root 5164 1 0 10月19 ? 00:23:55 ./redis-server 192.168.8.28:7001 [cluster]
root 5168 1 0 10月19 ? 00:24:12 ./redis-server 192.168.8.28:7002 [cluster]
root 5172 1 0 10月19 ? 00:23:47 ./redis-server 192.168.8.28:7003 [cluster]
root 5176 1 0 10月19 ? 00:23:48 ./redis-server 192.168.8.28:7004 [cluster]
root 5180 1 0 10月19 ? 00:24:11 ./redis-server 192.168.8.28:7005 [cluster]
root 10997 1 0 10:34 ? 00:00:00 ./redis-server 192.168.8.28:7006 [cluster]
root 11007 1 0 10:35 ? 00:00:00 ./redis-server 192.168.8.28:7007 [cluster]
root 11011 10937 0 10:35 pts/0 00:00:00 grep --color=auto redis
[root@apec-008 r7007]#
3、添加一个主节点
[root@apec-008 src]# ./redis-trib.rb add-node 192.168.8.28:7006 192.168.8.28:7000
>>> Adding node 192.168.8.28:7006 to cluster 192.168.8.28:7000
>>> Performing Cluster Check (using node 192.168.8.28:7000)
M: 6bf319dbb78bc54b33b80ef9ed9c96b77497a90e 192.168.8.28:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: aeaa50b29990fd58335a5755564a71be364482d8 192.168.8.28:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 869178d46874074636763951ea59e81a2d124d04 192.168.8.28:7003
slots: (0 slots) slave
replicates 6bf319dbb78bc54b33b80ef9ed9c96b77497a90e
S: 9b2b28ce6db5d0dc4b3849c3e427a87d97d17d07 192.168.8.28:7001
slots: (0 slots) slave
replicates 77682c93fc6bf535a3bf2701777fcc0df73b1791
S: b715e624c719bac4507dfac49f6d2d62a51dd357 192.168.8.28:7005
slots: (0 slots) slave
replicates aeaa50b29990fd58335a5755564a71be364482d8
M: 77682c93fc6bf535a3bf2701777fcc0df73b1791 192.168.8.28:7004
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.
>>> Send CLUSTER MEET to node 192.168.8.28:7006 to make it join the cluster.
[OK] New node added correctly.
注意:192.168.8.28:7006是要加的新节点,192.168.8.28:7000是集群中已存在的任意节点
检查集群状态
[root@apec-008 src]# ./redis-trib.rb check 192.168.8.28:7000
>>> Performing Cluster Check (using node 192.168.8.28:7000)
M: 6bf319dbb78bc54b33b80ef9ed9c96b77497a90e 192.168.8.28:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: aeaa50b29990fd58335a5755564a71be364482d8 192.168.8.28:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 869178d46874074636763951ea59e81a2d124d04 192.168.8.28:7003
slots: (0 slots) slave
replicates 6bf319dbb78bc54b33b80ef9ed9c96b77497a90e
M: 8c3f3f136094fa762c78f239428828e972871e05 192.168.8.28:7006
slots: (0 slots) master
0 additional replica(s)
S: 9b2b28ce6db5d0dc4b3849c3e427a87d97d17d07 192.168.8.28:7001
slots: (0 slots) slave
replicates 77682c93fc6bf535a3bf2701777fcc0df73b1791
S: b715e624c719bac4507dfac49f6d2d62a51dd357 192.168.8.28:7005
slots: (0 slots) slave
replicates aeaa50b29990fd58335a5755564a71be364482d8
M: 77682c93fc6bf535a3bf2701777fcc0df73b1791 192.168.8.28:7004
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.
3、给主节点添加从节点
[root@apec-008 src]# ./redis-trib.rb add-node --slave --master-id 8c3f3f136094fa762c78f239428828e972871e05 192.168.8.28:7007 192.168.8.28:7000
>>> Adding node 192.168.8.28:7007 to cluster 192.168.8.28:7000
>>> Performing Cluster Check (using node 192.168.8.28:7000)
M: 6bf319dbb78bc54b33b80ef9ed9c96b77497a90e 192.168.8.28:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: aeaa50b29990fd58335a5755564a71be364482d8 192.168.8.28:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 869178d46874074636763951ea59e81a2d124d04 192.168.8.28:7003
slots: (0 slots) slave
replicates 6bf319dbb78bc54b33b80ef9ed9c96b77497a90e
M: 8c3f3f136094fa762c78f239428828e972871e05 192.168.8.28:7006
slots: (0 slots) master
0 additional replica(s)
S: 9b2b28ce6db5d0dc4b3849c3e427a87d97d17d07 192.168.8.28:7001
slots: (0 slots) slave
replicates 77682c93fc6bf535a3bf2701777fcc0df73b1791
S: b715e624c719bac4507dfac49f6d2d62a51dd357 192.168.8.28:7005
slots: (0 slots) slave
replicates aeaa50b29990fd58335a5755564a71be364482d8
M: 77682c93fc6bf535a3bf2701777fcc0df73b1791 192.168.8.28:7004
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.
>>> Send CLUSTER MEET to node 192.168.8.28:7007 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 192.168.8.28:7006.
[OK] New node added correctly.
注意:--slave 指定要加的是从节点,--master-id 指定这个从节点的主节点ID,10.10.2.85:6303是需要新加的从节点,10.10.2.70:6300是集群已存在的任意节点
4、迁移一些slots给新节点
执行后显示分配情况,然后根据提示填yes确认迁移
Do you want to proceed with the proposed reshard plan (yes/no)? Yes
命令:
./redis-trib.rb reshard 192.168.8.28:7000
How many slots do you want to move (from 1 to 16384)? 4000 #设置需要把4000个slot做移动
What is the receiving node ID? 8c3f3f136094fa762c78f239428828e972871e05 #设置接收这4000个slot的节点ID,也就是刚才新加的192.168.8.28:7006的ID
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:6bf319dbb78bc54b33b80ef9ed9c96b77497a90e #设置这4000slot的来源ID,这里我从集群之前的3个节点分别去取一部分slot
Source node #2:aeaa50b29990fd58335a5755564a71be364482d8 #设置这4000slot的来源ID,这里我从集群之前的3个节点分别去取一部分slot
Source node #3:77682c93fc6bf535a3bf2701777fcc0df73b1791 #设置这4000slot的来源ID,这里我从集群之前的3个节点分别去取一部分slot
Source node #4:done #输入done开始做一些初始化操作
此处省略
Do you want to proceed with the proposed reshard plan (yes/no)? yes # 输入yes确认开始迁移slot
检查新增节点情况
[root@apec-008 src]# ./redis-trib.rb check 192.168.8.28:7000
>>> Performing Cluster Check (using node 192.168.8.28:7000)
M: 6bf319dbb78bc54b33b80ef9ed9c96b77497a90e 192.168.8.28:7000
slots:1333-5460 (4128 slots) master
1 additional replica(s)
M: aeaa50b29990fd58335a5755564a71be364482d8 192.168.8.28:7002
slots:12256-16383 (4128 slots) master
1 additional replica(s)
S: 869178d46874074636763951ea59e81a2d124d04 192.168.8.28:7003
slots: (0 slots) slave
replicates 6bf319dbb78bc54b33b80ef9ed9c96b77497a90e
M: 8c3f3f136094fa762c78f239428828e972871e05 192.168.8.28:7006
slots:0-1332,5461-6794,10923-12255 (4000 slots) master
1 additional replica(s)
S: 9b2b28ce6db5d0dc4b3849c3e427a87d97d17d07 192.168.8.28:7001
slots: (0 slots) slave
replicates 77682c93fc6bf535a3bf2701777fcc0df73b1791
S: b715e624c719bac4507dfac49f6d2d62a51dd357 192.168.8.28:7005
slots: (0 slots) slave
replicates aeaa50b29990fd58335a5755564a71be364482d8
M: 77682c93fc6bf535a3bf2701777fcc0df73b1791 192.168.8.28:7004
slots:6795-10922 (4128 slots) master
1 additional replica(s)
S: 7784af78f9d04a2da3a9ac6ef48bc1506ca90e40 192.168.8.28:7007
slots: (0 slots) slave
replicates 8c3f3f136094fa762c78f239428828e972871e05
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
验证
[root@apec-008 r7000]# ./redis-cli -c -h 192.168.8.28 -p 7000
192.168.8.28:7000> set a 1
-> Redirected to slot [15495] located at 192.168.8.28:7002
OK
192.168.8.28:7002> set b 2
-> Redirected to slot [3300] located at 192.168.8.28:7000
OK
192.168.8.28:7000> set c 3
-> Redirected to slot [7365] located at 192.168.8.28:7004
OK
192.168.8.28:7004> set d 4
-> Redirected to slot [11298] located at 192.168.8.28:7006
OK
192.168.8.28:7006>
2>删除节点
[root@yw_0_0 ~]# ./redis-trib.rb reshard 192.168.8.28:7000
[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)? 1334 #上文给这个节点迁入了4000个slot,所以这里还选择迁出4000个slot平均分配到三个主节点中
What is the receiving node ID? 77682c93fc6bf535a3bf2701777fcc0df73b1791 #接收这1334slot节点的主ID
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:8c3f3f136094fa762c78f239428828e972871e05 #要下线节点的主ID
Source node #2:done
此处省略
Do you want to proceed with the proposed reshard plan (yes/no)?yes
2、下线从节点
[root@apec-008 src]# ./redis-trib.rb del-node 192.168.8.28:7007 7784af78f9d04a2da3a9ac6ef48bc1506ca90e40
>>> Removing node 7784af78f9d04a2da3a9ac6ef48bc1506ca90e40 from cluster 192.168.8.28:7007
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
3、下线主节点
[root@apec-008 src]# ./redis-trib.rb del-node 192.168.8.28:7006 8c3f3f136094fa762c78f239428828e972871e05
>>> Removing node 8c3f3f136094fa762c78f239428828e972871e05 from cluster 192.168.8.28:7006
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
3、检查
[root@apec-008 src]# ./redis-trib.rb check 192.168.8.28:7000
>>> Performing Cluster Check (using node 192.168.8.28:7000)
M: 6bf319dbb78bc54b33b80ef9ed9c96b77497a90e 192.168.8.28:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: aeaa50b29990fd58335a5755564a71be364482d8 192.168.8.28:7002
slots:5461-6793,12256-16383 (5461 slots) master
1 additional replica(s)
S: 869178d46874074636763951ea59e81a2d124d04 192.168.8.28:7003
slots: (0 slots) slave
replicates 6bf319dbb78bc54b33b80ef9ed9c96b77497a90e
S: 9b2b28ce6db5d0dc4b3849c3e427a87d97d17d07 192.168.8.28:7001
slots: (0 slots) slave
replicates 77682c93fc6bf535a3bf2701777fcc0df73b1791
S: b715e624c719bac4507dfac49f6d2d62a51dd357 192.168.8.28:7005
slots: (0 slots) slave
replicates aeaa50b29990fd58335a5755564a71be364482d8
M: 77682c93fc6bf535a3bf2701777fcc0df73b1791 192.168.8.28:7004
slots:6794-12255 (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.
4、验证
[root@apec-008 r7000]# ./redis-cli -c -h 192.168.8.28 -p 7000
192.168.8.28:7000> get a
-> Redirected to slot [15495] located at 192.168.8.28:7002
"1"
192.168.8.28:7002> get b
-> Redirected to slot [3300] located at 192.168.8.28:7000
"2"
192.168.8.28:7000> get c
-> Redirected to slot [7365] located at 192.168.8.28:7004
"3"
192.168.8.28:7004> get d
"4"