步骤2:在所有节点上配置Redis实例

  1. 如何配置Redis集群节点

redis 配置文件: /etc/redis.conf

先对其进行备份。

# cp /etc/redis.conf /etc/redis.conf.orig
# vi /etc/redis.conf
  1. 监听地址
bind  10.42.0.247

保护模式

protected-mode no

监听端口,默认值为6379

port 6379
  1. 启用集群模式
cluster-enabled yes

指定集群的配置文件:/var/lib/redis/nodes-6379.conf

cluster-config-file nodes-6379.conf

设置实例不可用的最长时间(以毫秒为单位),以便将其视为故障状态。值15000等于15秒。

cluster-node-timeout 15000
  1. 在磁盘上启用Redis持久性
appendonly yes
  1. 在所有节点上重新启动Redis服务
# systemctl restart redis
  1. 每个群集节点现在都应具有一个ID。可以在/var/log/redis/redis.log的日志文件中进行检查。
# cat /var/log/redis/redis.log
  1. 配置防火墙
# firewall-cmd --zone=public --permanent --add-port=6379/tcp 
# firewall-cmd --zone=public --permanent --add-port=16379/tcp 
# firewall-cmd --reload

第三步:创建Redis集群

  1. 使用redis-cli创建集群

对于具有6个节点的设置,我们将有3个主节点和3个从节点。
--cluster-replicas 1表示创建一个副本

# redis-cli --cluster create 10.42.0.247:6379 10.42.0.197:6379 10.42.0.132:6379 10.42.0.200:6379 10.42.0.21:6379 10.42.0.34:6379 --cluster-replicas 1
  1. 在任意主机上运行以下命令,以列出所有集群节点。
# redis-cli -h 10.42.0.247 -p 6379 cluster nodes

步骤5:测试跨Redis集群的数据复制

  1. 如何验证群集数据复制。 在其中一个主节点上创建一个键和值,然后在所有群集节点中读取它
# redis-cli -c -h 10.42.0.247 -p 6379 set name 'Oir'
# redis-cli -c -h 10.42.0.247 -p 6379 get name
# redis-cli -c -h 10.42.0.21 -p 6379 get name
# redis-cli -c -h 10.42.0.132 -p 6379 get name
# redis-cli -c -h 10.42.0.200 -p 6379 get name
# redis-cli -c -h 10.42.0.197 -p 6379 get name
# redis-cli -c -h 10.42.0.34 -p 6379 get name
``

步骤4:测试Redis群集故障转移

  1. 查看主节点
# redis-cli -h 10.42.0.247 -p 6379 cluster nodes  | grep master

查看从节点

# redis-cli -h 10.42.0.247 -p 6379 cluster nodes  | grep slave
  1. 在一个主节点(例如10.42.0.197)上停止Redis服务,检查集群中的所有主节点。
# systemctl stop redis
# redis-cli -h 10.42.0.247 -p 6379 cluster nodes | grep master
  1. 在发生故障的节点上再次启动Redis服务,再次检查集群中的所有主服务器。
# systemctl start redis
# redis-cli -h 10.42.0.247 -p 6379 cluster nodes  | grep master

检查群集从服务器,确认原来发生故障的主服务器现在是否为从属服务器。

# redis-cli -h 10.42.0.247 -p 6379 cluster nodes  | grep slave
如何在CentOS 8中设置Redis集群

Redis Cluster是内置的Redis功能,它支持自动分片,复制和高可用性,该功能以前是使用Sentinels实现的。

根据Redis群集文档, “ 最小群集 ”要求至少包含3个主节点。

重要提示:Redis Cluster还具有一些局限性,例如,对NATted环境以及在Docker下重新映射IP地址或TCP端口的环境缺乏支持。此外,并非每个客户端库都支持它。

步骤1:在所有节点上安装Redis

  1. 使用DNF软件包管理器安装Redis模块
# dnf module install redis
  1. 启动Redis服务
# systemctl start redis
# systemctl enable redis
# systemctl status redis

测试环境

Redis 主1:10.42.0.247
Redis 主2:10.42.0.197
Redis 主3:10.42.0.132

Redis从1:10.42.0.200
Redis从2:10.42.0.21
Redis从3:10.42.0.34
日期:2019-04-29 03:17:14 来源:oir作者:oir