redis cluster 命令行,执行以下命令需登录cluster,是集群所独有的
集群(cluster)
CLUSTER INFO 打印集群的信息
CLUSTER NODES 列出集群当前已知的所有节点(node),以及这些节点的相关信息

节点(node)
CLUSTER MEET <ip> <port> 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子
CLUSTER FORGET <node_id> 从集群中移除 node_id 指定的节点
CLUSTER REPLICATE <node_id> 将当前节点设置为 node_id 指定的节点的从节点
CLUSTER SAVECONFIG 将节点的配置文件保存到硬盘里面

槽(slot)
CLUSTER ADDSLOTS <slot> [slot ...] 将一个或多个槽(slot)指派(assign)给当前节点
CLUSTER DELSLOTS <slot> [slot ...] 移除一个或多个槽对当前节点的指派
CLUSTER FLUSHSLOTS 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点
CLUSTER SETSLOT <slot> NODE <node_id> 将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派
CLUSTER SETSLOT <slot> MIGRATING <node_id> 将本节点的槽 slot 迁移到 node_id 指定的节点中
CLUSTER SETSLOT <slot> IMPORTING <node_id> 从 node_id 指定的节点中导入槽 slot 到本节点
CLUSTER SETSLOT <slot> STABLE 取消对槽 slot 的导入(import)或者迁移(migrate)

键 (key)
CLUSTER KEYSLOT <key> 计算键 key 应该被放置在哪个槽上
CLUSTER COUNTKEYSINSLOT <slot> 返回槽 slot 目前包含的键值对数量
CLUSTER GETKEYSINSLOT <slot> <count> 返回 count 个 slot 槽中的键

验证将集群某一个节点强行kill后是否可以自动加入集群:
查看redis cluster关系:

[root@--- ~]# redis-cli -h 192.13.168.77 -p  -c -a "ysBhqkYHDifB" cluster nodes
06031e33797ef0aa6427bddb1ff958f7af0f1a4a 192.13.168.77:@ master - connected -
d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 192.13.168.77:@ myself,master - connected -
532ef94c81188111827fef599ee73c0996a04e5e 192.13.168.77:@ slave 06031e33797ef0aa6427bddb1ff958f7af0f1a4a connected
46233f6d8b508be0cedafc5f07aca04210f654ea 192.13.168.77:@ slave ef76f232efb578249e8d0ec8fef8ec02b3524010 connected
ef76f232efb578249e8d0ec8fef8ec02b3524010 192.13.168.77:@ master - connected -
b11fc826c15cdee6e026a59ed98f31c9fa490aaa 192.13.168.77:@ slave d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 connected

查看redis cluster进程:

[root@--- ~]# ps -ef | grep redis
root : ? :: /usr/local/redis-4.0./bin/redis-server 0.0.0.0: [cluster]
root : ? :: /usr/local/redis-4.0./bin/redis-server 0.0.0.0: [cluster]
root : ? :: /usr/local/redis-4.0./bin/redis-server 0.0.0.0: [cluster]
root : ? :: /usr/local/redis-4.0./bin/redis-server 0.0.0.0: [cluster]
root : ? :: /usr/local/redis-4.0./bin/redis-server 0.0.0.0: [cluster]
root : ? :: /usr/local/redis-4.0./bin/redis-server 0.0.0.0: [cluster]
[root@--- ~]# redis-cli -h 192.13.168.77 -p  -c -a "ysBhqkYHDifB" cluster nodes
06031e33797ef0aa6427bddb1ff958f7af0f1a4a 192.13.168.77:@ master - connected -
d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 192.13.168.77:@ myself,master - connected -
532ef94c81188111827fef599ee73c0996a04e5e 192.13.168.77:@ slave 06031e33797ef0aa6427bddb1ff958f7af0f1a4a connected
46233f6d8b508be0cedafc5f07aca04210f654ea 192.13.168.77:@ slave ef76f232efb578249e8d0ec8fef8ec02b3524010 connected
ef76f232efb578249e8d0ec8fef8ec02b3524010 192.13.168.77:@ master - connected -
b11fc826c15cdee6e026a59ed98f31c9fa490aaa 192.13.168.77:@ slave d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 connected

