首页天道酬勤rediscluster集群原理,redis cluster原理

rediscluster集群原理,redis cluster原理

张世龙 05-05 19:20 18次浏览

使用redis工具自动部署集群并手动构建集群,有助于理解集群创建的流程和详细信息,但手动构建集群需要很多步骤,集群节点较多时,必然会带来集群构建的复杂性和运输

redis-trib.rb是一个利用Ruby实现的redis群集管理工具,内部通过与群集相关的命令简化了群集创建、检查、插槽迁移和平衡等常见运输任务。 使用前必须安装Ruby依赖关系

在redis-trib.rb中,并非所有节点都是交叉复制的,并且其中一个节点不会交叉,因此必须在安装群集后手动调整交叉

环境准备

IP主机名端口号节点192.168.81.210 redis-16380 master 192.168.81.210 redis-16381 slave-redis-2192.168.81.220 redis 0 redis-36381 slave-redis-11.ruby环境的安装只需安装在使用redis-trib的设备上即可

ruby管理工具[ root @ redis-1~] # yum-yinstallrubygems//删除官方网站源[ root @ redis-1~] # gem sources-- remove 3359 rr9 增加Alibaba云源[ root @ redis-1至] # gem sources-a http://mirrors.a liyun.com/ruby gems/3358 mirrorors gem更新- -在system ruby2.3. 0或更低版本中运行时报告错误//rubyredis兼容插件[ root @ redis-1~] # geminstallredis-v 3.3.3 successfullyinstalledredis-3.3.5 parsingdocumentationforredis-3.3.5 installingridocumentationforredis-3.3.51 gege 部署路径mkdir-p/data/redis _ cluster/redis _ { 6380,6381 }/{ conf,data,logs, pid}2.配置文件cat/data/redis _ cluster/redis _ 6380/conf/redis _ 6380.confeofbind $ (ifconfig|awk ' NR==数据/redis _ cluster/redis _ 6380/PID/redis _ 6380 redis _ cluster/redis _ 6380/数据集群- enabledyescluster-config-file node _ 6380.conf cluster-node-time out 110数据/redis _ cluster/redis \ awk'NR==2{print$2} ' ) PS redis _ 6381/logs/redis _ 6381.log pidfile/data/redis _ cluster/redis _ 65381 redis _ 6381.logdbfilenent数据集群- enabledyescluster-config-file node _ 6381.conf cluster-node-time out 15000 sasa

2.2 .使用redis-trib部署群集语法格式:/redis-trib.rb create

--replicas 每个主节点的副本数量(从库数量) cluster节点地址

create //创建

–replicas //指定主库的副本数量,也就是从库数量

使用redis-trib安装的cluster集群,总会有一个节点不是交叉复制的,需要手动调整,因为trib也是根据节点地址交叉对应,到了最后一个机器已经没有第二个可以与它交叉的机器,它只能和自己去复制

[root@redis-1 ~]# cd /data/redis_cluster/redis-3.2.9/src[root@redis-1 /data/redis_cluster/redis-3.2.9/src]# ./redis-trib.rb create --replicas 1 192.168.81.210:6380 192.168.81.220:6380 192.168.81.230:6380 192.168.81.210:6381 192.168.81.220:6381 192.168.81.230:6381安装完查看集群准备已经是可用的[root@redis-1 ~]# redis-cli -h 192.168.81.210 -p 6380 192.168.81.210:6380> CLUSTER infocluster_state:okcluster_slots_assigned:16384cluster_slots_ok:16384cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:6cluster_size:3cluster_current_epoch:6cluster_my_epoch:1cluster_stats_messages_sent:1618cluster_stats_messages_received:1618

2.3.手动调整三主三从交叉复制

由于只有redis-3的复制不是交叉的,如果直接让redis-3去交叉复制某一个节点,那么就没有节点去复制redis-3的6380了,因此我们要手动调整所有节点之间的交叉入职

举个例子:redis-3的6381要成为redis-1的6380的主库,需要去redis-3的6381redis交互式操作

1.获取主节点的信息[root@redis-1 ~]# redis-cli -h 192.168.81.210 -p 6381 cluster nodes | grep 6380 | awk '{print $1.$2}'812ae8ccf55d8994f1f9d30a20f6cff42fb24b4a192.168.81.230:6380ce75dacf45d3ad4f852b7fb4d359a295b8a2bcdd192.168.81.220:6380b61b8d0421b94b9de7267dda6c6f401a42622047192.168.81.210:63802.配置三主三从交叉复制[root@redis-1 ~]# redis-cli -h 192.168.81.210 -p 6381192.168.81.210:6381> CLUSTER REPLICATE ce75dacf45d3ad4f852b7fb4d359a295b8a2bcddOK[root@redis-2 ~]# redis-cli -h 192.168.81.220 -p 6381192.168.81.220:6381> CLUSTER REPLICATE 812ae8ccf55d8994f1f9d30a20f6cff42fb24b4aOK[root@redis-3 ~]# redis-cli -h 192.168.81.230 -p 6381192.168.81.230:6381> CLUSTER REPLICATE b61b8d0421b94b9de7267dda6c6f401a42622047OK3.查看集群信息已经交叉复制[root@redis-1 ~]# redis-cli -h 192.168.81.210 -p 6381 cluster nodes 812ae8ccf55d8994f1f9d30a20f6cff42fb24b4a 192.168.81.230:6380 master - 0 1612342768677 3 connected 10923-16383bdd20b03b573b2def6a9ee5053a75867709fc908 192.168.81.210:6381 myself,slave ce75dacf45d3ad4f852b7fb4d359a295b8a2bcdd 0 0 4 connected9b7641253ea66073d865accdd4460d2877f9ff5d 192.168.81.220:6381 slave 812ae8ccf55d8994f1f9d30a20f6cff42fb24b4a 0 1612342767669 5 connectedce75dacf45d3ad4f852b7fb4d359a295b8a2bcdd 192.168.81.220:6380 master - 0 1612342766658 2 connected 5461-10922904a0109976cae38e5a3059fd70ce2727a0ed8fb 192.168.81.230:6381 slave b61b8d0421b94b9de7267dda6c6f401a42622047 0 1612342769686 3 connectedb61b8d0421b94b9de7267dda6c6f401a42622047 192.168.81.210:6380 master - 0 1612342770189 1 connected 0-5460

2.4.查看集群完整性

如果集群没问题会输出ok

[root@redis-1 ~]# cd /data/redis_cluster/redis-3.2.9/src[root@redis-1 /data/redis_cluster/redis-3.2.9/src]# ./redis-trib.rb check 192.168.81.210:6380

2.5.验证hash分配是否均匀 1.首先插入1000条数据[root@redis-1 ~]# for i in {1..1000}doredis-cli -c -h 192.168.81.210 -p 6380 set key_${i} value_${i}done2.查看每个节点的数据量[root@redis-1 ~]# redis-cli -c -h 192.168.81.210 -p 6380 dbsize(integer) 334[root@redis-1 ~]# redis-cli -c -h 192.168.81.220 -p 6380 dbsize(integer) 336[root@redis-1 ~]# redis-cli -c -h 192.168.81.230 -p 6380 dbsize(integer) 330 2.6.查看集群分配的误差值 [root@redis-1 /data/redis_cluster/redis-3.2.9/src]# ./redis-trib.rb rebalance 192.168.81.210:6380>>> Performing Cluster Check (using node 192.168.81.210:6380)[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.*** No rebalancing needed! All nodes are within the 2.0% threshold.

parallels desktop 16,mac m1 docker