共计 9855 个字符,预计需要花费 25 分钟才能阅读完成。
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 的文章您也可能喜欢,不妨参考下:
Ubuntu 14.04 下 Redis 安装及简单测试 http://www.linuxidc.com/Linux/2014-05/101544.htm
Redis 主从复制基本配置 http://www.linuxidc.com/Linux/2015-03/115610.htm
CentOS 7 下 Redis 的安装与配置 http://www.linuxidc.com/Linux/2017-02/140363.htm
Ubuntu 14.04 安装 Redis 与简单配置 http://www.linuxidc.com/Linux/2017-01/139075.htm
Ubuntu 16.04 环境中安装 PHP7.0 Redis 扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm
Redis 单机 & 集群离线安装部署 http://www.linuxidc.com/Linux/2017-03/141403.htm
CentOS 7.0 安装 Redis 3.2.1 详细过程和使用常见问题 http://www.linuxidc.com/Linux/2016-09/135071.htm
Ubuntu 16.04 环境中安装 PHP7.0 Redis 扩展 http://www.linuxidc.com/Linux/2016-09/135631.htm
Ubuntu 15.10 下 Redis 集群部署文档 http://www.linuxidc.com/Linux/2016-06/132340.htm
Redis 实战 中文 PDF http://www.linuxidc.com/Linux/2016-04/129932.htm
Redis 热迁移实战总结 http://www.linuxidc.com/Linux/2017-02/141083.htm
Redis3.0 配置文件详解 http://www.linuxidc.com/Linux/2017-03/141369.htm
Redis 的详细介绍:请点这里
Redis 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-03/141404.htm
