Redis Cluster管理

author:JevonWei
版权声明:原创作品


  • Redis 3开始支持了Cluster模式,增强了Redis的水平扩展能力,Redis Cluster的节点分片通过hash slot实现,每个节点上的键都属于16384(0~16383)个slots中的一个,每个节点负责处理一部分slots。 Redis Cluster采用无中心节点方式实现,无需proxy代理,客户端直接与redis集群的每个节点连接,根据同样的hash算法计算出key对应的slot,然后直接在slot对应的Redis上执行命令。

  • Redis集群配置参考:http://blog.frognew.com/2017/03/redis-3.2-cluster-install-and-test.html

集群相关的配置:

cluster-enabled 是否开启集群配置
cluster-config-file 集群节点集群信息配置文件,每个节点都有一个,由redis生成和更新,配置时避免名称冲突
cluster-node-timeout 集群节点互连超时的阀值,单位毫秒
cluster-slave-validity-factor 进行故障转移时,salve会 申请成为master。有时slave会和master失联很久导致数据较旧,这样的slave不应该成为master。这个配置用来判断slave是否和master失联时间过长。

拓扑环境

Redis1	172.16.252.82
Redis2  172.16.252.184
Redis3  172.16.252.67

各节点安装redis

[root@Redis1  ~]# yum -y install redis
[root@Redis1  ~]# iptables -F
[root@Redis1  ~]# setenforce 0

Redis集群架构

Redis1

[root@Redis1 ~]# vim /etc/redis.conf 
bind 172.16.252.82
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
[root@Redis1 ~]# systemctl start redis

Redis2

[root@Redis2 ~]# vim /etc/redis.conf 
bind 172.16.252.184
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
[root@Redis2 ~]# systemctl start redis

Redis3

[root@Redis3 ~]# vim /etc/redis.conf 
bind 172.16.252.67
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-slave-validity-factor 10
[root@Redis3 ~]# systemctl start redis

Redis1

使用cluster meet添加主节点:
[root@Redis1 ~]# redis-cli -h 172.16.252.82 
172.16.252.82:6379> CLUSTER MEET 172.16.252.184 6379
OK
172.16.252.82:6379> CLUSTER MEET 172.16.252.67 6379
OK

查看集群状态
[root@Redis1 ~]# redis-cli -h 172.16.252.82
172.16.252.82:6379> CLUSTER INFO
cluster_state:fail  		#集群状态
cluster_slots_assigned:0	#分配的slot数
cluster_slots_ok:0			#正确的slot数
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1		 #当前的节点数
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0

上面显示cluster的状态是fail,是由于没有分配slot,需要把16384分配到3个Node上,编写addslots脚本:

addslots.sh
#!/bin/bash
for ((i=$2;i<=$3;i++))
do
	/usr/local/redis/bin/redis-cli -h $1 -p 6379 CLUSTER ADDSLOTS $i
done

分别在Redis1,Redis2,Redis3三个主机上执行:

./addslots.sh 172.16.252.82 0 5461
./addslots.sh 172.16.252.184 5462 10922
./addslots.sh 172.16.252.67 10923 16383

Redis1

查看集群状态
172.16.252.82:6379> CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:3
cluster_size:3
cluster_current_epoch:2
cluster_my_epoch:1
cluster_stats_messages_sent:1102
cluster_stats_messages_received:327

查看集群中的节点信息
172.16.252.82:6379> CLUSTER NODES
73535560317156a7a09cbe755beaefa0d0dd9c83 172.16.252.82:6379 myself,master - 0 0 1 connected 0-5641
52bb8acf89c6b15c4ae702452b855cea93b94729 172.16.252.67:6379 master - 0 1505562458095 2 connected 10922-16383
8f60e94a47da98286795b2fbbcae1b3aea75e123 172.16.252.184:6379  master - 0 1488959933751 0 connected 5642-10922
posted @ 2017-09-16 20:32  JevonWei  阅读(243)  评论(0编辑  收藏  举报