Redis集群搭建

集群主节点搭建

创建三个配置文件

vim redis6379.conf

内容如下

# 引入Redis的公共部分
include /lzc/app/redis-5.0.3/redis.conf
# 设置pid进程文件位置
pidfile /var/run/redis_6379.pid
# 设置端口号
port 6379
# 设置rdb文件名
dbfilename dump6379.rdb
# 设置Redis密码
requirepass 密码
# 设置主库密码
masterauth 主节点密码
# 开启集群模式
cluster-enabled yes
# 配置节点配置文件
cluster-config-file nodes-6379.conf
# 配置节点失联时间(超过指定毫秒,自动进行主从切换)
cluster-node-timeout 15000
# 配置日志文件
logfile './logs/6379.log'
vim redis6380.conf

内容如下

# 引入Redis的公共部分
include /lzc/app/redis-5.0.3/redis.conf
# 设置pid进程文件位置
pidfile /var/run/redis_6380.pid
# 设置端口号
port 6380
# 设置rdb文件名
dbfilename dump6380.rdb
# 设置Redis密码
requirepass 密码
# 设置主库密码
masterauth 主节点密码
# 开启集群模式
cluster-enabled yes
# 配置节点配置文件
cluster-config-file nodes-6380.conf
# 配置节点失联时间(超过指定毫秒,自动进行主从切换)
cluster-node-timeout 15000
# 配置日志文件
logfile './logs/6380.log'
vim redis6381.conf

内容如下

vim redis6381.conf
# 引入Redis的公共部分
include /lzc/app/redis-5.0.3/redis.conf
# 设置pid进程文件位置
pidfile /var/run/redis_6381.pid
# 设置端口号
port 6381
# 设置rdb文件名
dbfilename dump6381.rdb
# 设置Redis密码
requirepass 密码
# 设置主库密码
masterauth 主节点密码
# 开启集群模式
cluster-enabled yes
# 配置节点配置文件
cluster-config-file nodes-6381.conf
# 配置节点失联时间(超过指定毫秒,自动进行主从切换)
cluster-node-timeout 15000
# 配置日志文件
logfile './logs/6381.log'

启动三个Redis

src/redis-server ./redis6379.conf
src/redis-server ./redis6380.conf
src/redis-server ./redis6381.conf

使用命令进行合并创建集群

src/redis-cli --cluster create --cluster-replicas 0 -a 你的redis密码 xxx:6379 xxx:6380 xxx:6381

-replicas 0 表示创建集群的方式,以0个从机的方式创建集群(此处即为创建仅有三个主机的集群)。

-a参数填写redis服务器的密码(没有设置密码可以不写-a)

最后面跟的是服务器的ip和端口号。

命令执行后,运行结果如下:

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: 9c705291a6fd1414a097e489c47cbe19b581564f ip地址:6379
   slots:[0-5460] (5461 slots) master
M: 6966994211f4fa27931516f481886588b288071f ip地址:6380
   slots:[5461-10922] (5462 slots) master
M: b87165138e8126e0d97853cb973d0988c71ec5ea ip地址:6381
   slots:[10923-16383] (5461 slots) master
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 ip地址:6379)
M: 9c705291a6fd1414a097e489c47cbe19b581564f ip地址:6379
   slots:[0-5460] (5461 slots) master
M: b87165138e8126e0d97853cb973d0988c71ec5ea ip地址:6381
   slots:[10923-16383] (5461 slots) master