通过redis cluster slots查看主从关系:

[root@--- ~]# redis-cli -h 192.13.168.77 -p  -c -a "ysBhqkYHDifB" cluster slots | xargs -n8 | awk '{print $3":"$4"->"$6":"$7}' | sort -nk2 -t ':' | uniq
192.13.168.77:->192.13.168.77:
192.13.168.77:->192.13.168.77:
192.13.168.77:->192.13.168.77:

强行kill:

[root@--- ~]# kill - 
[root@--- ~]# ps -ef | grep redis
root : ? :: /usr/local/redis-4.0./bin/redis-server 0.0.0.0: [cluster]
root : ? :: /usr/local/redis-4.0./bin/redis-server 0.0.0.0: [cluster]
root : ? :: /usr/local/redis-4.0./bin/redis-server 0.0.0.0: [cluster]
root : ? :: /usr/local/redis-4.0./bin/redis-server 0.0.0.0: [cluster]
root : ? :: /usr/local/redis-4.0./bin/redis-server 0.0.0.0: [cluster]

查看7007从4004的日志,4004升级为新master:

[root@--- log]# cat redis_4004.log
:C Dec ::40.502 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
:C Dec ::40.502 # Redis version=4.0., bits=, commit=, modified=, pid=, just started
:C Dec ::40.502 # Configuration loaded
:M Dec ::40.517 # Server initialized
:M Dec ::40.517 # WARNING overcommit_memory is set to ! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
:M Dec ::40.517 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
:M Dec ::15.782 # User requested shutdown...
:M Dec ::15.783 # Redis is now ready to exit, bye bye...
:C Dec ::25.888 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
:C Dec ::25.888 # Redis version=4.0., bits=, commit=, modified=, pid=, just started
:C Dec ::25.889 # Configuration loaded
:M Dec ::25.896 # Server initialized
:M Dec ::25.896 # WARNING overcommit_memory is set to ! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
:M Dec ::25.896 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
:M Dec ::24.003 # configEpoch set to via CLUSTER SET-CONFIG-EPOCH
:M Dec ::24.180 # IP address for this node updated to 192.13.168.77
:M Dec ::29.005 # Cluster state changed: ok
:M Dec ::56.069 # Manual failover requested by slave 532ef94c81188111827fef599ee73c0996a04e5e.
:M Dec ::56.258 # Failover auth granted to 532ef94c81188111827fef599ee73c0996a04e5e for epoch
:M Dec ::56.263 # Connection with slave 192.13.168.77: lost.
:M Dec ::56.266 # Configuration change detected. Reconfiguring myself as a replica of 532ef94c81188111827fef599ee73c0996a04e5e
:S Dec ::56.538 # Master replication ID changed to 880dfbbc29acda08aa0f997ec0d3d9238f987cca
:S Dec ::13.970 # Connection with master lost.
:S Dec ::14.427 # Error condition on socket for SYNC: Connection refused
:S Dec ::15.427 # Error condition on socket for SYNC: Connection refused
:S Dec ::16.430 # Error condition on socket for SYNC: Connection refused
:S Dec ::17.433 # Error condition on socket for SYNC: Connection refused
:S Dec ::18.436 # Error condition on socket for SYNC: Connection refused
:S Dec ::19.438 # Error condition on socket for SYNC: Connection refused
:S Dec ::20.441 # Error condition on socket for SYNC: Connection refused
:S Dec ::21.443 # Error condition on socket for SYNC: Connection refused
:S Dec ::22.445 # Error condition on socket for SYNC: Connection refused
:S Dec ::23.448 # Error condition on socket for SYNC: Connection refused
:S Dec ::24.449 # Error condition on socket for SYNC: Connection refused
:S Dec ::25.451 # Error condition on socket for SYNC: Connection refused
:S Dec ::26.453 # Error condition on socket for SYNC: Connection refused
:S Dec ::27.455 # Error condition on socket for SYNC: Connection refused
:S Dec ::28.457 # Error condition on socket for SYNC: Connection refused
:S Dec ::29.459 # Error condition on socket for SYNC: Connection refused
:S Dec ::29.960 # Start of election delayed for milliseconds (rank #, offset ).
:S Dec ::30.461 # Error condition on socket for SYNC: Connection refused
:S Dec ::30.662 # Starting a failover election for epoch .
:S Dec ::30.667 # Failover election won: I'm the new master.
:S Dec ::30.667 # configEpoch set to after successful failover
:M Dec ::30.667 # Setting secondary replication ID to 880dfbbc29acda08aa0f997ec0d3d9238f987cca, valid up to offset: . New replication ID is 8ae5eb556fbfd6c13ec33b1123c87de1fbe4db05

