Redis 集群节点操作

Redis 集群操作

当前Redis Cluster状态:

$ pwd
/home/zxm/BD/redis-cluster
$ ls -l
总用量 32
drwxrwxr-x. 2 zxm zxm 4096 8月  24 20:15 7000
drwxrwxr-x. 2 zxm zxm 4096 8月  24 20:22 7001
drwxrwxr-x. 2 zxm zxm 4096 8月  24 17:32 7002
drwxrwxr-x. 2 zxm zxm 4096 8月  24 20:36 7003
drwxrwxr-x. 2 zxm zxm 4096 8月  24 17:32 7004
drwxrwxr-x. 2 zxm zxm 4096 8月  24 20:23 7005

集群中各个节点状态:

$ ./redis-trib.rb check 192.168.174.3:7000
>>> Performing Cluster Check (using node 192.168.174.3:7000)
M: 7f12512f46ce8e0bac2fffcb7ca05c9941d2864c 192.168.174.3:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: ca33b3d7a60f8df7b74473f86c11f84df609fa45 192.168.174.3:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 8f26d8a386853e338e7176c33e66166b5f835175 192.168.174.3:7003
   slots: (0 slots) slave
   replicates 7f12512f46ce8e0bac2fffcb7ca05c9941d2864c
S: 46f81794cfcf6fbf10fcf18c44f137f446f28be9 192.168.174.3:7005
   slots: (0 slots) slave
   replicates ca33b3d7a60f8df7b74473f86c11f84df609fa45
M: a2e08eda093f5b962881f309b8a3b9d5961e6aa8 192.168.174.3:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 43d98665d41381c89d45c6f69bf8a2622b7b9dc3 192.168.174.3:7004
   slots: (0 slots) slave
   replicates a2e08eda093f5b962881f309b8a3b9d5961e6aa8
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

Redis集群 Resharding

resharding步骤:

1) 使用命令开启reshard命令

./redis-trib.rb reshard host:port

2) 指定需要重新reshard的slots个数

How many slots do you want to move (from 1 to 16384)? 1000

3) 指定需要接收的节点ID

What is the receiving node ID? 7f12512f46ce8e0bac2fffcb7ca05c9941d2864c

4) 指定需要重新shard的节点,输入done开始reshard

Source node #1:ca33b3d7a60f8df7b74473f86c11f84df609fa45
Source node #2:done

具体操作流程如下:

$ ./redis-trib.rb reshard 192.168.174.3:7000
>>> Performing Cluster Check (using node 192.168.174.3:7000)
M: 7f12512f46ce8e0bac2fffcb7ca05c9941d2864c 192.168.174.3:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: ca33b3d7a60f8df7b74473f86c11f84df609fa45 192.168.174.3:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 8f26d8a386853e338e7176c33e66166b5f835175 192.168.174.3:7003
   slots: (0 slots) slave
   replicates 7f12512f46ce8e0bac2fffcb7ca05c9941d2864c
S: 46f81794cfcf6fbf10fcf18c44f137f446f28be9 192.168.174.3:7005
   slots: (0 slots) slave
   replicates ca33b3d7a60f8df7b74473f86c11f84df609fa45
M: a2e08eda093f5b962881f309b8a3b9d5961e6aa8 192.168.174.3:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 43d98665d41381c89d45c6f69bf8a2622b7b9dc3 192.168.174.3:7004
   slots: (0 slots) slave
   replicates a2e08eda093f5b962881f309b8a3b9d5961e6aa8
[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? 7f12512f46ce8e0bac2fffcb7ca05c9941d2864c
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:ca33b3d7a60f8df7b74473f86c11f84df609fa45
Source node #2:done

Ready to move 1000 slots.
  Source nodes:
    M: ca33b3d7a60f8df7b74473f86c11f84df609fa45 192.168.174.3:7002
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
  Destination node:
    M: 7f12512f46ce8e0bac2fffcb7ca05c9941d2864c 192.168.174.3:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
  Resharding plan:
    Moving slot 10923 from ca33b3d7a60f8df7b74473f86c11f84df609fa45
    ... ...
    Moving slot 11921 from ca33b3d7a60f8df7b74473f86c11f84df609fa45
    Moving slot 11922 from ca33b3d7a60f8df7b74473f86c11f84df609fa45
Do you want to proceed with the proposed reshard plan (yes/no)?yes
Moving slot 10923 from 192.168.174.3:7002 to 192.168.174.3:7000:
Moving slot 10924 from 192.168.174.3:7002 to 192.168.174.3:7000:
... ...
Moving slot 11921 from 192.168.174.3:7002 to 192.168.174.3:7000:
Moving slot 11922 from 192.168.174.3:7002 to 192.168.174.3:7000:

重新sharding后集群中各个节点状态:

$ ./redis-trib.rb check 192.168.174.3:7000
>>> Performing Cluster Check (using node 192.168.174.3:7000)
M: 7f12512f46ce8e0bac2fffcb7ca05c9941d2864c 192.168.174.3:7000
   slots:0-5460,10923-11922 (6461 slots) master
   1 additional replica(s)
M: ca33b3d7a60f8df7b74473f86c11f84df609fa45 192.168.174.3:7002
   slots:11923-16383 (4461 slots) master
   1 additional replica(s)
S: 8f26d8a386853e338e7176c33e66166b5f835175 192.168.174.3:7003
   slots: (0 slots) slave
   replicates 7f12512f46ce8e0bac2fffcb7ca05c9941d2864c
S: 46f81794cfcf6fbf10fcf18c44f137f446f28be9 192.168.174.3:7005
   slots: (0 slots) slave
   replicates ca33b3d7a60f8df7b74473f86c11f84df609fa45
M: a2e08eda093f5b962881f309b8a3b9d5961e6aa8 192.168.174.3:7001
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 43d98665d41381c89d45c6f69bf8a2622b7b9dc3 192.168.174.3:7004
   slots: (0 slots) slave
   replicates a2e08eda093f5b962881f309b8a3b9d5961e6aa8
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

新增节点

1) 添加节点