M: 6966994211f4fa27931516f481886588b288071f ip地址:6380
   slots:[5461-10922] (5462 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

上面会自动为每台机器分配卡槽

计算key输入哪个卡槽

cluster keyslot key

加入从节点

新建从节点

三个主库的端口分别为6379、6380、6381

再新建三个从库,端口分别为6382、6383、6384

vim redis6382.conf

内容如下

# 引入Redis的公共部分
include /lzc/app/redis-5.0.3/redis.conf
# 设置pid进程文件位置
pidfile /var/run/redis_6382.pid
# 设置端口号
port 6382
# 设置rdb文件名
dbfilename dump6382.rdb
# 设置Redis密码
requirepass 密码
# 设置主库密码
masterauth 主节点密码
# 开启集群模式
cluster-enabled yes
# 配置节点配置文件
cluster-config-file nodes-6382.conf
# 配置节点失联时间(超过指定毫秒,自动进行主从切换)
cluster-node-timeout 15000
# 配置日志文件
logfile './logs/6382.log'
vim redis6383.conf

内容如下

# 引入Redis的公共部分
include /lzc/app/redis-5.0.3/redis.conf
# 设置pid进程文件位置
pidfile /var/run/redis_6383.pid
# 设置端口号
port 6383
# 设置rdb文件名
dbfilename dump6383.rdb
# 设置Redis密码
requirepass 密码
# 设置主库密码
masterauth 主节点密码
# 开启集群模式
cluster-enabled yes
# 配置节点配置文件
cluster-config-file nodes-6383.conf
# 配置节点失联时间(超过指定毫秒,自动进行主从切换)
cluster-node-timeout 15000
# 配置日志文件
logfile './logs/6383.log'
vim redis6384.conf

内容如下

# 引入Redis的公共部分
include /lzc/app/redis-5.0.3/redis.conf
# 设置pid进程文件位置
pidfile /var/run/redis_6384.pid
# 设置端口号
port 6384
# 设置rdb文件名
dbfilename dump6384.rdb
# 设置Redis密码
requirepass 密码
# 设置主库密码
masterauth 主节点密码
# 开启集群模式
cluster-enabled yes
# 配置节点配置文件
cluster-config-file nodes-6384.conf
# 配置节点失联时间(超过指定毫秒,自动进行主从切换)
cluster-node-timeout 15000
# 配置日志文件
logfile './logs/6384.log'

然后启动三个从节点

从节点加入集群

增加三个从节点到集群中去,并设置相应的主机

redis-cli --cluster add-node xx.xx.xx.xx:6382 xx.xx.xx.xx:6379 --cluster-slave --cluster-master-id 主库id -a Redis密码

增加从节点6382到集群中去,并将其设置为6379主机的从机。

slave 表示此操作要添加从节点

cluster-master-id 标识主机的id:要添加到哪一个主节点,集群中主机的id可以通过cluster nodes查看

-a :如果设置了redis密码,需要-a参数并填入密码

以同样的方式将6383和6385加入到集群中

127.0.0.1:6383> cluster nodes
b87165138e8126e0d97853cb973d0988c71ec5ea xxx.xx.xx.xxx:6381@16381 master - 0 1686704586897 3 connected 10923-16383
6966994211f4fa27931516f481886588b288071f xxx.xx.xx.xxx:6380@16380 master - 0 1686704586000 2 connected 5461-10922
3044c700f6c06500ea5dc40730599004cb63799a xxx.xx.xx.xxx:6382@16382 slave 9c705291a6fd1414a097e489c47cbe19b581564f 0 1686704584893 1 connected
c573e0c9978511ac0b0150342cc5cad4beacc800 xxx.xx.xx.xxx:6384@16384 slave b87165138e8126e0d97853cb973d0988c71ec5ea 0 1686704587898 3 connected
0e2bbd1b5cfcaf5f6cfd3f722122f8bae6ba67ef xxx.xx.xx.xxx:6383@16383 myself,slave 6966994211f4fa27931516f481886588b288071f 0 1686704585000 0 connected
9c705291a6fd1414a097e489c47cbe19b581564f xxx.xx.xx.xxx:6379@16379 master - 0 1686704586000 1 connected 0-5460

问题

1.集群搭建一直处于等待

搭建Redis集群的过程中,执行到cluster create : … 的时候,发现程序在阻塞,
显示:Waiting for the cluster to join 的字样,然后就无休无尽的等待… 如下:

>>> 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
.............................................................................................................

开放Redis服务的两个TCP端口。譬如Redis客户端连接端口为6379,而Redis服务在集群中还有一个叫集群总线端口,其端口为客户端连接端口加上10000,即 6379 + 10000 = 16379。
所以开放每个集群节点的客户端端口和集群总线端口才能成功创建集群!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值