到7007data目录下删除nodes-7007.conf:
[root@192-13-168-77 ~]# cd /u02/redis/7007/

[root@192-13-168-77 7007]# ls
conf data log pid
[root@192-13-168-77 7007]# cd data/
[root@192-13-168-77 data]# ls
nodes-7007.conf redis_7007_dump.rdb

[root@192-13-168-77 data]# rm -rf nodes-7007.conf

[root@192-13-168-77 data]# ps -ef | grep redis
root 2039 1 0 14:00 ? 00:00:02 /usr/local/redis-4.0.9/bin/redis-server 0.0.0.0:2002 [cluster]
root 2236 1 0 14:00 ? 00:00:02 /usr/local/redis-4.0.9/bin/redis-server 0.0.0.0:3003 [cluster]
root 2335 1 0 14:00 ? 00:00:02 /usr/local/redis-4.0.9/bin/redis-server 0.0.0.0:4004 [cluster]
root 2365 1 0 14:00 ? 00:00:02 /usr/local/redis-4.0.9/bin/redis-server 0.0.0.0:5005 [cluster]
root 2391 1 0 14:00 ? 00:00:02 /usr/local/redis-4.0.9/bin/redis-server 0.0.0.0:6006 [cluster]

启动7007进程:

[root@--- data]# /usr/local/redis-4.0./bin/redis-server /u02/redis//conf/redis_7007.conf

因强制kill无法自动加入集群:

[root@--- data]# redis-cli -h 192.13.168.77 -p  -c -a "ysBhqkYHDifB" cluster nodes
06031e33797ef0aa6427bddb1ff958f7af0f1a4a 192.13.168.77:@ master - connected -
d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 192.13.168.77:@ myself,master - connected -
532ef94c81188111827fef599ee73c0996a04e5e :@ master,fail,noaddr - disconnected
46233f6d8b508be0cedafc5f07aca04210f654ea 192.13.168.77:@ slave ef76f232efb578249e8d0ec8fef8ec02b3524010 connected
ef76f232efb578249e8d0ec8fef8ec02b3524010 192.13.168.77:@ master - connected -
b11fc826c15cdee6e026a59ed98f31c9fa490aaa 192.13.168.77:@ slave d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 connected

在每个节点执行:
CLUSTER FORGET 06031e33797ef0aa6427bddb1ff958f7af0f1a4a

[root@--- data]# redis-cli -h 192.13.168.77 -p  -c -a "ysBhqkYHDifB"
192.13.168.77:> CLUSTER FORGET 532ef94c81188111827fef599ee73c0996a04e5e
OK 192.13.168.77:> CLUSTER FORGET 532ef94c81188111827fef599ee73c0996a04e5e
OK 192.13.168.77:> CLUSTER FORGET 532ef94c81188111827fef599ee73c0996a04e5e
OK 192.13.168.77:> CLUSTER FORGET 532ef94c81188111827fef599ee73c0996a04e5e
OK

重新启动7007 redis进程:

[root@--- data]# /usr/local/redis-4.0./bin/redis-server /u02/redis//conf/redis_7007.conf

