集群命令
--cluster-search-multiple-owners
[root@redis06 etc]# redis-cli --cluster help Cluster Manager Commands: create host1:port1 ... hostN:portN # 创建集群 --cluster-replicas <arg> # 指定从节点数量 check host:port # 检查集群
--cluster-search-multiple-owners # 检查是否有slots同事呗分配给了多个节点 info host:port fix host:port reshard host:port --cluster-from <arg> --cluster-to <arg> --cluster-slots <arg> --cluster-yes --cluster-timeout <arg> --cluster-pipeline <arg> rebalance host:port --cluster-weight <node1=w1...nodeN=wN> --cluster-use-empty-masters --cluster-timeout <arg> --cluster-simulate --cluster-pipeline <arg> --cluster-threshold <arg> add-node new_host:new_port existing_host:existing_port --cluster-slave --cluster-master-id <arg> del-node host:port node_id call host:port command arg arg .. arg set-timeout host:port milliseconds import host:port --cluster-from <arg> --cluster-copy --cluster-replace help For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.
对集群进行重新分片
重新分片并不会对正在运行的集群程序产生任何影响,重新分片操作基本上就是将某些主节点上的哈希槽移动到另外一些主节点上面,和创建集群一样
[root@redis01 etc]# redis-cli --cluster reshard 10.0.0.10:6379 -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Performing Cluster Check (using node 10.0.0.10:6379) M: bb273c158818c55f8f3ad0d0b2d971157fd56c90 10.0.0.10:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: ad58497a499e41df39d2f597e64beb046c5dc756 10.0.0.72:6379 slots: (0 slots) slave replicates fc80f137ccb33e09fe33f2e3364de00bbbc4e888 S: 97c7b9789303a8f1eb8f8516ab0d598c304635b8 10.0.0.71:6379 slots: (0 slots) slave replicates 5a6291fd3fb00119ae4828f5b86554fe4f35a1a7 M: fc80f137ccb33e09fe33f2e3364de00bbbc4e888 10.0.0.61:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 312b3228330eff446e5c9c159182bcdcd61353c6 10.0.0.70:6379 slots: (0 slots) slave replicates bb273c158818c55f8f3ad0d0b2d971157fd56c90 M: 5a6291fd3fb00119ae4828f5b86554fe4f35a1a7 10.0.0.60:6379 slots:[5461-10922] (5462 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? 5a6291fd3fb00119ae4828f5b86554fe4f35a1a7
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. - # 输入节点ID 如果输入all 表示从所有主节点移动槽位到指定节点ID,如果输入all,将直接移动哈希槽,输入done直接退出
Source node #1: fc80f137ccb33e09fe33f2e3364de00bbbc4e888
Source node #2: doneReady to move
1000 slots.
Source nodes:
M: fc80f137ccb33e09fe33f2e3364de00bbbc4e888 10.0.0.61:6379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
Destination node:
M: 5a6291fd3fb00119ae4828f5b86554fe4f35a1a7 10.0.0.60:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
Resharding plan:
Moving slot 10923 from fc80f137ccb33e09fe33f2e3364de00bbbc4e888
Moving slot 10924 from fc80f137ccb33e09fe33f2e3364de00bbbc4e888
Moving slot 10925 from fc80f137ccb33e09fe33f2e3364de00bbbc4e888
Moving slot 10926 from fc80f137ccb33e09fe33f2e3364de00bbbc4e888
Moving slot 10927 from fc80f137ccb33e09fe33f2e3364de00bbbc4e888
- # 输入节点ID 如果输入all 表示从所有主节点移动槽位到指定节点ID,如果输入all,将直接移动哈希槽,输入done直接退出
- # 是否执行重新分片计划
Do you want to proceed with the proposed reshard plan (yes/no)? yes
Moving slot 10923 from 10.0.0.61:6379 to 10.0.0.60:6379:
Moving slot 10924 from 10.0.0.61:6379 to 10.0.0.60:6379:
Moving slot 10925 from 10.0.0.61:6379 to 10.0.0.60:6379:
Moving slot 10926 from 10.0.0.61:6379 to 10.0.0.60:6379:
查看重新分片后的哈希槽分配情况
[root@redis01 etc]# redis-cli -c -h 10.0.0.10 -p 6379 -a 123456 cluster nodes Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. ad58497a499e41df39d2f597e64beb046c5dc756 10.0.0.72:6379@16379 slave fc80f137ccb33e09fe33f2e3364de00bbbc4e888 0 1581333622773 8 connected 97c7b9789303a8f1eb8f8516ab0d598c304635b8 10.0.0.71:6379@16379 slave 5a6291fd3fb00119ae4828f5b86554fe4f35a1a7 0 1581333622567 7 connected bb273c158818c55f8f3ad0d0b2d971157fd56c90 10.0.0.10:6379@16379 myself,master - 0 1581333609000 1 connected 458-5460 fc80f137ccb33e09fe33f2e3364de00bbbc4e888 10.0.0.61:6379@16379 master - 0 1581333621726 8 connected 0-457 5461-6002 11923-16383 312b3228330eff446e5c9c159182bcdcd61353c6 10.0.0.70:6379@16379 slave bb273c158818c55f8f3ad0d0b2d971157fd56c90 0 1581333621209 4 connected 5a6291fd3fb00119ae4828f5b86554fe4f35a1a7 10.0.0.60:6379@16379 master - 0 1581333623293 7 connected 6003-11922
目标节点的哈希槽分为了三部分,前两部分是从其他节点移动过来的
命令输出的解释:
- 节点 ID :例如 3fc783611028b1707fd65345e763befb36454d73 。
- ip:port :节点的 IP 地址和端口号, 例如 127.0.0.1:7000 , 其中 :0 表示的是客户端当前连接的 IP 地址和端口号。
- flags :节点的角色(例如 master 、 slave 、 myself )以及状态(例如 fail ,等等)。
- 如果节点是一个从节点的话, 那么跟在 flags 之后的将是主节点的节点 ID : 例如 127.0.0.1:7002 的主节点的节点 ID 就是 3c3a0c74aae0b56170ccb03a76b60cfe7dc1912e 。
- 集群最近一次向节点发送 PING 命令之后, 过去了多长时间还没接到回复。
- 节点最近一次返回 PONG 回复的时间。
- 节点的配置纪元(configuration epoch):详细信息请参考 Redis 集群规范 。
- 本节点的网络连接情况:例如 connected 。
- 节点目前包含的槽:例如 127.0.0.1:7001 目前包含号码为 5960 至 10921 的哈希槽。
注意:(1)旧版本使用redis-trib.rb reshard 127.0.0.1:6379移动哈希槽
(2)不能将主节点的哈希槽移动到从节点(从节点是只读的)
[root@redis01 etc]# redis-cli -c -h 10.0.0.10 -p 6379 -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 10.0.0.10:6379> quit [root@redis01 etc]# redis-cli --cluster reshard 10.0.0.10:6379 -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. >>> Performing Cluster Check (using node 10.0.0.10:6379) M: bb273c158818c55f8f3ad0d0b2d971157fd56c90 10.0.0.10:6379 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: ad58497a499e41df39d2f597e64beb046c5dc756 10.0.0.72:6379 slots: (0 slots) slave replicates fc80f137ccb33e09fe33f2e3364de00bbbc4e888 S: 97c7b9789303a8f1eb8f8516ab0d598c304635b8 10.0.0.71:6379 slots: (0 slots) slave replicates 5a6291fd3fb00119ae4828f5b86554fe4f35a1a7 M: fc80f137ccb33e09fe33f2e3364de00bbbc4e888 10.0.0.61:6379 slots:[10923-16383] (5461 slots) master 1 additional replica(s) S: 312b3228330eff446e5c9c159182bcdcd61353c6 10.0.0.70:6379 slots: (0 slots) slave replicates bb273c158818c55f8f3ad0d0b2d971157fd56c90 M: 5a6291fd3fb00119ae4828f5b86554fe4f35a1a7 10.0.0.60:6379 slots:[5461-10922] (5462 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? 97c7b9789303a8f1eb8f8516ab0d598c304635b8 *** The specified node (97c7b9789303a8f1eb8f8516ab0d598c304635b8) is not known or not a master, please retry.
redis集群健康状态检查
[root@redis06 etc]# redis-cli --cluster check 10.0.0.72:6379 -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 10.0.0.72:6379 (ad58497a...) -> 0 keys | 5461 slots | 1 slaves. 10.0.0.60:6379 (5a6291fd...) -> 0 keys | 5920 slots | 1 slaves. 10.0.0.10:6379 (bb273c15...) -> 0 keys | 5003 slots | 1 slaves. [OK] 0 keys in 3 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 10.0.0.72:6379) M: ad58497a499e41df39d2f597e64beb046c5dc756 10.0.0.72:6379 slots:[0-457],[5461-6002],[11923-16383] (5461 slots) master 1 additional replica(s) M: 5a6291fd3fb00119ae4828f5b86554fe4f35a1a7 10.0.0.60:6379 slots:[6003-11922] (5920 slots) master 1 additional replica(s) S: 97c7b9789303a8f1eb8f8516ab0d598c304635b8 10.0.0.71:6379 slots: (0 slots) slave replicates 5a6291fd3fb00119ae4828f5b86554fe4f35a1a7 M: bb273c158818c55f8f3ad0d0b2d971157fd56c90 10.0.0.10:6379 slots:[458-5460] (5003 slots) master 1 additional replica(s) S: 312b3228330eff446e5c9c159182bcdcd61353c6 10.0.0.70:6379 slots: (0 slots) slave replicates bb273c158818c55f8f3ad0d0b2d971157fd56c90 S: fc80f137ccb33e09fe33f2e3364de00bbbc4e888 10.0.0.61:6379 slots: (0 slots) slave replicates ad58497a499e41df39d2f597e64beb046c5dc756 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
当一组主、从节点都宕机时,会出现哈希槽无节点接管,导致集群不可用
# 将72和61这组节点宕机 [root@redis06 etc]# redis-cli -c -h 10.0.0.61 -p 6379 -a 123456 shutdown [root@redis06 etc]# redis-cli -c -h 10.0.0.72 -p 6379 -a 123456 shutdown # 检测集群状态 [root@redis06 etc]# redis-cli --cluster check 10.0.0.71:6379 -a 123456 Could not connect to Redis at 10.0.0.61:6379: Connection refused Could not connect to Redis at 10.0.0.72:6379: Connection refused 10.0.0.70:6379 (312b3228...) -> 0 keys | 5003 slots | 1 slaves. 10.0.0.60:6379 (5a6291fd...) -> 0 keys | 5920 slots | 1 slaves. [OK] 0 keys in 2 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 10.0.0.71:6379) S: 97c7b9789303a8f1eb8f8516ab0d598c304635b8 10.0.0.71:6379 slots: (0 slots) slave replicates 5a6291fd3fb00119ae4828f5b86554fe4f35a1a7 M: 312b3228330eff446e5c9c159182bcdcd61353c6 10.0.0.70:6379 slots:[458-5460] (5003 slots) master 1 additional replica(s) M: 5a6291fd3fb00119ae4828f5b86554fe4f35a1a7 10.0.0.60:6379 slots:[6003-11922] (5920 slots) master 1 additional replica(s) S: bb273c158818c55f8f3ad0d0b2d971157fd56c90 10.0.0.10:6379 slots: (0 slots) slave replicates 312b3228330eff446e5c9c159182bcdcd61353c6 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [ERR] Not all 16384 slots are covered by nodes. 查看集群信息 [root@redis06 etc]# redis-cli -c -h 10.0.0.10 -p 6379 -a 123456 cluster info cluster_state:fail cluster_slots_assigned:16384 cluster_slots_ok:10923 cluster_slots_pfail:0 cluster_slots_fail:5461 cluster_known_nodes:6 cluster_size:3 cluster_current_epoch:11 cluster_my_epoch:11 cluster_stats_messages_ping_sent:15829 cluster_stats_messages_pong_sent:1445 cluster_stats_messages_sent:17274 cluster_stats_messages_ping_received:1445 cluster_stats_messages_pong_received:1449 cluster_stats_messages_fail_received:6 cluster_stats_messages_update_received:2 cluster_stats_messages_received:2902 在10这台服务器上设置一个键 [root@redis01 etc]# redis-cli -c -h 10.0.0.10 -p 6379 -a 123456 10.0.0.10:6379> set foo bar (error) CLUSTERDOWN The cluster is down
故障节点重新上线
[root@redis06 etc]# redis-server redis.conf [root@redis06 etc]# ss -lntp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 10.0.0.72:6379 *:* users:(("redis-server",pid=2079,fd=6)) LISTEN 0 128 *:22 *:* users:(("sshd",pid=756,fd=3)) LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=840,fd=13)) LISTEN 0 128 10.0.0.72:16379 *:* users:(("redis-server",pid=2079,fd=9)) LISTEN 0 32 :::21 :::* users:(("vsftpd",pid=764,fd=4)) LISTEN 0 128 :::22 :::* users:(("sshd",pid=756,fd=4)) LISTEN 0 100 ::1:25 :::* users:(("master",pid=840,fd=14)) [root@redis03 etc]# redis-server redis.conf [root@redis03 etc]# ss -lntp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 10.0.0.61:6379 *:* users:(("redis-server",pid=2143,fd=6)) LISTEN 0 128 *:22 *:* users:(("sshd",pid=768,fd=3)) LISTEN 0 128 10.0.0.61:16379 *:* users:(("redis-server",pid=2143,fd=9)) LISTEN 0 128 :::22 :::* users:(("sshd",pid=768,fd=4)) # 再次检测 [root@redis06 etc]# redis-cli --cluster check 10.0.0.71:6379 -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 10.0.0.70:6379 (312b3228...) -> 0 keys | 5003 slots | 1 slaves. 10.0.0.60:6379 (5a6291fd...) -> 0 keys | 5920 slots | 1 slaves. 10.0.0.61:6379 (fc80f137...) -> 0 keys | 5461 slots | 1 slaves. [OK] 0 keys in 3 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 10.0.0.71:6379) S: 97c7b9789303a8f1eb8f8516ab0d598c304635b8 10.0.0.71:6379 slots: (0 slots) slave replicates 5a6291fd3fb00119ae4828f5b86554fe4f35a1a7 M: 312b3228330eff446e5c9c159182bcdcd61353c6 10.0.0.70:6379 slots:[458-5460] (5003 slots) master 1 additional replica(s) M: 5a6291fd3fb00119ae4828f5b86554fe4f35a1a7 10.0.0.60:6379 slots:[6003-11922] (5920 slots) master 1 additional replica(s) S: bb273c158818c55f8f3ad0d0b2d971157fd56c90 10.0.0.10:6379 slots: (0 slots) slave replicates 312b3228330eff446e5c9c159182bcdcd61353c6 M: fc80f137ccb33e09fe33f2e3364de00bbbc4e888 10.0.0.61:6379 slots:[0-457],[5461-6002],[11923-16383] (5461 slots) master 1 additional replica(s) S: ad58497a499e41df39d2f597e64beb046c5dc756 10.0.0.72:6379 slots: (0 slots) slave replicates fc80f137ccb33e09fe33f2e3364de00bbbc4e888 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
集群扩容和缩容和哈希槽操作命令
# 将 ip 和 port 所指定的节点添加到集群中 CLUSTER MEET <ip> <port> # 从集群中移除 node_id 指定的节点 CLUSTER FORGET <node_id> 将当前节点设置为 node_id 指定的节点的从节点 CLUSTER REPLICATE <node_id> 6、将节点的配置文件保存到硬盘里面 CLUSTER SAVECONFIG 7、将一个或多个槽(slot)指派(assign)给当前节点 CLUSTER ADDSLOTS <slot> [slot ...] 8、移除一个或多个槽对当前节点的指派 CLUSTER DELSLOTS <slot> [slot ...] 9、 移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点 CLUSTER FLUSHSLOTS 10、将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派 CLUSTER SETSLOT <slot> NODE <node_id> 11、将本节点的槽 slot 迁移到 node_id 指定的节点中 CLUSTER SETSLOT <slot> MIGRATING <node_id> 12、从 node_id 指定的节点中导入槽 slot 到本节点 CLUSTER SETSLOT <slot> IMPORTING <node_id> 13、取消对槽 slot 的导入(import)或者迁移(migrate) CLUSTER SETSLOT <slot> STABLE 14、计算键 key 应该被放置在哪个槽上 CLUSTER KEYSLOT <key> 15、返回槽 slot 目前包含的键值对数量 CLUSTER COUNTKEYSINSLOT <slot> 16、返回 count 个 slot 槽中的键 CLUSTER GETKEYSINSLOT <slot> <count>
测试从节点是否能进行故障切换
# 停掉一个主节点
[root@redis06 etc]# redis-cli -c -h 10.0.0.61 -p 6379 -a 123456 shutdown Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. [root@redis06 etc]# ss -lntp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 10.0.0.72:6379 *:* users:(("redis-server",pid=1973,fd=6)) LISTEN 0 128 *:22 *:* users:(("sshd",pid=756,fd=3)) LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=840,fd=13)) LISTEN 0 128 10.0.0.72:16379 *:* users:(("redis-server",pid=1973,fd=9)) LISTEN 0 32 :::21 :::* users:(("vsftpd",pid=764,fd=4)) LISTEN 0 128 :::22 :::* users:(("sshd",pid=756,fd=4)) LISTEN 0 100 ::1:25 :::* users:(("master",pid=840,fd=14))
[root@redis06 etc]# redis-cli -c -h 10.0.0.61 -p 6379 -a 123456 cluster nodes Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. Could not connect to Redis at 10.0.0.61:6379: Connection refused
#查看集群状态
[root@redis06 etc]# redis-cli -c -h 10.0.0.72 -p 6379 -a 123456 cluster nodes Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 5a6291fd3fb00119ae4828f5b86554fe4f35a1a7 10.0.0.60:6379@16379 master - 0 1581337801101 7 connected 6003-11922 ad58497a499e41df39d2f597e64beb046c5dc756 10.0.0.72:6379@16379 myself,master - 0 1581337801000 9 connected 0-457 5461-6002 11923-16383 97c7b9789303a8f1eb8f8516ab0d598c304635b8 10.0.0.71:6379@16379 slave 5a6291fd3fb00119ae4828f5b86554fe4f35a1a7 0 1581337803166 7 connected bb273c158818c55f8f3ad0d0b2d971157fd56c90 10.0.0.10:6379@16379 master - 0 1581337802652 1 connected 458-5460 312b3228330eff446e5c9c159182bcdcd61353c6 10.0.0.70:6379@16379 slave bb273c158818c55f8f3ad0d0b2d971157fd56c90 0 1581337802134 4 connected fc80f137ccb33e09fe33f2e3364de00bbbc4e888 10.0.0.61:6379@16379 master,fail - 1581337776852 1581337775113 8 disconnected
宕机节点恢复
[root@redis03 etc]# ss -lntp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* users:(("sshd",pid=768,fd=3)) LISTEN 0 128 :::22 :::* users:(("sshd",pid=768,fd=4)) [root@redis03 etc]# redis-server redis.conf [root@redis03 etc]# ss -lntp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 10.0.0.61:6379 *:* users:(("redis-server",pid=2109,fd=6)) LISTEN 0 128 *:22 *:* users:(("sshd",pid=768,fd=3)) LISTEN 0 128 10.0.0.61:16379 *:* users:(("redis-server",pid=2109,fd=9)) LISTEN 0 128 :::22 :::* users:(("sshd",pid=768,fd=4))
查看集群状态
[OK] All 16384 slots covered. [root@redis06 etc]# redis-cli --cluster check 10.0.0.72:6379 -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 10.0.0.72:6379 (ad58497a...) -> 0 keys | 5461 slots | 1 slaves. 10.0.0.60:6379 (5a6291fd...) -> 0 keys | 5920 slots | 1 slaves. 10.0.0.10:6379 (bb273c15...) -> 0 keys | 5003 slots | 1 slaves. [OK] 0 keys in 3 masters. 0.00 keys per slot on average. >>> Performing Cluster Check (using node 10.0.0.72:6379) M: ad58497a499e41df39d2f597e64beb046c5dc756 10.0.0.72:6379 slots:[0-457],[5461-6002],[11923-16383] (5461 slots) master 1 additional replica(s) M: 5a6291fd3fb00119ae4828f5b86554fe4f35a1a7 10.0.0.60:6379 slots:[6003-11922] (5920 slots) master 1 additional replica(s) S: 97c7b9789303a8f1eb8f8516ab0d598c304635b8 10.0.0.71:6379 slots: (0 slots) slave replicates 5a6291fd3fb00119ae4828f5b86554fe4f35a1a7 M: bb273c158818c55f8f3ad0d0b2d971157fd56c90 10.0.0.10:6379 slots:[458-5460] (5003 slots) master 1 additional replica(s) S: 312b3228330eff446e5c9c159182bcdcd61353c6 10.0.0.70:6379 slots: (0 slots) slave replicates bb273c158818c55f8f3ad0d0b2d971157fd56c90 S: fc80f137ccb33e09fe33f2e3364de00bbbc4e888 10.0.0.61:6379 slots: (0 slots) slave replicates ad58497a499e41df39d2f597e64beb046c5dc756 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
计数器测试集群一致性
consistency-test.rb创建了多个计数器(默认为 1000 个), 并通过发送 INCR key 命令来增加这些计数器的值。在增加计数器值的同时, consistency-test.rb 还执行以下操作: 每次使用 INCR key 命令更新一个计数器时, 应用会记录下计数器执行 INCR key 命令之后应该有的值。 举个例子, 如果计数器的起始值为 0 , 而这次是程序第 50 次向它发送 INCR key 命令, 那么计数器的值应该是 50 。 在每次发送 INCR key 命令之前, 程序会随机从集群中读取一个计数器的值, 并将它与自己记录的值进行对比, 看两个值是否相同。 换句话说, 这个程序是一个一致性检查器(consistency checker):
如果集群在执行 INCR key 命令的过程中, 丢失了某条 INCR key 命令, 又或者多执行了某条客户端没有确认到的 INCR key 命令, 那么检查器将察觉到这一点
-----在前一种情况中, consistency-test.rb 记录的计数器值将比集群记录的计数器值要大; 而在后一种情况中, consistency-test.rb 记录的计数器值将比集群记录的计数器值要小。
安装ruby
[root@redis06 etc]# yum install -y ruby ruby-devel rubygems rpm-build
[root@redis06 etc]# ruby -v
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]
报错,需要安装高版本ruby
[root@redis06 etc]# gem install redis Fetching: redis-4.1.3.gem (100%) ERROR: Error installing redis: redis requires Ruby version >= 2.3.0.
使用rvm安装ruby
[root@redis01 etc]# curl -sSL https://get.rvm.io | bash -s stable curl: (35) TCP connection reset by peer [root@redis01 etc]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB gpg: directory `/root/.gnupg' created gpg: new configuration file `/root/.gnupg/gpg.conf' created gpg: WARNING: options in `/root/.gnupg/gpg.conf' are not yet active during this run gpg: keyring `/root/.gnupg/secring.gpg' created gpg: keyring `/root/.gnupg/pubring.gpg' created gpg: requesting key D39DC0E3 from hkp server keys.gnupg.net gpg: requesting key 39499BDB from hkp server keys.gnupg.net gpg: /root/.gnupg/trustdb.gpg: trustdb created gpg: key D39DC0E3: public key "Michal Papis (RVM signing) <mpapis@gmail.com>" imported gpg: key 39499BDB: public key "Piotr Kuczynski <piotr.kuczynski@gmail.com>" imported gpg: no ultimately trusted keys found gpg: Total number processed: 2 gpg: imported: 2 (RSA: 2) [root@redis01 etc]# curl -sSL https://get.rvm.io | bash -s stable Downloading https://github.com/rvm/rvm/archive/1.29.9.tar.gz Downloading https://github.com/rvm/rvm/releases/download/1.29.9/1.29.9.tar.gz.asc gpg: Signature made Wed 10 Jul 2019 04:31:02 AM EDT using RSA key ID 39499BDB gpg: Good signature from "Piotr Kuczynski <piotr.kuczynski@gmail.com>" gpg: WARNING: This key is not certified with a trusted signature! gpg: There is no indication that the signature belongs to the owner. Primary key fingerprint: 7D2B AF1C F37B 13E2 069D 6956 105B D0E7 3949 9BDB GPG verified '/usr/local/rvm/archives/rvm-1.29.9.tgz' Creating group 'rvm' Installing RVM to /usr/local/rvm/ Installation of RVM in /usr/local/rvm/ is almost complete: * First you need to add all users that will be using rvm to 'rvm' group, and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`. * To start using RVM you need to run `source /etc/profile.d/rvm.sh` in all your open shell windows, in rare cases you need to reopen all shell windows. * Please do NOT forget to add your users to the rvm group. The installer no longer auto-adds root or users to the rvm group. Admins must do this. Also, please note that group memberships are ONLY evaluated at login time. This means that users must log out then back in before group membership takes effect! Thanks for installing RVM ???? Please consider donating to our open collective to help us maintain RVM. ???? Donate: https://opencollective.com/rvm/donate
读取环境变量
[root@redis06 etc]# source /etc/profile.d/rvm.sh
安装一个2.3以上的版本
[root@redis06 etc]# rvm install 2.6.3
指定默认版本
[root@redis06 etc]# rvm use 2.6.3 --default Using /usr/local/rvm/gems/ruby-2.6.3
[root@redis06 redis-rb-cluster-master]# gem install redis
Fetching redis-4.1.3.gem
Successfully installed redis-4.1.3
Parsing documentation for redis-4.1.3
Installing ri documentation for redis-4.1.3
Done installing documentation for redis after 0 seconds
1 gem installed
解压软件包
[root@redis06 softwares]# unzip redis-rb-cluster-master.zip -d ../module/redis-5.0.0/ Archive: redis-rb-cluster-master.zip 9a5b39fec03fb16ebb9b1796334dd95fc034ac14 creating: ../module/redis-5.0.0/redis-rb-cluster-master/ inflating: ../module/redis-5.0.0/redis-rb-cluster-master/README.md inflating: ../module/redis-5.0.0/redis-rb-cluster-master/cluster.rb inflating: ../module/redis-5.0.0/redis-rb-cluster-master/consistency-test.rb inflating: ../module/redis-5.0.0/redis-rb-cluster-master/crc16.rb inflating: ../module/redis-5.0.0/redis-rb-cluster-master/example.rb [root@redis06 softwares]# cd ../module/redis-5.0.0/redis-rb-cluster-master/ [root@redis06 redis-rb-cluster-master]# ll total 32 -rw-rw-r-- 1 root root 10412 Oct 28 2015 cluster.rb -rw-rw-r-- 1 root root 4083 Oct 28 2015 consistency-test.rb -rw-rw-r-- 1 root root 4127 Oct 28 2015 crc16.rb -rw-rw-r-- 1 root root 688 Oct 28 2015 example.rb -rw-rw-r-- 1 root root 2031 Oct 28 2015 README.md
关闭redis集群认证,并重启集群
[root@redis06 redis-rb-cluster-master]# find / -name "redis.conf" |xargs sed -ri.bak 's@(^requirepass.*)@#&@' [root@redis06 redis-rb-cluster-master]# pkill redis [root@redis06 redis-rb-cluster-master]# redis-server ../../redis-5.0.0/etc/redis.conf [root@redis06 redis-rb-cluster-master]# ruby consistency-test.rb 10.0.0.72 6379 116 R (0 err) | 116 W (0 err) | 1034 R (0 err) | 1034 W (0 err) | 1948 R (0 err) | 1948 W (0 err) | 2785 R (0 err) | 2785 W (0 err) | 3622 R (0 err) | 3622 W (0 err) | 4443 R (0 err) | 4443 W (0 err) | 5253 R (0 err) | 5253 W (0 err) | 6095 R (0 err) | 6095 W (0 err) | 6918 R (0 err) | 6918 W (0 err) |
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:redis集群命令及常规操作 - Python技术站