直观获取redis cluster 主从关系

Posted knowledge-is-power

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了直观获取redis cluster 主从关系相关的知识,希望对你有一定的参考价值。

 

需求:还是redis-trib.rb脚本获取的信息不足或者太繁杂,这里给出更加直观的一种方法,

说明:已在4.x版本测试通过,3.x不可用。

 

原生的输出

[[email protected] redis]$ redis-trib.rb info 5.5.5.101:29001
5.5.5.101:29001 (1adfa7f3...) -> 2599 keys | 4596 slots | 1 slaves.
5.5.5.101:29004 (32fc101a...) -> 4181 keys | 3595 slots | 1 slaves.
5.5.5.102:29002 (76a8592d...) -> 5041 keys | 4096 slots | 1 slaves.
5.5.5.103:29003 (31ba5414...) -> 5011 keys | 4097 slots | 1 slaves.
[OK] 16832 keys in 4 masters.
1.03 keys per slot on average.
[[email protected] redis]$ redis-cli -h 5.5.5.101 -p 29001 -c -a abc123 cluster nodes
6178b88137d2dd621f5445f13faa9936b300333a 5.5.5.102:29004@39004 slave 32fc101a2920c98f279a31292ace9ff3aeaa35a3 0 1544694513000 16 connected
32fc101a2920c98f279a31292ace9ff3aeaa35a3 5.5.5.101:29004@39004 master - 0 1544694515000 16 connected 824-1382 2200-2499 3001 5462-6828 10923-12290
c27b170791473946085c85c6fa7c163b24efe6e0 5.5.5.102:29001@39001 slave 1adfa7f3e80c47d5bd3072e9d07c1b2bcc8ef4a1 0 1544694514000 24 connected
cb88f415e0dc61616e1ed861a00aa8416dd6dd3f 5.5.5.101:29003@39003 slave 31ba5414196da8b6ed8e0640472d3870d5f6f591 0 1544694515665 20 connected
76a8592df53de4d45fb66fa8e48ef9fb0991ae14 5.5.5.102:29002@39002 master - 0 1544694514000 27 connected 821-823 1383-2199 7647-10922
1adfa7f3e80c47d5bd3072e9d07c1b2bcc8ef4a1 5.5.5.101:29001@39001 myself,master - 0 1544694513000 24 connected 2500-3000 3002-3299 3301-5461 6829-7646 12291-13108
723bfff7241ab8abfc46eeea280d673f84311056 5.5.5.103:29002@39002 slave 76a8592df53de4d45fb66fa8e48ef9fb0991ae14 0 1544694514663 27 connected
31ba5414196da8b6ed8e0640472d3870d5f6f591 5.5.5.103:29003@39003 master - 0 1544694513656 20 connected 0-820 3300 13109-16383
[[email protected] redis]$ 

 

编写脚本直观获取

[[email protected] redis]$ cat get_master_slave.sh 
cmd="redis-cli -c -h $1 -p $2"
master_id=(`$cmd cluster nodes | grep master | awk {print $1}`)
master_ip_port=(`$cmd cluster nodes | grep master | awk $2 ~ /@.*/{gsub("@.*","",$2);print $2}`)

len=${#master_id[@]}
for ((i=0;i<=$len-1;i++));
do
echo -n "${master_ip_port[$i]} ->  "
echo `$cmd cluster slaves ${master_id[$i]} | awk $2 ~ /@.*/{gsub("@.*","",$2);print $2}` 
done
[[email protected] redis]$ 
[[email protected] redis]$ sh get_master_slave.sh 5.5.5.101 29001
5.5.5.101:29004 ->  5.5.5.102:29004
5.5.5.102:29002 ->  5.5.5.103:29002
5.5.5.101:29001 ->  5.5.5.102:29001
5.5.5.103:29003 ->  5.5.5.101:29003
[[email protected] redis]$ 

 

以上是关于直观获取redis cluster 主从关系的主要内容,如果未能解决你的问题,请参考以下文章

redis非关系型数据库—Redis高可用(主从复制哨兵Cluster)

redis非关系型数据库—Redis高可用(主从复制哨兵Cluster)

redis非关系型数据库—Redis高可用(主从复制哨兵Cluster)

「故障演练」 Redis Cluster集群,当master宕机,主从切换

故障演练 Redis Cluster集群,当master宕机,主从切换,客户端报错 timed out

Redis数据库——Redis集群模式(主从复制哨兵Cluster)