35°

Redis 5-新的集群安装方式-redis-cli --cluster create

概要

关于集群(Cluster)的原理等可以参考 Redis设计与实现-集群工作原理 ,在这里就不赘述了,自己在很久很久以前介绍了使用redis-trib工具集安装集群的方式 Redis集群搭建与使用方法 ,这两天看官方文档发现了新的安装部署方式,用博客记录下来

准备工作

准备了三台虚拟机,IP地址分别是:

192.168.10.23
192.168.10.24
192.168.10.25

每台机器分别启动三个RedisServer实例,它们的端口分别是7001至7009,按照如下配置修改各个实例的redis.conf配置文件

#端口
port 7001
#开启集群功能
cluster-enabled yes
#集群节点配置文件名,该文件不是人工编写的,由程序自动生成和修改
cluster-config-file cluster-nodes-7001.conf
#集群节点超时时间,配合cluster-replica-validity-factor使用
cluster-node-timeout 1000
#探测集群节点超时不可用的次数,假设cluster-node-timeout设置为1000毫秒,cluster-replica-validity-factor为5,那么1000*5之间内集群节点还不可用的话,会被标记为疑似下线
cluster-replica-validity-factor 5
#主节点下最少的从节点数
cluster-migration-barrier 1
#yes 要求所有主节点正常工作,且所有hash slots被分配到工作的主节点,集群才能提供服务,如果想一部分hash slots即可响应请求,则设置为no
cluster-require-full-coverage yes
#yes 禁止当主节点挂掉时,让从节点不能竞选为主节点
cluster-replica-no-failover no
#开启AOF持久化
appendonly yes
#设置RDB和AOF文件目录
dir /home/acct/data/redis/dbfile

记得在主机上将/home/acct/data/redis/dbfile与/home/acct/data/redis/config目录创建好,前者存放持久化文件,后者存放redis.conf

上传配置文件到各个主机的目录

启动实例

在三台机器上启动对应的Redis-Server

redis-server /home/acct/data/redis/config/redis-7001.conf
redis-server /home/acct/data/redis/config/redis-7002.conf
redis-server /home/acct/data/redis/config/redis-7003.conf

redis-server /home/acct/data/redis/config/redis-7004.conf
redis-server /home/acct/data/redis/config/redis-7005.conf
redis-server /home/acct/data/redis/config/redis-7006.conf

redis-server /home/acct/data/redis/config/redis-7007.conf
redis-server /home/acct/data/redis/config/redis-7008.conf
redis-server /home/acct/data/redis/config/redis-7009.conf

可以使用ps -ef|grep redis启动情况

配置集群

做这步操作时,一定要配置防火墙策略或者关闭防火墙,放开端口,而Redis集群是需要两个端口的,其中一个端口是redis.conf.port端口加10000,比如端口是7001,那么它还需要一个17001端口。根据自己的防火墙安全级别可能需要罗列很多条目,在这里就不给出了

我在自己的虚拟机上,直接暴力的将防火墙关闭了。

centos7.x:systemctl stop firewalld.service

 

:生产环境一定要和网络管理员沟通清楚了再决定,不要贸然给开通申请或者关闭防火墙

一条命令配置集群

Redis 5提供的redis-cli --cluster的命令可以一次性自动的解决集群搭建问题,甚至将hash slots都给你平均分好了,只要你敲yes即可

redis-cli --cluster create \
192.168.10.23:7001 \
192.168.10.23:7002 \
192.168.10.23:7003 \
192.168.10.24:7004 \
192.168.10.24:7005 \
192.168.10.24:7006 \
192.168.10.25:7007 \
192.168.10.25:7008 \
192.168.10.25:7009 \
--cluster-replicas 2

执行效果如下

[acct@centos-template /home/acct/data/redis]$ redis-cli --cluster create 192.168.10.23:7001 192.168.10.23:7002 192.168.10.23:7003 192.168.10.24:7004 192.168.10.24:7005 192.168.10.24:7006 192.168.10.25:7007 192.168.10.25:7008 192.168.10.25:7009 --cluster-replicas 2
>>> Performing hash slots allocation on 9 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.10.24:7005 to 192.168.10.23:7001
Adding replica 192.168.10.25:7008 to 192.168.10.23:7001
Adding replica 192.168.10.23:7002 to 192.168.10.24:7004
Adding replica 192.168.10.23:7003 to 192.168.10.24:7004
Adding replica 192.168.10.24:7006 to 192.168.10.25:7007
Adding replica 192.168.10.25:7009 to 192.168.10.25:7007
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves of the same master are in the same host
M: e730a96a3a6985594af909291a804cca2c0953b6 192.168.10.23:7001
   slots:[0-5460] (5461 slots) master
