Redis5.X自动搭建集群超详细
Posted OkidoGreen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis5.X自动搭建集群超详细相关的知识,希望对你有一定的参考价值。
一、说明
1、Redis 3.0 版本之后官方发布了一个集群管理工具 redis-trib.rb,集成在 Redis 源码包的src
目录下。其封装了 Redis 提供的集群命令,使用简单、便捷。不过 redis-trib.rb 是 Redis 作者使用 Ruby 语言开发的,故使用该工具之前还需要先在机器上安装 Ruby 环境。后面作者可能意识到这个问题,Redis 5.0 版本开始便把这个工具集成到 redis-cli 中,以--cluster
参数提供使用,其中create
命令可以用来创建集群;
2、此处以Redis5.0.5稳定版为例进行redis集群搭建,即使用官方提供的集群管理工具快速搭建;
二、自动搭建集群
1、创建放置配置文件的文件夹
[root@master softWare]# cd redis-cluster/
[root@master redis-cluster]# mkdir -p redis01 redis02 redis03 redis04 redis05 redis06
[root@master redis-cluster]# pwd
/opt/softWare/redis-cluster
[root@master redis-cluster]# ls
redis01 redis02 redis03 redis04 redis05 redis06
2、将配置文件拷入redis01并修改配置文件
[root@master redis-5.0.5]# cp -r redis.conf /opt/softWare/redis-cluster/redis01/
#端口号
port 7001
#开启后台运行
daemonize yes
#设置 Redis 实例 pid 文件
pidfile /var/run/redis_7001.pid
#绑定本机IP
bind 192.168.230.21
#设置密码
requirepass 123456
#节点内部互相访问的密码,如果节点设置了密码,这项一定要配,否则不会主从复制和宕机选举
masterauth 123456
#启用集群莫斯
cluster-enabled yes
#设置当前节点集群配置文件路径
cluster-config-file nodes-7001.conf
#设置当前节点连接超时毫秒数
cluster-node-timeout 15000
3、将此配置文件分别 复制到另外五个,并相应配置
[root@master redis01]# cp -r redis.conf ../redis02/
[root@master redis01]# cp -r redis.conf ../redis03/
[root@master redis01]# cp -r redis.conf ../redis04/
[root@master redis01]# cp -r redis.conf ../redis05/
[root@master redis01]# cp -r redis.conf ../redis06/
......
4、分别启动这六个节点
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis01/redis.conf
9138:C 31 Mar 2020 11:17:13.864 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9138:C 31 Mar 2020 11:17:13.864 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=9138, just started
9138:C 31 Mar 2020 11:17:13.864 # Configuration loaded
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis02/redis.conf
9143:C 31 Mar 2020 11:17:19.821 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9143:C 31 Mar 2020 11:17:19.821 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=9143, just started
9143:C 31 Mar 2020 11:17:19.821 # Configuration loaded
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis03/redis.conf
9148:C 31 Mar 2020 11:17:23.999 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9148:C 31 Mar 2020 11:17:23.999 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=9148, just started
9148:C 31 Mar 2020 11:17:23.999 # Configuration loaded
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis04/redis.conf
9161:C 31 Mar 2020 11:17:34.234 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9161:C 31 Mar 2020 11:17:34.234 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=9161, just started
9161:C 31 Mar 2020 11:17:34.234 # Configuration loaded
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis05/redis.conf
9166:C 31 Mar 2020 11:17:38.080 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9166:C 31 Mar 2020 11:17:38.080 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=9166, just started
9166:C 31 Mar 2020 11:17:38.080 # Configuration loaded
[root@master bin]# ./redis-server /opt/softWare/redis-cluster/redis06/redis.conf
9171:C 31 Mar 2020 11:17:42.510 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
9171:C 31 Mar 2020 11:17:42.510 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=9171, just started
9171:C 31 Mar 2020 11:17:42.510 # Configuration loaded
[root@master bin]# ps -ef | grep redis
root 9139 1 0 11:17 ? 00:00:00 ./redis-server 192.168.230.21:7001 [cluster]
root 9144 1 0 11:17 ? 00:00:00 ./redis-server 192.168.230.21:7002 [cluster]
root 9149 1 0 11:17 ? 00:00:00 ./redis-server 192.168.230.21:7003 [cluster]
root 9162 1 0 11:17 ? 00:00:00 ./redis-server 192.168.230.21:8001 [cluster]
root 9167 1 0 11:17 ? 00:00:00 ./redis-server 192.168.230.21:8002 [cluster]
root 9172 1 0 11:17 ? 00:00:00 ./redis-server 192.168.230.21:8003 [cluster]
root 9177 2683 0 11:17 pts/1 00:00:00 grep --color=auto redis
5、自动方式搭建集群
[root@master bin]# ./redis-cli --cluster create 192.168.230.21:7001 192.168.230.21:7002 192.168.230.21:7003 192.168.230.21:8001 192.168.230.21:8002 192.168.230.21:8003 --cluster-replicas 1 -a 123456
说明:主节点在前,从节点在后。其中--cluster-replicas参数用来指定一个主节点带有的从节点个数,如上--cluster-replicas 1即表示 1 个主节点有 1 个从节点。
[root@master bin]# ./redis-cli --cluster create 192.168.230.21:7001 192.168.230.21:7002 192.168.230.21:7003 192.168.230.21:8001 192.168.230.21:8002 192.168.230.21:8003 --cluster-replicas 1 -a 123456
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.230.21:8002 to 192.168.230.21:7001
Adding replica 192.168.230.21:8003 to 192.168.230.21:7002
Adding replica 192.168.230.21:8001 to 192.168.230.21:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: fd74c9b036320a92d7d812fcbce982cc0277d170 192.168.230.21:7001
slots:[0-5460] (5461 slots) master
M: e1b9beb8b7f6f09c81249350b668ffcd0b9985ee 192.168.230.21:7002
slots:[5461-10922] (5462 slots) master
M: 13021a9468e8de4dcdb6db8fbe2ec4af0ba7bd27 192.168.230.21:7003
slots:[10923-16383] (5461 slots) master
S: 7415b3691dbdf221d8745561cc50a2130f0776d3 192.168.230.21:8001
replicates fd74c9b036320a92d7d812fcbce982cc0277d170
S: 86ccb7a8779aaa4bda409824cfd769c228fb9caa 192.168.230.21:8002
replicates e1b9beb8b7f6f09c81249350b668ffcd0b9985ee
S: 4d40ef295bb5f47a0a02134019046845a995cf39 192.168.230.21:8003
replicates 13021a9468e8de4dcdb6db8fbe2ec4af0ba7bd27
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.230.21:7001)
M: fd74c9b036320a92d7d812fcbce982cc0277d170 192.168.230.21:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 13021a9468e8de4dcdb6db8fbe2ec4af0ba7bd27 192.168.230.21:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 7415b3691dbdf221d8745561cc50a2130f0776d3 192.168.230.21:8001
slots: (0 slots) slave
replicates fd74c9b036320a92d7d812fcbce982cc0277d170
M: e1b9beb8b7f6f09c81249350b668ffcd0b9985ee 192.168.230.21:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 4d40ef295bb5f47a0a02134019046845a995cf39 192.168.230.21:8003
slots: (0 slots) slave
replicates 13021a9468e8de4dcdb6db8fbe2ec4af0ba7bd27
S: 86ccb7a8779aaa4bda409824cfd769c228fb9caa 192.168.230.21:8002
slots: (0 slots) slave
replicates e1b9beb8b7f6f09c81249350b668ffcd0b9985ee
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@master bin]#
192.168.230.21:7002> cluster nodes
e1b9beb8b7f6f09c81249350b668ffcd0b9985ee 192.168.230.21:7002@17002 myself,master - 0 1585625394000 2 connected 5461-10922
86ccb7a8779aaa4bda409824cfd769c228fb9caa 192.168.230.21:8002@18002 slave e1b9beb8b7f6f09c81249350b668ffcd0b9985ee 0 1585625396000 5 connected
fd74c9b036320a92d7d812fcbce982cc0277d170 192.168.230.21:7001@17001 master - 0 1585625396322 1 connected 0-5460
13021a9468e8de4dcdb6db8fbe2ec4af0ba7bd27 192.168.230.21:7003@17003 master - 0 1585625395000 3 connected 10923-16383
7415b3691dbdf221d8745561cc50a2130f0776d3 192.168.230.21:8001@18001 slave fd74c9b036320a92d7d812fcbce982cc0277d170 0 1585625396000 4 connected
4d40ef295bb5f47a0a02134019046845a995cf39 192.168.230.21:8003@18003 slave 13021a9468e8de4dcdb6db8fbe2ec4af0ba7bd27 0 1585625397331 6 connected
6、测试
[root@master bin]# ./redis-cli -p 7001 -a 123456 -h 192.168.230.21 -a 123456 -c
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.230.21:7001> set aa AA
OK
192.168.230.21:7001> set str aa
-> Redirected to slot [6928] located at 192.168.230.21:7002
OK
《新程序员》:云原生和全面数字化实践
50位技术专家共同创作,文字、视频、音频交互阅读
以上是关于Redis5.X自动搭建集群超详细的主要内容,如果未能解决你的问题,请参考以下文章