手动搭建集群便于理解集群创建的流程和细节,不过手动搭建集群需要很多步骤,当集群节点众多时,必然会加大搭建集群的复杂度和运维成本,因此官方提供了 redis-trib.rb 的工具方便我们快速搭建集群。
redis-trib.rb是采用 Ruby 实现的 redis 集群管理工具,内部通过 Cluster相关命令帮我们简化集群创建、检查、槽迁移和均衡等常见运维操作,使用前要安装 ruby 依赖环境。
redis-trib.rb无法实现所有节点都交叉复制,总会有一个节点不交叉,因此在安装完cluster以后,需要手动调整交叉。
环境准备
安装ruby环境
只在使用redis-trib的机器上安装即可
//安装ruby管理工具
[root@redis-1 ~]# yum -y install rubygems
//移除官网源
[root@redis-1 ~]# gem sources --remove https://rubygems.org/
https://rubygems.org/ removed from sources
//增加阿里云源
[root@redis-1 ~]# gem sources -a http://mirrors.aliyun.com/rubygems/
http://mirrors.aliyun.com/rubygems/ added to sources
//更新缓存
[root@redis-1 ~]# gem update --system
ruby2.3.0以下版本执行会报错
//安装ruby支持redis的插件
[root@redis-1 ~]# gem install redis -v 3.3.5
Fetching: redis-3.3.5.gem (100%)
Successfully installed redis-3.3.5
Parsing documentation for redis-3.3.5
Installing ri documentation for redis-3.3.5
1 gem installed
使用redis-trib自动部署cluster集群
所有节点安装redis
#创建部署路径
mkdir -p /data/redis_cluster/redis_{6380,6381}/{conf,data,logs,pid}
#准备配置文件
cat > /data/redis_cluster/redis_6380/conf/redis_6380.conf CLUSTER REPLICATE 812ae8ccf55d8994f1f9d30a20f6cff42fb24b4a
OK
[root@redis-3 ~]# redis-cli -h 192.168.81.230 -p 6381
192.168.81.230:6381> CLUSTER REPLICATE b61b8d0421b94b9de7267dda6c6f401a42622047
OK
#查看集群信息已经交叉复制
[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-16383
bdd20b03b573b2def6a9ee5053a75867709fc908 192.168.81.210:6381 myself,slave ce75dacf45d3ad4f852b7fb4d359a295b8a2bcdd 0 0 4 connected
9b7641253ea66073d865accdd4460d2877f9ff5d 192.168.81.220:6381 slave 812ae8ccf55d8994f1f9d30a20f6cff42fb24b4a 0 1612342767669 5 connected
ce75dacf45d3ad4f852b7fb4d359a295b8a2bcdd 192.168.81.220:6380 master - 0 1612342766658 2 connected 5461-10922
904a0109976cae38e5a3059fd70ce2727a0ed8fb 192.168.81.230:6381 slave b61b8d0421b94b9de7267dda6c6f401a42622047 0 1612342769686 3 connected
b61b8d0421b94b9de7267dda6c6f401a42622047 192.168.81.210:6380 master - 0 1612342770189 1 connected 0-5460
查看集群完整性
如果集群没问题会输出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
验证hash分配是否均匀
#首先插入1000条数据
[root@redis-1 ~]# for i in {1..1000}
do
redis-cli -c -h 192.168.81.210 -p 6380 set key_${i} value_${i}
done
#查看每个节点的数据量
[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
查看集群分配的误差值
[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.
链接:https://blog.csdn.net/weixin_44953658/article/details/121265752