S: b80e23c0a80fe54ffaf934e1bb9a6c5927fdafe5 192.168.10.23:7002
   replicates 5113f201c95db8e342b954e72da89ceadbdf7d4f
S: a4b0199cf8a95e3cfe239db38c0ca20cb0636bfe 192.168.10.23:7003
   replicates 5113f201c95db8e342b954e72da89ceadbdf7d4f
M: 5113f201c95db8e342b954e72da89ceadbdf7d4f 192.168.10.24:7004
   slots:[5461-10922] (5462 slots) master
S: 3b4cce96f5412c0b6a9e304e326e11df7b7e8e36 192.168.10.24:7005
   replicates 74c3c0f445317d40cdead5305139be6693dcc83d
S: b8d787d6eca1a139d493e69489fa9c8224bbcbca 192.168.10.24:7006
   replicates 74c3c0f445317d40cdead5305139be6693dcc83d
M: 74c3c0f445317d40cdead5305139be6693dcc83d 192.168.10.25:7007
   slots:[10923-16383] (5461 slots) master
S: 9c73b7b1e09f04ccad6f3c09ac8030bd46143a22 192.168.10.25:7008
   replicates e730a96a3a6985594af909291a804cca2c0953b6
S: b10aa1db953e489173bb4af124a0d6beebfb86e8 192.168.10.25:7009
   replicates e730a96a3a6985594af909291a804cca2c0953b6
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.10.23:7001)
M: e730a96a3a6985594af909291a804cca2c0953b6 192.168.10.23:7001
   slots:[0-5460] (5461 slots) master
   2 additional replica(s)
S: b10aa1db953e489173bb4af124a0d6beebfb86e8 192.168.10.25:7009
   slots: (0 slots) slave
   replicates e730a96a3a6985594af909291a804cca2c0953b6
S: 9c73b7b1e09f04ccad6f3c09ac8030bd46143a22 192.168.10.25:7008
   slots: (0 slots) slave
   replicates e730a96a3a6985594af909291a804cca2c0953b6
S: 3b4cce96f5412c0b6a9e304e326e11df7b7e8e36 192.168.10.24:7005
   slots: (0 slots) slave
   replicates 74c3c0f445317d40cdead5305139be6693dcc83d
S: b8d787d6eca1a139d493e69489fa9c8224bbcbca 192.168.10.24:7006
   slots: (0 slots) slave
   replicates 74c3c0f445317d40cdead5305139be6693dcc83d
S: a4b0199cf8a95e3cfe239db38c0ca20cb0636bfe 192.168.10.23:7003
   slots: (0 slots) slave
   replicates 5113f201c95db8e342b954e72da89ceadbdf7d4f
S: b80e23c0a80fe54ffaf934e1bb9a6c5927fdafe5 192.168.10.23:7002
   slots: (0 slots) slave
   replicates 5113f201c95db8e342b954e72da89ceadbdf7d4f
M: 5113f201c95db8e342b954e72da89ceadbdf7d4f 192.168.10.24:7004
   slots:[5461-10922] (5462 slots) master
   2 additional replica(s)
M: 74c3c0f445317d40cdead5305139be6693dcc83d 192.168.10.25:7007
   slots:[10923-16383] (5461 slots) master
   2 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

查看集群状态

[acct@centos-template /home/acct/data/redis]$ redis-cli -c -p 7001 cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:9
cluster_size:3
cluster_current_epoch:9
cluster_my_epoch:1
cluster_stats_messages_ping_sent:119
cluster_stats_messages_pong_sent:114
cluster_stats_messages_sent:233
cluster_stats_messages_ping_received:106
cluster_stats_messages_pong_received:119
cluster_stats_messages_meet_received:8
cluster_stats_messages_received:233

测试

[acct@centos-template /home/acct/data/redis]$ redis-cli -c -p 7001
127.0.0.1:7001> set username Messi
-> Redirected to slot [14315] located at 192.168.10.25:7007
OK
192.168.10.25:7007> 

更多集群后期维护命令,在Redis官方文档中 https://redis.io/commands#cluster 查看。

本文由【特拉仔】发布于开源中国,原文链接:https://my.oschina.net/u/3049601/blog/3159593

全部评论: 0

    我有话说: