Redis集群搭建

Redis集群搭建简介

ubuntu20.04 , Redis5.0.7
建立一个三主三从的简单集群。
因本人只有一个电脑,所以就使用一个ip,多个端口的模式建立。
(集群搭建最少需要6个)

第一步:

cd /etc/redis/
mkdir RedisCluster
cd RedisCluster

第二步:

sudo vim 7000.conf

# 修改对应端口号
port 7000
# 绑定本机ip
bind 192.168.1.13
# 修改为为后台启动
daemonize yes

pidfile 7000.pid
# 开启集群
cluster-enabled yes
# 集群配置信息,开启集群后自动生成
cluster-config-file 7000_node.conf
# 请求超时时长
cluster-node-timeout 15000
# 开启日志后,它会每次写操作记录一条日志
appendonly yes

使用同样的方式建立出 7001.conf ,7002.conf ,7003.conf ,7004.conf ,7005.conf 文件。

第二步:

sudo apt-get install ruby
cd /etc/redis/RedisCluster
sudo gem install redis

第三步:

sudo redis-server 7000.conf
sudo redis-server 7001.conf
sudo redis-server 7002.conf
sudo redis-server 7003.conf
sudo redis-server 7004.conf
sudo redis-server 7005.conf

查看节点是否启动:

ps -aux | grep redis

第四步:

使用 redis-cli 设置集群

redis-cli --cluster  create  --cluster-replicas 1   192.168.1.13:7000 192.168.1.13:7001 192.168.1.13:7002 192.168.1.13:7003 192.168.1.13:7004 192.168.1.13:7005

成功标志:

>>> 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.1.13:7004 to 192.168.1.13:7000
Adding replica 192.168.1.13:7005 to 192.168.1.13:7001
Adding replica 192.168.1.13:7003 to 192.168.1.13:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 418498bb77ca6c47295ff2252a6402d4ed921dbc 192.168.1.13:7000
   slots:[0-5460] (5461 slots) master
M: 660792090b92267de99f1c6dba9a0d968d145004 192.168.1.13:7001
   slots:[5461-10922] (5462 slots) master
M: 70cb06fb19c8ba74864f338e6e1123adf56cf02a 192.168.1.13:7002
   slots:[10923-16383] (5461 slots) master
S: d0802e7f126d2ed6c963b128580c68920651a51b 192.168.1.13:7003
   replicates 70cb06fb19c8ba74864f338e6e1123adf56cf02a
S: 04c222405019d1638d4f9809b9e2d2323a9e17eb 192.168.1.13:7004
   replicates 418498bb77ca6c47295ff2252a6402d4ed921dbc
S: 5134cdd4015227ab0a90c0fa43f38abd46e8b093 192.168.1.13:7005
   replicates 660792090b92267de99f1c6dba9a0d968d145004
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.1.13:7000)
M: 418498bb77ca6c47295ff2252a6402d4ed921dbc 192.168.1.13:7000
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 660792090b92267de99f1c6dba9a0d968d145004 192.168.1.13:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: d0802e7f126d2ed6c963b128580c68920651a51b 192.168.1.13:7003
   slots: (0 slots) slave
   replicates 70cb06fb19c8ba74864f338e6e1123adf56cf02a
S: 5134cdd4015227ab0a90c0fa43f38abd46e8b093 192.168.1.13:7005
   slots: (0 slots) slave
   replicates 660792090b92267de99f1c6dba9a0d968d145004
S: 04c222405019d1638d4f9809b9e2d2323a9e17eb 192.168.1.13:7004
   slots: (0 slots) slave
   replicates 418498bb77ca6c47295ff2252a6402d4ed921dbc
M: 70cb06fb19c8ba74864f338e6e1123adf56cf02a 192.168.1.13:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

第五步:

测试:

终端命令:
	redis-cli -h 192.168.1.13 -p 7000 -c
测试效果:
192.168.1.13:7000> set name zhangsan001
-> Redirected to slot [5798] located at 192.168.1.13:7001
OK
192.168.1.13:7001> get name
"zhangsan001"
192.168.1.13:7001> exit
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值