1、搭建好redis集群
参考我的博文 http://blog.csdn.net/nuli888/article/details/52134117
下面操作都在同台机子server1 192.168.1.198上操作
2、查看现有节点
[root@localhost ~]# cd /usr/local/redis-3.0.6/src/
[root@localhost src]# redis-cli -c -p 7000 cluster nodes
6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004 slave 26101db06b5c2d4431ca8308cf43d51f6939b4fc 0 1470480300217 7 connected
ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005 slave 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 0 1470480302228 9 connected
e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001 master - 0 1470480302226 8 connected 5000-5460
02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003 slave e60f49920cf8620927b200b0001892d08067d065 0 1470480301220 8 connected
2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000 myself,master - 0 0 9 connected 0-4999 5461-15922
26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198:7002 master - 0 1470480300217 7 connected 15923-16383
3、增加主节点192.168.1.198:7006
[root@localhost cluster]# cd /usr/local/cluster/
[root@localhost cluster]# mkdir 7006
#复制redis配置文件并把端口改成mkdir 7006
[root@localhost cluster]# cp redis.conf 7006/redis.conf && sed -i "s/7000/7006/g" 7006/redis.conf
[root@localhost cluster]# cd 7006
#启动redis
[root@localhost 7006]# redis-server redis.conf
[root@localhost 7006]# ps -ef | grep redis
root 2741 1 0 09:39 ? 00:03:09 redis-server *:7000 [cluster]
root 2747 1 0 09:40 ? 00:03:08 redis-server *:7001 [cluster]
root 2751 1 0 09:40 ? 00:03:07 redis-server *:7002 [cluster]
root 3482 1 0 18:11 ? 00:00:00 redis-server *:7006 [cluster]
root 3486 2687 0 18:11 pts/0 00:00:00 grep redis
[root@localhost 7006]# cd /usr/local/redis-3.0.6/src/
#增加主节点操作192.168.1.198:7000是任意的旧节点
[root@localhost src]# ./redis-trib.rb add-node 192.168.1.198:7006 192.168.1.198:7000
>>> Adding node 192.168.1.198:7006 to cluster 192.168.1.198:7000
>>> Performing Cluster Check (using node 192.168.1.198:7000)
M: 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004
slots: (0 slots) slave
replicates 26101db06b5c2d4431ca8308cf43d51f6939b4fc
S: ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005
slots: (0 slots) slave
replicates 2f70e9f2b4a06a846e46d7034a54e0fe6971beea
M: e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: 02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003
slots: (0 slots) slave
replicates e60f49920cf8620927b200b0001892d08067d065
M: 26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198:7002
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 192.168.1.198:7006 to make it join the cluster.
[OK] New node added correctly.
#查看集群节点情况
[root@localhost src]# redis-cli -c -p 7000 cluster nodes
6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004 slave 26101db06b5c2d4431ca8308cf43d51f6939b4fc 0 1470478484964 7 connected
ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005 slave 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 0 1470478486472 6 connected
e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001 master - 0 1470478484961 8 connected 0-5460
02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003 slave e60f49920cf8620927b200b0001892d08067d065 0 1470478485466 8 connected
4458ef594b310e61bf5f249e8efd707d0da880b0 192.168.1.198:7006 master - 0 1470478485968 0 connected
2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000 myself,master - 0 0 1 connected 5461-10922
26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198:7002 master - 0 1470478485968 7 connected 10923-16383
4、增加从节点192.168.1.198:7007
[root@localhost cluster]# cd /usr/local/cluster/
[root@localhost cluster]# mkdir 7007
[root@localhost cluster]# cp redis.conf 7007/redis.conf && sed -i "s/7000/7007/g" 7007/redis.conf
[root@localhost cluster]# cd 7007
[root@localhost 7007]# redis-server redis.conf
[root@localhost 7007]# ps -ef | grep redis
root 2741 1 0 09:39 ? 00:03:11 redis-server *:7000 [cluster]
root 2747 1 0 09:40 ? 00:03:10 redis-server *:7001 [cluster]
root 2751 1 0 09:40 ? 00:03:09 redis-server *:7002 [cluster]
root 3482 1 0 18:11 ? 00:00:01 redis-server *:7006 [cluster]
root 3495 1 0 18:17 ? 00:00:00 redis-server *:7007 [cluster]
root 3499 2687 0 18:17 pts/0 00:00:00 grep redis
[root@localhost 7007]# cd /usr/local/redis-3.0.6/src/
#--slave,表示添加的是从节点
#--master-id 2f70e9f2b4a06a846e46d7034a54e0fe6971beea,主节点的node id,在这里是7000的node id
#192.168.1.198:7007,新节点
#192.168.1.198:7000集群任一个旧节点
[root@localhost src]# ./redis-trib.rb add-node --slave --master-id 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7007 192.168.1.198:7000
>>> Adding node 192.168.1.198:7007 to cluster 192.168.1.198:7000
>>> Performing Cluster Check (using node 192.168.1.198:7000)
M: 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000
slots:0-4999,5461-15922 (15462 slots) master
1 additional replica(s)
S: 6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004
slots: (0 slots) slave
replicates 26101db06b5c2d4431ca8308cf43d51f6939b4fc
S: ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005
slots: (0 slots) slave
replicates 2f70e9f2b4a06a846e46d7034a54e0fe6971beea
M: e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001
slots:5000-5460 (461 slots) master
1 additional replica(s)
S: 02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003
slots: (0 slots) slave
replicates e60f49920cf8620927b200b0001892d08067d065
M: 4458ef594b310e61bf5f249e8efd707d0da880b0 192.168.1.198:7006
slots: (0 slots) master
0 additional replica(s)
M: 26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198:7002
slots:15923-16383 (461 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.1.198:7007 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 192.168.1.198:7000.
[OK] New node added correctly.
#查看集群节点
[root@localhost src]# redis-cli -c -p 7000 cluster nodes
6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004 slave 26101db06b5c2d4431ca8308cf43d51f6939b4fc 0 1470483081269 7 connected
ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005 slave 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 0 1470483079858 9 connected
e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001 master - 0 1470483080765 8 connected 5000-5460
9e5422dac0c715ef6d81bf82994fe909f97ccbff 192.168.1.198:7007 slave 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 0 1470483081771 9 connected
02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003 slave e60f49920cf8620927b200b0001892d08067d065 0 1470483080261 8 connected
2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000 myself,master - 0 0 9 connected 0-4999 5461-15922
4458ef594b310e61bf5f249e8efd707d0da880b0 192.168.1.198:7006 master - 0 1470483080765 0 connected
26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198:7002 master - 0 1470483080260 7 connected 15923-16383
5、对主节点重新分配slot
新增加的主节点,是没有slots的,主节点如果没有slots的话,存取数据就都不会被选中
可以把分配的过程理解成打扑克牌,all表示大家重新洗牌;输入某个主节点的node id,然后在输入done的话,就好比从某个节点,抽牌
[root@localhost src]# ./redis-trib.rb reshard 192.168.1.198:7006
>>> Performing Cluster Check (using node 192.168.1.198:7006)
M: 4458ef594b310e61bf5f249e8efd707d0da880b0 192.168.1.198:7006
slots:0-943,5000-5027,15923-15950 (1000 slots) master
0 additional replica(s)
S: 9e5422dac0c715ef6d81bf82994fe909f97ccbff 192.168.1.198:7007
slots: (0 slots) slave
replicates 2f70e9f2b4a06a846e46d7034a54e0fe6971beea
S: ebb27bd0a48b67a4f4e0584be27c1c909944e935 192.168.1.199:7005
slots: (0 slots) slave
replicates 2f70e9f2b4a06a846e46d7034a54e0fe6971beea
S: 6c4f18b9e8729c3ab5d43b00b0bc1e2ee976f299 192.168.1.199:7004
slots: (0 slots) slave
replicates 26101db06b5c2d4431ca8308cf43d51f6939b4fc
M: 2f70e9f2b4a06a846e46d7034a54e0fe6971beea 192.168.1.198:7000
slots:944-4999,5461-15922 (14518 slots) master
2 additional replica(s)
M: 26101db06b5c2d4431ca8308cf43d51f6939b4fc 192.168.1.198:7002
slots:15951-16383 (433 slots) master
1 additional replica(s)
S: 02f1958bd5032caca2fd47a56362c8d562d7e621 192.168.1.199:7003
slots: (0 slots) slave
replicates e60f49920cf8620927b200b0001892d08067d065
M: e60f49920cf8620927b200b0001892d08067d065 192.168.1.198:7001
slots:5028-5460 (433 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)? 1000
What is the receiving node ID? 4458ef594b310e61bf5f249e8efd707d0da880b0
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
#输入all 表示从所有的主节点中随机转移,凑够1000个哈希槽
......
Do you want to proceed with the proposed reshard plan (yes/no)? yes
#输入yes,redis集群就开始分配哈希槽了
......
6、删除节点
1)192.168.1.198:7007 节点ip和端口
2)9e5422dac0c715ef6d81bf82994fe909f97ccbff 节点的id值
[root@localhost src]# ./redis-trib.rb del-node 192.168.1.198:7007 '9e5422dac0c715ef6d81bf82994fe909f97ccbff'
>>> Removing node 9e5422dac0c715ef6d81bf82994fe909f97ccbff from cluster 192.168.1.198:7007
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.