Redis集群(3.X后原生集群)实践

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Redis安装

Centos7 yum 安装的redis最新版本

启动redis服务

  • 这里启动6个redis服务,3主3从,如下分别为其创建conf配置文件,再一一启动

redis.conf文件夹内容如下

cluster-enabled yes #是否开启集群模式
port 700x
  • 然后分别创建conf文件
[root@iZuf64res1ona9ad2ddsejZ ~]# cd /root/
[root@iZuf64res1ona9ad2ddsejZ ~]# mkdir cluster-test
[root@iZuf64res1ona9ad2ddsejZ ~]# cd cluster-test/
[root@iZuf64res1ona9ad2ddsejZ cluster-test]# mkdir 7000
[root@iZuf64res1ona9ad2ddsejZ cluster-test]# mkdir 7001
[root@iZuf64res1ona9ad2ddsejZ cluster-test]# mkdir 7002
[root@iZuf64res1ona9ad2ddsejZ cluster-test]# mkdir 7003
[root@iZuf64res1ona9ad2ddsejZ cluster-test]# mkdir 7004
[root@iZuf64res1ona9ad2ddsejZ cluster-test]# mkdir 7005
[root@iZuf64res1ona9ad2ddsejZ cluster-test]# vim 7000/redis.conf
[root@iZuf64res1ona9ad2ddsejZ cluster-test]# vim 7001/redis.conf
[root@iZuf64res1ona9ad2ddsejZ cluster-test]# vim 7002/redis.conf
[root@iZuf64res1ona9ad2ddsejZ cluster-test]# vim 7003/redis.conf
[root@iZuf64res1ona9ad2ddsejZ cluster-test]# vim 7004/redis.conf
[root@iZuf64res1ona9ad2ddsejZ cluster-test]# vim 7005/redis.conf

打开后分别填入相应内容

  • 前台启动6个redis服务
[root@iZuf64res1ona9ad2ddsejZ ~]# cd /root/cluster-test/7000
[root@iZuf64res1ona9ad2ddsejZ 7000]# redis-server redis.conf
15423:C 02 Jan 2022 14:00:54.003 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
15423:C 02 Jan 2022 14:00:54.003 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=15423, just started
15423:C 02 Jan 2022 14:00:54.003 # Configuration loaded
15423:M 02 Jan 2022 14:00:54.004 * monotonic clock: POSIX clock_gettime
15423:M 02 Jan 2022 14:00:54.004 * No cluster configuration found, I'm 85a76e39ca29ab52c3cc0eded001acad020bc007
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 6.2.6 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                  
 (    '      ,       .-`  | `,    )     Running in cluster mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 7000
 |    `-._   `._    /     _.-'    |     PID: 15423
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           https://redis.io       
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

15423:M 02 Jan 2022 14:00:54.008 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
15423:M 02 Jan 2022 14:00:54.008 # Server initialized
15423:M 02 Jan 2022 14:00:54.008 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
15423:M 02 Jan 2022 14:00:54.008 * Loading RDB produced by version 6.2.6
15423:M 02 Jan 2022 14:00:54.008 * RDB age 43886 seconds
15423:M 02 Jan 2022 14:00:54.008 * RDB memory usage when created 0.81 Mb
15423:M 02 Jan 2022 14:00:54.008 # Done loading RDB, keys loaded: 2, keys expired: 0.
15423:M 02 Jan 2022 14:00:54.008 * DB loaded from disk: 0.000 seconds
15423:M 02 Jan 2022 14:00:54.008 # I have keys for unassigned slot 903. Taking responsibility for it.
15423:M 02 Jan 2022 14:00:54.008 # I have keys for unassigned slot 6918. Taking responsibility for it.
15423:M 02 Jan 2022 14:00:54.012 * Ready to accept connections

其余5个启动如上同理

  • 检测端口

其中7000-7005是redis的客户端端口,17000-17005是redis内部通信端口

[root@iZuf64res1ona9ad2ddsejZ cluster-test]# ss -tanl | grep 700
LISTEN     0      128          *:17004                    *:*                  
LISTEN     0      128          *:17005                    *:*                  
LISTEN     0      128          *:7000                     *:*                  
LISTEN     0      128          *:7001                     *:*                  
LISTEN     0      128          *:7002                     *:*                  
LISTEN     0      128          *:7003                     *:*                  
LISTEN     0      128          *:7004                     *:*                  
LISTEN     0      128          *:7005                     *:*                  
LISTEN     0      128          *:17000                    *:*                  
LISTEN     0      128          *:17001                    *:*                  
LISTEN     0      128          *:17002                    *:*                  
LISTEN     0      128          *:17003                    *:*                  
LISTEN     0      128       [::]:17004                 [::]:*                  
LISTEN     0      128       [::]:17005                 [::]:*                  
LISTEN     0      128       [::]:7000                  [::]:*                  
LISTEN     0      128       [::]:7001                  [::]:*                  
LISTEN     0      128       [::]:7002                  [::]:*                  
LISTEN     0      128       [::]:7003                  [::]:*                  
LISTEN     0      128       [::]:7004                  [::]:*                  
LISTEN     0      128       [::]:7005                  [::]:*                  
LISTEN     0      128       [::]:17000                 [::]:*                  
LISTEN     0      128       [::]:17001                 [::]:*                  
LISTEN     0      128       [::]:17002                 [::]:*                  
LISTEN     0      128       [::]:17003                 [::]:*                  
[root@iZuf64res1ona9ad2ddsejZ cluster-test]# 

创建集群

脚本的槽位通过redis-trib这个ruby脚本来分配,3个主实例,3个从实例

  • 安装依赖

由于 Redis 集群需要使用 ruby 命令,所以我们需要安装 ruby 和相关接口。

yum install centos-release-scl-rh
yum install rh-ruby24 -y
scl enable rh-ruby24 bash
yum install rubygems
gem install redis # 安装ruby操作redis模块
  • 分配槽位

使用如下命令, --replicas 1 表示每个主有一个从,如下后三个redis服务自动为从

/usr/local/redis-cluster/bin/redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005

Redis Cluster 在5.0之后取消了ruby脚本 redis-trib.rb的支持(手动命令行添加集群的方式不变),集合到redis-cli里,避免了再安装ruby的相关环境故可以参考

https://blog.csdn.net/weixin_45904404/article/details/107640193
https://www.cnblogs.com/zhoujinyi/p/11606935.html

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

执行命令后会问你是否同意如上的分配,即可以看到master的槽位分配情况,slaver的情况。

输入yes

[root@iZuf64res1ona9ad2ddsejZ /]# redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:7004 to 127.0.0.1:7000
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7003 to 127.0.0.1:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 85a76e39ca29ab52c3cc0eded001acad020bc007 127.0.0.1:7000
   slots:[0-5460],[6918] (5461 slots) master
M: c6ccc961b8db3011ad11052ba96b7a6093e5db58 127.0.0.1:7001
   slots:[5461-10922] (5462 slots) master
M: 54d435e747df905ef175eb8b18b15da446b2ef8d 127.0.0.1:7002
   slots:[10923-16383] (5461 slots) master
S: 032ff14ea3fe244f35bd80ae93a881ce39a3b38f 127.0.0.1:7003
   replicates 54d435e747df905ef175eb8b18b15da446b2ef8d
S: 46269c92bd6fec251e39e6a0ca7af76c9423cffe 127.0.0.1:7004
   replicates 85a76e39ca29ab52c3cc0eded001acad020bc007
S: 7e305c4beed5979384647abe8f0c35dd447903e9 127.0.0.1:7005
   replicates c6ccc961b8db3011ad11052ba96b7a6093e5db58
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 127.0.0.1:7000)
M: 85a76e39ca29ab52c3cc0eded001acad020bc007 127.0.0.1:7000
   slots:[903] (1 slots) master
   1 additional replica(s)