[root@--- data]# ps -ef | grep redis
root : ? :: /usr/local/redis-4.0./bin/redis-server 0.0.0.0: [cluster]
root : ? :: /usr/local/redis-4.0./bin/redis-server 0.0.0.0: [cluster]
root : ? :: /usr/local/redis-4.0./bin/redis-server 0.0.0.0: [cluster]
root : ? :: /usr/local/redis-4.0./bin/redis-server 0.0.0.0: [cluster]
root : ? :: /usr/local/redis-4.0./bin/redis-server 0.0.0.0: [cluster]
root : ? :: /usr/local/redis-4.0./bin/redis-server 0.0.0.0: [cluster]

将7007重新加入为4004的slave

[root@--- data]# redis-trib.rb add-node --slave --master-id 06031e33797ef0aa6427bddb1ff958f7af0f1a4a 192.13.168.77: 192.13.168.77:
>>> Adding node 192.13.168.77: to cluster 192.13.168.77:
>>> Performing Cluster Check (using node 192.13.168.77:)
M: d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 192.13.168.77:
slots:- ( slots) master
additional replica(s)
M: 06031e33797ef0aa6427bddb1ff958f7af0f1a4a 192.13.168.77:
slots:- ( slots) master
additional replica(s)
S: 46233f6d8b508be0cedafc5f07aca04210f654ea 192.13.168.77:
slots: ( slots) slave
replicates ef76f232efb578249e8d0ec8fef8ec02b3524010
M: ef76f232efb578249e8d0ec8fef8ec02b3524010 192.13.168.77:
slots:- ( slots) master
additional replica(s)
S: b11fc826c15cdee6e026a59ed98f31c9fa490aaa 192.13.168.77:
slots: ( slots) slave
replicates d49ebf2a5f3605487ea4c8deee7e2aa2782667e6
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All slots covered.
>>> Send CLUSTER MEET to node 192.13.168.77: to make it join the cluster.
Waiting for the cluster to join.
>>> Configure node as replica of 192.13.168.77:.
[OK] New node added correctly.

检查redis cluster状态:

[root@--- data]# redis-trib.rb check 192.13.168.77:
>>> Performing Cluster Check (using node 192.13.168.77:)
M: d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 192.13.168.77:
slots:- ( slots) master
additional replica(s)
M: 06031e33797ef0aa6427bddb1ff958f7af0f1a4a 192.13.168.77:
slots:- ( slots) master
additional replica(s)
S: 46233f6d8b508be0cedafc5f07aca04210f654ea 192.13.168.77:
slots: ( slots) slave
replicates ef76f232efb578249e8d0ec8fef8ec02b3524010
M: ef76f232efb578249e8d0ec8fef8ec02b3524010 192.13.168.77:
slots:- ( slots) master
additional replica(s)
S: 1dba8274e36bde79a215a77d1f241ae6fc81c03e 192.13.168.77:
slots: ( slots) slave
replicates 06031e33797ef0aa6427bddb1ff958f7af0f1a4a
S: b11fc826c15cdee6e026a59ed98f31c9fa490aaa 192.13.168.77:
slots: ( slots) slave
replicates d49ebf2a5f3605487ea4c8deee7e2aa2782667e6
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All slots covered.

重新查看集群状态:

[root@--- data]# redis-cli -h 192.13.168.77 -p  -c -a "ysBhqkYHDifB" cluster nodes
b11fc826c15cdee6e026a59ed98f31c9fa490aaa 192.13.168.77:@ slave d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 connected
1dba8274e36bde79a215a77d1f241ae6fc81c03e 192.13.168.77:@ slave 06031e33797ef0aa6427bddb1ff958f7af0f1a4a connected
46233f6d8b508be0cedafc5f07aca04210f654ea 192.13.168.77:@ slave ef76f232efb578249e8d0ec8fef8ec02b3524010 connected
06031e33797ef0aa6427bddb1ff958f7af0f1a4a 192.13.168.77:@ master - connected -
ef76f232efb578249e8d0ec8fef8ec02b3524010 192.13.168.77:@ myself,master - connected -
d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 192.13.168.77:@ master - connected -

正常shtudown某一个节点后重新加入集群:

[root@--- data]# redis-cli -h 192.13.168.77 -p  -c -a "ysBhqkYHDifB" shutdown
[root@--- log]# cat redis_7007.log
:S Dec ::32.106 # Connection with master lost.
:S Dec ::32.921 # Error condition on socket for SYNC: Connection refused
:S Dec ::33.924 # Error condition on socket for SYNC: Connection refused
:S Dec ::34.925 # Error condition on socket for SYNC: Connection refused
:S Dec ::35.928 # Error condition on socket for SYNC: Connection refused
:S Dec ::36.930 # Error condition on socket for SYNC: Connection refused
:S Dec ::37.932 # Error condition on socket for SYNC: Connection refused
:S Dec ::38.934 # Error condition on socket for SYNC: Connection refused
:S Dec ::39.935 # Error condition on socket for SYNC: Connection refused
:S Dec ::40.936 # Error condition on socket for SYNC: Connection refused
:S Dec ::41.938 # Error condition on socket for SYNC: Connection refused
:S Dec ::42.940 # Error condition on socket for SYNC: Connection refused
:S Dec ::43.942 # Error condition on socket for SYNC: Connection refused
:S Dec ::44.943 # Error condition on socket for SYNC: Connection refused
:S Dec ::45.945 # Error condition on socket for SYNC: Connection refused
:S Dec ::46.948 # Error condition on socket for SYNC: Connection refused
:S Dec ::47.550 # Start of election delayed for milliseconds (rank #, offset ).
:S Dec ::47.950 # Error condition on socket for SYNC: Connection refused
:S Dec ::48.150 # Starting a failover election for epoch .
:S Dec ::48.155 # Failover election won: I'm the new master.
:S Dec ::48.155 # configEpoch set to after successful failover
:M Dec ::48.155 # Setting secondary replication ID to 8ae5eb556fbfd6c13ec33b1123c87de1fbe4db05, valid up to offset: . New replication ID is e44a29dcbf3dce8e1d29c06bd310ba2ba3d0c41b

查看集群状态:

redis-cli -h 192.13.168.77 -p  -c -a "ysBhqkYHDifB" cluster nodes
Could not connect to Redis at 192.13.168.77:: Connection refused
[root@--- log]# redis-cli -h 192.13.168.77 -p -c -a "ysBhqkYHDifB" cluster nodes
06031e33797ef0aa6427bddb1ff958f7af0f1a4a 192.13.168.77:@ master,fail - disconnected
d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 192.13.168.77:@ myself,master - connected -
46233f6d8b508be0cedafc5f07aca04210f654ea 192.13.168.77:@ slave ef76f232efb578249e8d0ec8fef8ec02b3524010 connected
ef76f232efb578249e8d0ec8fef8ec02b3524010 192.13.168.77:@ master - connected -
1dba8274e36bde79a215a77d1f241ae6fc81c03e 192.13.168.77:@ master - connected -
b11fc826c15cdee6e026a59ed98f31c9fa490aaa 192.13.168.77:@ slave d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 connected

重新启动4004进程:

[root@--- log]# /usr/local/redis-4.0./bin/redis-server /u02/redis//conf/redis_4004.conf

查看集群状态:(4004自动添加进来并变成slave)

[[email protected] log]# redis-cli -h 192.13.168.77 -p  -c -a "ysBhqkYHDifB" cluster nodes
06031e33797ef0aa6427bddb1ff958f7af0f1a4a 192.13.168.77:@ slave 1dba8274e36bde79a215a77d1f241ae6fc81c03e connected
d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 192.13.168.77:@ myself,master - connected -
46233f6d8b508be0cedafc5f07aca04210f654ea 192.13.168.77:@ slave ef76f232efb578249e8d0ec8fef8ec02b3524010 connected
ef76f232efb578249e8d0ec8fef8ec02b3524010 192.13.168.77:@ master - connected -
1dba8274e36bde79a215a77d1f241ae6fc81c03e 192.13.168.77:@ master - connected -
b11fc826c15cdee6e026a59ed98f31c9fa490aaa 192.13.168.77:@ slave d49ebf2a5f3605487ea4c8deee7e2aa2782667e6 connected
05-17 10:12