上一篇有介绍手动安装redis cluster传送门,很明显,手动创建redis cluster的过程比较繁杂,容易出错,因此redis官方开发了一个工具,可以快速搭建redis cluster。在5.0之前该工具是由ruby语言编写的redis-trib.rb,在使用前需要安装ruby语言环境。在5.0之后redis摒弃了该工具,将搭建集群的功能合并到了redis-cli上,进一步简化了搭建redis cluster的过程。
搭建环境是centos7,六台机器(三主三从),默认已经安装好了redis,若未安装好redis,可参考https://blog.csdn.net/u012062455/article/details/86677040
1.修改配置文件
在{redis_home}目录下创建conf和data两个目录,将{redis_home}下的redis.conf拷贝到{redis_home}/conf/下
[root@kafka31 redis]# cp redis.conf conf/redis_cluster_auto.conf
修改redis_cluster_auto.conf
删除绑定
bind 127.0.0.1
关闭保护模式
protected-mode no
端口改为6380
port 6380
开启后台启动
daemonize yes
修改pid文件名,避免和其它redis进程冲突,我本地开启了多个redis进程,如果不存在其它redis进程,默认即可
pidfile /var/run/redis_6380.pid
和进程pid同理
logfile "redis_6380.log"
dbfilename dump_6380.rdb
修改目录
dir "/opt/redis-5.0.3/data"
开启集群相关配置
cluster-enabled yes
cluster-config-file nodes-6380.conf
cluster-node-timeout 15000
2.启动所有节点
redis-server {redis_home}/conf/redis_cluster_auto.conf
3.使用redis-cli创建集群
redis-cli会按照给定的顺序设置主节点和从节点,比如下面31,32,33设置在前,所以会被定为主节点。
[root@kafka31 conf]# redis-cli --cluster create 192.168.0.31:6380 192.168.0.32:6380 192.168.0.33:6380 192.168.0.34:6380 192.168.0.35:6380 192.168.0.36:6380 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.0.34:6380 to 192.168.0.31:6380
Adding replica 192.168.0.35:6380 to 192.168.0.32:6380
Adding replica 192.168.0.36:6380 to 192.168.0.33:6380
M: 92fd7c2a7b7b8933d1019e72a852f621f6b4faff 192.168.0.31:6380
slots:[0-5460] (5461 slots) master
M: ed93d5ea74751d7124a2d5830ce0806a0c962d43 192.168.0.32:6380
slots:[5461-10922] (5462 slots) master
M: 3641ec8359d5400e5540d77600a2360de8ca367e 192.168.0.33:6380
slots:[10923-16383] (5461 slots) master
S: 7fbf45fdc4d0780074f8fe324aac28ae686eebaa 192.168.0.34:6380
replicates 92fd7c2a7b7b8933d1019e72a852f621f6b4faff
S: 3ba3e8323b7b637c958977335bf7f7213c009929 192.168.0.35:6380
replicates ed93d5ea74751d7124a2d5830ce0806a0c962d43
S: a5ac7dfedd81f2ecd9dd101ef8d9d4f70f895bbf 192.168.0.36:6380
replicates 3641ec8359d5400e5540d77600a2360de8ca367e
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
...
>>> Performing Cluster Check (using node 192.168.0.31:6380)
M: 92fd7c2a7b7b8933d1019e72a852f621f6b4faff 192.168.0.31:6380
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: ed93d5ea74751d7124a2d5830ce0806a0c962d43 192.168.0.32:6380
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 3ba3e8323b7b637c958977335bf7f7213c009929 192.168.0.35:6380
slots: (0 slots) slave
replicates ed93d5ea74751d7124a2d5830ce0806a0c962d43
S: 7fbf45fdc4d0780074f8fe324aac28ae686eebaa 192.168.0.34:6380
slots: (0 slots) slave
replicates 92fd7c2a7b7b8933d1019e72a852f621f6b4faff
M: 3641ec8359d5400e5540d77600a2360de8ca367e 192.168.0.33:6380
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: a5ac7dfedd81f2ecd9dd101ef8d9d4f70f895bbf 192.168.0.36:6380
slots: (0 slots) slave
replicates 3641ec8359d5400e5540d77600a2360de8ca367e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
4.集群完整性检查
集群完整性是指所有的槽都分配到存活的redis主节点上,只要16384个槽中有一个槽未被分配,则表示集群不完整
[root@kafka31 redis]# redis-cli --cluster check 127.0.0.1:6380
127.0.0.1:6380 (92fd7c2a...) -> 0 keys | 5461 slots | 1 slaves.
192.168.0.32:6380 (ed93d5ea...) -> 0 keys | 5462 slots | 1 slaves.
192.168.0.33:6380 (3641ec83...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 127.0.0.1:6380)
M: 92fd7c2a7b7b8933d1019e72a852f621f6b4faff 127.0.0.1:6380
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: ed93d5ea74751d7124a2d5830ce0806a0c962d43 192.168.0.32:6380
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 3ba3e8323b7b637c958977335bf7f7213c009929 192.168.0.35:6380
slots: (0 slots) slave
replicates ed93d5ea74751d7124a2d5830ce0806a0c962d43
S: 7fbf45fdc4d0780074f8fe324aac28ae686eebaa 192.168.0.34:6380
slots: (0 slots) slave
replicates 92fd7c2a7b7b8933d1019e72a852f621f6b4faff
M: 3641ec8359d5400e5540d77600a2360de8ca367e 192.168.0.33:6380
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: a5ac7dfedd81f2ecd9dd101ef8d9d4f70f895bbf 192.168.0.36:6380
slots: (0 slots) slave
replicates 3641ec8359d5400e5540d77600a2360de8ca367e
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
出现一下信息说明所有槽都分配到节点了
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
5.集群测试
在31上执行
[root@kafka31 redis]# redis-cli -p 6380
127.0.0.1:6380> set hello cluster
OK
127.0.0.1:6380> get hello
"cluster"
在32上执行,正常重定向,后面会介绍
[root@kafka32 opt]# redis-cli -p 6380
127.0.0.1:6380> get hello
(error) MOVED 866 192.168.0.31:6380
在从节点34上执行,由于从节点处于集群默认,同样会存在重定向
[root@localhost opt]# redis-cli -p 6380
127.0.0.1:6380> get hello
(error) MOVED 866 192.168.0.31:6380
将从节点设为只读默认,再执行
127.0.0.1:6380> readonly
OK
127.0.0.1:6380> get hello
"cluster"