M: c6ccc961b8db3011ad11052ba96b7a6093e5db58 127.0.0.1:7001
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 7e305c4beed5979384647abe8f0c35dd447903e9 127.0.0.1:7005
   slots: (0 slots) slave
   replicates c6ccc961b8db3011ad11052ba96b7a6093e5db58
M: 54d435e747df905ef175eb8b18b15da446b2ef8d 127.0.0.1:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 032ff14ea3fe244f35bd80ae93a881ce39a3b38f 127.0.0.1:7003
   slots: (0 slots) slave
   replicates 54d435e747df905ef175eb8b18b15da446b2ef8d
S: 46269c92bd6fec251e39e6a0ca7af76c9423cffe 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 85a76e39ca29ab52c3cc0eded001acad020bc007
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[ERR] Not all 16384 slots are covered by nodes.

[root@iZuf64res1ona9ad2ddsejZ /]# 

测试

  • 验证主从关系

查看7002的主从关系,发现其从节点是slave0:ip=127.0.0.1,port=7003

[root@iZuf64res1ona9ad2ddsejZ bin]# redis-cli -p 7002
127.0.0.1:7002> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=7003,state=online,offset=882,lag=0
master_failover_state:no-failover
master_replid:a67a48275f960a09303d494e0159135cdba0e62d
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:882
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:882
  • 下线主进程

在7002的窗口停止前台的运行,在7003的窗口查看前台信息如下,发现一段时间失联后7003说:“我赢了,我是master”Failover election won: I'm the new master.

在这里插入图片描述

  • 上线7002进程

在线下了7002redis服务后,再次启动,查看其信息,发现他自动成为了7003的从节点,并不会竞争主节点。

[root@iZuf64res1ona9ad2ddsejZ bin]# redis-cli -p 7002
127.0.0.1:7002> info replication
# Replication
role:slave
master_host:127.0.0.1
master_port:7003
master_link_status:up
master_last_io_seconds_ago:10
master_sync_in_progress:0
slave_read_repl_offset:1050
slave_repl_offset:1050
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:44957f55f69275bd12b97dcf94bdacd391ca7f61
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:1050
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1023
repl_backlog_histlen:28
127.0.0.1:7002> 
  • 从节点写数据

在从节点7005写数据,会发现失败

[root@iZuf64res1ona9ad2ddsejZ bin]# redis-cli -p 7005
127.0.0.1:7005> set my 123
(error) READONLY You can't write against a read only replica.
127.0.0.1:7005> 
  • 主节点写数据,从节点读数据

主节点7001写数据发现demo写失败,因为不在此服务的槽里面

[root@iZuf64res1ona9ad2ddsejZ bin]# redis-cli -p 7001
127.0.0.1:7001> set my 123
OK
127.0.0.1:7001> set demo 123
(error) MOVED 903 127.0.0.1:7000

为了方便的写非此槽的,注意启动时连接集群启动 -c

[root@iZuf64res1ona9ad2ddsejZ bin]# redis-cli -p 7001 -c
127.0.0.1:7001> set demo 123
-> Redirected to slot [903] located at 127.0.0.1:7000
OK
127.0.0.1:7000> 

从节点读数据成功

[root@iZuf64res1ona9ad2ddsejZ bin]# redis-cli -p 7005 -c
127.0.0.1:7005> get demo
-> Redirected to slot [903] located at 127.0.0.1:7000
"123"
127.0.0.1:7000> 
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AlwaysDayOne

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值