添加redis实例,端口为7006

$ cp -r 7005 7006

修改7006目录中redis-cluster.conf文件中的参数:

$ cat 7006/redis-cluster.conf
bind 192.168.174.3
port 7006
daemonize yes
logfile "/home/zxm/BD/redis-cluster/7006/redis.log"
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

启动redis实例节点

$ cd 7006
$ redis-server redis-cluster.conf

添加Salve节点

方式一:
1)将新的节点添加到集群中

$ ./redis-trib.rb add-node 192.168.174.3:7006 192.168.174.3:7000
 >>> Adding node 192.168.174.3:7006 to cluster 192.168.174.3:7000
>>> Performing Cluster Check (using node 192.168.174.3:7000)
    ... ...
[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.174.3:7006 to make it join the cluster.
[OK] New node added correctly.

2) 进入redis命令行,并查看集群状态

$ redis-cli -c -h 192.168.174.3 -p 7006
192.168.174.3:7006> cluster nodes   
... ... 
fca935760ac28db5cbdc5396b5a326a85e4f8760 192.168.174.3:7006 myself,master - 0 0 0 connected
7f12512f46ce8e0bac2fffcb7ca05c9941d2864c 192.168.174.3:7000 master - 0 1472046744344 7 connected 0-5460 10923-11922
... ...

可以看到默认将新添加的节点作为master

3) 将新节点修改为7000节点的slave,并查看修改后的集群状态

192.168.174.3:7006> cluster replicate 7f12512f46ce8e0bac2fffcb7ca05c9941d2864c
OK
192.168.174.3:7006> cluster nodes
... ...
fca935760ac28db5cbdc5396b5a326a85e4f8760 192.168.174.3:7006 myself,slave 7f12512f46ce8e0bac2fffcb7ca05c9941d2864c 0 0 0 connected
7f12512f46ce8e0bac2fffcb7ca05c9941d2864c 192.168.174.3:7000 master - 0 1472047082185 7 connected 0-5460 10923-11922
... ...

方式二:

1) 将新节点直接作为7000节点的slave添加到集群中

$ ./redis-trib.rb add-node --slave --master-id 7f12512f46ce8e0bac2fffcb7ca05c9941d2864c 192.168.174.3:7006 192.168.174.3:7000
>>> Adding node 192.168.174.3:7006 to cluster 192.168.174.3:7000
>>> Performing Cluster Check (using node 192.168.174.3:7000)
... ...
[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.174.3:7006 to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 192.168.174.3:7000.
[OK] New node added correctly.

2) 查看集群状态

$ ./redis-trib.rb check 192.168.174.3:7001
>>> Performing Cluster Check (using node 192.168.174.3:7001)
... ...
M: 7f12512f46ce8e0bac2fffcb7ca05c9941d2864c 192.168.174.3:7000
   slots:0-5460,10923-11922 (6461 slots) master
   2 additional replica(s)
S: 1a0d5c686108182ab2ea7ca136c8e726d077171e 192.168.174.3:7006
   slots: (0 slots) slave
   replicates 7f12512f46ce8e0bac2fffcb7ca05c9941d2864c
... ...
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

删除节点

删除7000节点,并将7000端口节点上的slots分配到7006上

1) 将7006节点作为master节点添加到集群中,并察看集群状态:

[zxm@node1 src]$ ./redis-trib.rb check 192.168.174.3:7001
>>> Performing Cluster Check (using node 192.168.174.3:7001)
... ... 
M: d7a7e6e57827d94803a139f6f36425276c9e1019 192.168.174.3:7006
   slots: (0 slots) master
   0 additional replica(s)
M: 7f12512f46ce8e0bac2fffcb7ca05c9941d2864c 192.168.174.3:7000
   slots:0-5460,10923-11922 (6461 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.

2) 对集群进行reshard操作,将7000节点上的所有slots转移到7006节点上

[zxm@node1 src]$ ./redis-trib.rb reshard 192.168.174.3:7000
>>> Performing Cluster Check (using node 192.168.174.3:7000)
M: 7f12512f46ce8e0bac2fffcb7ca05c9941d2864c 192.168.174.3:7000
   slots:0-5460,10923-11922 (6461 slots) master
   1 additional replica(s)
... ...
M: d7a7e6e57827d94803a139f6f36425276c9e1019 192.168.174.3:7006
   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)? 6461
What is the receiving node ID? d7a7e6e57827d94803a139f6f36425276c9e1019
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:7f12512f46ce8e0bac2fffcb7ca05c9941d2864c
Source node #2:done

3) 查看集群状态

$ ./redis-trib.rb check 192.168.174.3:7001
>>> Performing Cluster Check (using node 192.168.174.3:7001)
... ...
M: d7a7e6e57827d94803a139f6f36425276c9e1019 192.168.174.3:7006
   slots:0-5460,10923-11922 (6461 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) 删除7000节点

$ ./redis-trib.rb del-node 192.168.174.3:7000 7f12512f46ce8e0bac2fffcb7ca05c9941d2864c
>>> Removing node 7f12512f46ce8e0bac2fffcb7ca05c9941d2864c from cluster 192.168.174.3:7000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

⚠️删除master节点时,该节点上slots必须为零

下面关于Redis的文章您也可能喜欢,不妨参考下:

Redis 的详细介绍:请点这里
Redis 的下载地址:请点这里

相关推荐