Redis搭建高可用集群

2021/6/26 19:58:26

本文主要是介绍Redis搭建高可用集群,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

redis集群至少需要三个master节点,并且给每个master再搭建一个slave节点,总共6个redis节点,这里用1台机器部署6个redis实例,搭建集群的步骤如下:

一、在redis 目录 创建 redis-cluster 目录

cd /opt/redis-5.0.7;mkdir redis-cluster
cd redis-cluster
mkdir 8001 8002 8003 8004 8005 8006

复制 redis.conf配置文件到8001下
cp . 表示当前目录

cd 8001
cp ../../conf/redis.conf .

编辑 8001 目录的redis.conf 配置问题

# 后台运行
daemonize yes

# 端口
port 8001

#pidfile pid
pidfile /var/run/redis_8001.pid 

# 数据存储位置 dir
dir /opt/redis-5.0.7/redis-cluster/8001/

#开启集群模式 cluster-enabled yes
cluster-enabled yes

# 集群配置信息cluster-config-file nodes-8001.conf
cluster-config-file nodes-8001.conf

#设置节点之前通信超时 配置 cluster-node-timeout 10000
cluster-node-timeout 10000

# bind 127.0.0.1 这个 设置绑定本机IP 或者 设置成 0.0.0.0
bind 0.0.0.0

#关闭保护模式 protected-mode  no
protected-mode  no

# 设置持久化模式
appendonly yes

# redis 登录密码 和集群节点密码(此步骤可以不设置,安全起见还是设置为好)
requirepass admin21262
masterauth admin21262

redis 的集群配置已经设置完成
将 8001 的 redis.conf 复制 到 8002…8006 修改其中的端口即可
vim 批量修改命令(vim命令模式 下操作) 将8001 全部改成 8002

:%s/8001/8002/g

二、启动所有redis 实例
进redis的 bin 目录;依次启动 6个redis实例

./redis-server ../redis-cluster/8001/redis.conf

启动完成 检查 redis 服务状态

ps -ef|grep redis

三、创建集群

进入 redis bin 目录
使用 redis-cli 创建集群

./redis-cli -a admin21262--cluster create --cluster-replicas 1 192.168.217.128:8001 192.168.217.128:8002 192.168.217.128:8003 192.168.217.128:8004 192.168.217.128:8005 192.168.217.128:8006

在 Can I set the above configuration? (type ‘yes’ to accept): yes
输入 yes

 ./redis-cli -a root  --cluster create --cluster-replicas 1 192.168.217.128:8001 192.168.217.128:8002 192.168.217.128:8003 192.168.217.128:8004 192.168.217.128:8005 192.168.217.128:8006
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> 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.217.128:8005 to 192.168.217.128:8001
Adding replica 192.168.217.128:8006 to 192.168.217.128:8002
Adding replica 192.168.217.128:8004 to 192.168.217.128:8003
M: 02d47d69650337ec4a967f5311718939c65d61ee 192.168.217.128:8001
   slots:[0-5460] (5461 slots) master
M: 736fe6711ab1c1abfaace923fa553661bf956c37 192.168.217.128:8002
   slots:[5461-10922] (5462 slots) master
M: 160224afce6c868c653d8e26e0cc5bdaf8da17be 192.168.217.128:8003
   slots:[10923-16383] (5461 slots) master
S: 1e10e6ac6562918ee95480feb7a48e4679c3db62 192.168.217.128:8004
   replicates 160224afce6c868c653d8e26e0cc5bdaf8da17be
S: 7952c769a0adb08753554e8767fdd415ed7a4e43 192.168.217.128:8005
   replicates 02d47d69650337ec4a967f5311718939c65d61ee
S: 0f3e37ec085d26448898129af9388f1a74a2c19e 192.168.217.128:8006
   replicates 736fe6711ab1c1abfaace923fa553661bf956c37
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.217.128:8001)
M: 02d47d69650337ec4a967f5311718939c65d61ee 192.168.217.128:8001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 160224afce6c868c653d8e26e0cc5bdaf8da17be 192.168.217.128:8003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 0f3e37ec085d26448898129af9388f1a74a2c19e 192.168.217.128:8006
   slots: (0 slots) slave
   replicates 736fe6711ab1c1abfaace923fa553661bf956c37
S: 1e10e6ac6562918ee95480feb7a48e4679c3db62 192.168.217.128:8004
   slots: (0 slots) slave
   replicates 160224afce6c868c653d8e26e0cc5bdaf8da17be
M: 736fe6711ab1c1abfaace923fa553661bf956c37 192.168.217.128:8002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 7952c769a0adb08753554e8767fdd415ed7a4e43 192.168.217.128:8005
   slots: (0 slots) slave
   replicates 02d47d69650337ec4a967f5311718939c65d61ee
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.


这篇关于Redis搭建高可用集群的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程