Redis 集群节点上下线操作与扩缩容操作一样,操作前务必做好测试和备份。
下线:迁移卡槽,踢除要下线实例,相当于缩容集群。
1、迁移卡槽
[root@9-2 ~]# psg(){ for i in "$@";do ps aux|grep -v grep |grep -i --color "$i";done;}; psg redis redis 31162 1.3 0.2 2338496 72512 ? Ssl Feb24 2445:54 /data/redis-6379/bin/redis-server 192.168.157.56:6379 [cluster] redis 31489 0.4 0.1 1289728 36672 ? Ssl Feb24 726:54 /data/redis-6479/bin/redis-server 192.168.157.56:6479 [cluster] 59000 124234 0.0 0.0 720448 10240 ? Ssl Apr17 58:00 /redis_exporter --redis.addr redis://192.168.157.56:6379 [root@9-2 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 861080a50959 oliver006/redis_exporter "/redis_exporter --r…" 2 months ago Up 2 months 0.0.0.0:9121->9121/tcp, :::9121->9121/tcp redis_exporter 74c617a1968f prom/node-exporter "/bin/node_exporter" 2 months ago Up 2 months 0.0.0.0:9100->9100/tcp, :::9100->9100/tcp node-exporter [root@9-2 bin]# ./redis-cli --cluster check 192.168.157.56 6379 -a anzhihe123 |grep b8c89b61f98ebda005cdd8792be9944ffab933d1 M: b8c89b61f98ebda005cdd8792be9944ffab933d1 192.168.157.56:6379 [root@9-2 bin]# ./redis-cli --cluster check 192.168.157.56 6379 -a anzhihe123 192.168.157.56:6379 (b8c89b61...) -> 161 keys | 2048 slots | 0 slaves. 192.168.157.47:6379 (e02e79b3...) -> 2311 keys | 2048 slots | 0 slaves. 192.168.157.46:6479 (8c1f3566...) -> 0 keys | 0 slots | 0 slaves. 192.168.157.57:6379 (7941b421...) -> 157 keys | 2048 slots | 0 slaves. 192.168.157.46:6379 (4bbeb80d...) -> 191 keys | 2048 slots | 0 slaves. 192.168.157.56:6479 (0e5e986e...) -> 0 keys | 0 slots | 0 slaves. 192.168.157.45:6479 (ea0c594c...) -> 0 keys | 0 slots | 0 slaves. 192.168.157.55:6479 (b40939ee...) -> 0 keys | 0 slots | 0 slaves. 192.168.157.55:6379 (96bdbaed...) -> 196 keys | 2048 slots | 0 slaves. 192.168.157.57:6479 (9730e1ed...) -> 0 keys | 0 slots | 0 slaves. 192.168.157.45:6379 (c8bedab7...) -> 172 keys | 2048 slots | 0 slaves. 192.168.157.47:6479 (6b98ded1...) -> 0 keys | 0 slots | 0 slaves. 192.168.157.50:6379 (c25cfca9...) -> 185 keys | 2048 slots | 0 slaves. 192.168.157.53:6379 (415fa2c0...) -> 162 keys | 2048 slots | 0 slaves. 192.168.157.50:6479 (c289ace5...) -> 0 keys | 0 slots | 0 slaves. 192.168.157.53:6479 (7910657c...) -> 0 keys | 0 slots | 0 slaves. # 迁移卡槽 ./redis-cli --cluster reshard 192.168.157.56:6379 -a anzhihe123 --cluster-from b8c89b61f98ebda005cdd8792be9944ffab933d1 --cluster-to 9730e1ede51c36f3b20ad91a7f477de921867d07 --cluster-slots 2048 --cluster-yes --cluster-replace [root@9-2 bin]# ./redis-cli --cluster check 192.168.157.56 6379 -a anzhihe123 192.168.157.56:6379 (b8c89b61...) -> 0 keys | 0 slots | 0 slaves. 192.168.157.47:6379 (e02e79b3...) -> 2307 keys | 2048 slots | 0 slaves. 192.168.157.46:6479 (8c1f3566...) -> 0 keys | 0 slots | 0 slaves. 192.168.157.57:6379 (7941b421...) -> 158 keys | 2048 slots | 0 slaves. 192.168.157.46:6379 (4bbeb80d...) -> 192 keys | 2048 slots | 0 slaves. 192.168.157.56:6479 (0e5e986e...) -> 0 keys | 0 slots | 0 slaves. 192.168.157.45:6479 (ea0c594c...) -> 0 keys | 0 slots | 0 slaves. 192.168.157.55:6479 (b40939ee...) -> 0 keys | 0 slots | 0 slaves. 192.168.157.55:6379 (96bdbaed...) -> 196 keys | 2048 slots | 0 slaves. 192.168.157.57:6479 (9730e1ed...) -> 161 keys | 2048 slots | 0 slaves. 192.168.157.45:6379 (c8bedab7...) -> 172 keys | 2048 slots | 0 slaves. 192.168.157.47:6479 (6b98ded1...) -> 0 keys | 0 slots | 0 slaves. 192.168.157.50:6379 (c25cfca9...) -> 181 keys | 2048 slots | 0 slaves. 192.168.157.53:6379 (415fa2c0...) -> 162 keys | 2048 slots | 0 slaves. 192.168.157.50:6479 (c289ace5...) -> 0 keys | 0 slots | 0 slaves. 192.168.157.53:6479 (7910657c...) -> 0 keys | 0 slots | 0 slaves. [OK] 3529 keys in 8 masters. 0.22 keys per slot on average. >>> Performing Cluster Check (using node 192.168.157.56:6379) ...... [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
2、踢除下线实例节点
# 踢除192.168.157.56机器上的redis实例 [root@9-2 bin]# ./redis-cli --cluster del-node 192.168.157.56:6379 b8c89b61f98ebda005cdd8792be9944ffab933d1 -a anzhihe123 >>> Removing node b8c89b61f98ebda005cdd8792be9944ffab933d1 from cluster 192.168.157.57:6379 >>> Sending CLUSTER FORGET messages to the cluster... >>> SHUTDOWN the node. [root@9-2 bin]# ./redis-cli --cluster del-node 192.168.157.56:6479 0e5e986eb4db9fd9cf09f1864dfca87c830fcae7 -a anzhihe123 >>> Removing node 0e5e986eb4db9fd9cf09f1864dfca87c830fcae7 from cluster 192.168.157.57:6479 >>> Sending CLUSTER FORGET messages to the cluster... >>> SHUTDOWN the node.
上线:将扩容节点加入集群,分配卡槽。
# 添加主节点192.168.157.56:6379 ./redis-cli --cluster add-node 192.168.157.56:6379 192.168.157.57:6379 -a anzhihe123 # 添加从节点192.168.157.56:6479(后面跟上192.168.157.56:6379主节点的节点id) ./redis-cli --cluster add-node 192.168.157.56:6479 192.168.157.57:6379 --cluster-slave --cluster-master-id b8c89b61f98ebda005cdd8792be9944ffab933d1 -a anzhihe123 # redis节点重新加入集群报错Node *:6379 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0. # 将data目录下的appendonly.aof、dump.rdb、nodes.conf备份后清除,再加入集群 # 分配卡槽 # 总哈希槽是16384个,平均分组7组,每组2340,多加一组到集群后,变成8组,平均每组分 16384/8=2048,那么要从7组中每组抽取 2048/7=292 个卡槽给192.168.157.56:6379 ./redis-cli --cluster reshard 192.168.157.56:6379 -a anzhihe123 输入 2048,然后输入接收者192.168.157.56:6379集群ID,回车后输入all选择平均分配,最后输入yes即可 # 查看和检查集群 ./redis-cli --cluster info 192.168.157.57:6379 -a anzhihe123 ./redis-cli --cluster check 192.168.157.57:6379 -a anzhihe123 ./redis-cli -h 192.168.157.57 -p 6379 -a anzhihe123 -c 192.168.157.57:6379> cluster nodes
问题解决:
# redis 修复 aof文件 ../bin/redis-check-aof --fix appendonly.aof # redis集群添加节点报错 [ERR] Node 192.168.157.45:6479 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0. 导致异常的主要原因是该节点中默认生成的配置或历史存储数据不一致导致的,清除对应节点的dump.rdb、nodes.conf文件,重启之后即可。 1、停止服务,删除aof/rdb文件 2、删除nodes.conf 3、必要的情况下执行flushdb(删除rdb和conf文件都解决不了的情况下尝试,提前做好备份)
注意:使用redis多实例创建redis集群,master、slave主从节点不要放在同一机器上,防止机器宕机,集群脑裂时,无法进行主从切换。
参考: