0
点赞
收藏
分享

微信扫一扫

Redis cluster

1、简述redis集群的实现原理

在哨兵sentinel机制中,可以解决redis的高可用问题,即当master故障后可以自动将slave提升为master,从而可以保证redis服务的正常运行,但是还是无法解决redis单机写入的瓶颈问题,即单机redis写入性能受限于单机的内存大小、并发数量、网卡速率等因素。
为了解决单机redis性能的瓶颈,提高redis的性能,可以使用分布式集群的解决方案。
在早期的Redis分布式集群部署方案:

  1. 客户端分区:由客户端程序决定key写分配和写入的redis node,但是需要客户端自己实现写入分配、高可用管理和故障转移等;
  2. 代理方案:基于三方软件实现redis proxy,客户端先连接之代理层,由代理层实现key的写入分配,对客户端来说是有比较简单,但是对于集群管节点增减相对比较麻烦,而且代理本身也是单点和性能瓶颈。

redis在3.0的版本后就推出了无中心架构的redis cluster机制,在无中心的redis集群当中,其每个节点保存当前节点数据和整个集群状态,每个节点都可以和其他节点连接。
Redis Cluster特点如下:

  1. 所有Redis节点使用(PING机制)互联
  2. 集群中某个节点的是否失效,是由整个集群中超过半数的节点监测都失效,才能算真正的失效
  3. 客户端不需要proxy即可直接连接redis,应用程序中需要配置有全部的redis服务器IP
  4. redis cluster把所有的redis node平均映射到 0-16383个槽位(slot)上,读写需要到指定的redis node上进行操作,因此有多少个redis node相当于redis并发扩展了多少倍,每个redis node承担16384/N个槽位
  5. Redis cluster预先分配16384个(slot)槽位,当需要在redis集群中写入一个key -value的时候,会使用CRC16(key) mod 16384之后的值,决定将key写入值哪一个槽位从而决定写入哪一个Redis节点上,从而有效解决单机瓶颈。

2、基于redis5的redis cluster部署

2.1、创建redis cluster集群的环境准备

  • 每个redis节点采用的redis版本要相同、相同的密码、硬件配置也最好是相同
  • 所有redis服务器必须没有任何数据
  • 这里我是准备了六台主机:
IP地址 redis版本 主机名
10.1.2.208 redis-6.2.7 node1.stars.org
10.1.2.209 redis-6.2.7 node2.stars.org
10.1.2.210 redis-6.2.7 node3.stars.org
10.1.2.213 redis-6.2.7 node4.stars.org
10.1.2.215 redis-6.2.7 node5.stars.org
10.1.2.217 redis-6.2.7 node6.stars.org

2.2、启用redis cluster配置

这里安装redis是使用的脚本一键安装的,脚本放在文章的最后了。

  • 每个节点修改redis配置,必须开启cluster功能的参数
    
    #手动修改配置文件需要修改的项
    bind 0.0.0.0
    masterauth wm521314   #建议配置,否则后期的master和slave主从复制无法成功,还需再配置
    requirepass wm521314
    cluster-enabled yes #取消此行注释,必须开启集群,开启后redis 进程会有cluster标识
    cluster-config-file nodes-6379.conf #取消此行注释,此为集群状态文件,记录主从关系及slot范围信息,由redis cluster集群自动创建和维护
    cluster-require-full-coverage no   #默认值为yes,设为no可以防止一个节点不可用导致整个cluster不可能

#在这里我就使用sed修改配置文件了
root@node1:~# sed -i.bak -e '/masterauth/a masterauth wm521314' -e '/# cluster-enabled yes/a cluster-enabled yes' -e '/# cluster-config-file nodes-6379.conf/a cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /apps/redis/etc/redis.conf
root@node1:~# systemctl restart redis


- 验证所有节点的Redis服务状态:
```bash
#开启了16379的cluster的端口,实际的端口=redis port + 10000
root@node1:~# ss -tnl
State                      Recv-Q                      Send-Q                                            Local Address:Port                                              Peer Address:Port                      
LISTEN                     0                           128                                                     0.0.0.0:34374                                                  0.0.0.0:*                         
LISTEN                     0                           128                                                     0.0.0.0:13450                                                  0.0.0.0:*                         
LISTEN                     0                           511                                                     0.0.0.0:6379                                                   0.0.0.0:*                         
LISTEN                     0                           64                                                      0.0.0.0:32654                                                  0.0.0.0:*                         
LISTEN                     0                           128                                                     0.0.0.0:111                                                    0.0.0.0:*                         
LISTEN                     0                           128                                                     0.0.0.0:51538                                                  0.0.0.0:*                         
LISTEN                     0                           128                                               127.0.0.53%lo:53                                                     0.0.0.0:*                         
LISTEN                     0                           128                                                     0.0.0.0:22                                                     0.0.0.0:*                         
LISTEN                     0                           128                                                   127.0.0.1:6010                                                   0.0.0.0:*                         
LISTEN                     0                           511                                                     0.0.0.0:16379                                                  0.0.0.0:*                         
LISTEN                     0                           64                                                      0.0.0.0:2049                                                   0.0.0.0:*                         
LISTEN                     0                           64                                                         [::]:10214                                                     [::]:*                         
LISTEN                     0                           128                                                        [::]:111                                                       [::]:*                         
LISTEN                     0                           128                                                        [::]:16208                                                     [::]:*                         
LISTEN                     0                           128                                                        [::]:22288                                                     [::]:*                         
LISTEN                     0                           128                                                        [::]:22                                                        [::]:*                         
LISTEN                     0                           128                                                        [::]:57046                                                     [::]:*                         
LISTEN                     0                           128                                                       [::1]:6010                                                      [::]:*                         
LISTEN                     0                           64                                                         [::]:2049                                                      [::]:*                         

#查看redis进程状态
root@node1:~# ps aux | grep redis
redis    22669  0.2  0.1  63004  5692 ?        Ssl  12:13   0:00 /apps/redis/bin/redis-server 0.0.0.0:6379 [cluster]
root     22677  0.0  0.0  14860  1088 pts/0    S+   12:15   0:00 grep --color=auto redis

2.3、创建集群

#命令redis-cli的选项 --cluster-replicas 1 表示每个master对应一个slave节点
root@node1:~# redis-cli -a wm521314 --cluster create 10.1.2.208:6379 10.1.2.209:6379 10.1.2.210:6379 10.1.2.213:6379 10.1.2.215:6379 10.1.2.217:6379 --cluster-replicas 1
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 10.1.2.215:6379 to 10.1.2.208:6379
Adding replica 10.1.2.217:6379 to 10.1.2.209:6379
Adding replica 10.1.2.213:6379 to 10.1.2.210:6379
M: 2d04605711615ba0acbf2a098c9c79c59b23f408 10.1.2.208:6379 #带M指的是master
   slots:[0-5460] (5461 slots) master   #当前master的槽位起始和结束位
M: 1e79791c833562821282d9aec31f4d116fc5e1c7 10.1.2.209:6379
   slots:[5461-10922] (5462 slots) master
M: bbc82d4f8a1c6c062a88711a32d1ac08dc59a647 10.1.2.210:6379
   slots:[10923-16383] (5461 slots) master
S: 285c027937cf19ed100efda1b5116c4bae04e50a 10.1.2.213:6379 #带S的slave
   replicates bbc82d4f8a1c6c062a88711a32d1ac08dc59a647
S: 8f463789ccd8638709df9611270b2c0cd83984d5 10.1.2.215:6379
   replicates 2d04605711615ba0acbf2a098c9c79c59b23f408
S: d1112ea7d3bed02b6b4e1d89aa19fd3a38568ffd 10.1.2.217:6379
   replicates 1e79791c833562821282d9aec31f4d116fc5e1c7
Can I set the above configuration? (type 'yes' to accept): yes  #输入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 10.1.2.208:6379)
M: 2d04605711615ba0acbf2a098c9c79c59b23f408 10.1.2.208:6379
   slots:[0-5460] (5461 slots) master   #已经分配的槽位
   1 additional replica(s)  #分配了一个slave
S: 8f463789ccd8638709df9611270b2c0cd83984d5 10.1.2.215:6379
   slots: (0 slots) slave   #slave没有分配槽位
   replicates 2d04605711615ba0acbf2a098c9c79c59b23f408  #对应的master的10.1.2.208的ID
S: d1112ea7d3bed02b6b4e1d89aa19fd3a38568ffd 10.1.2.217:6379
   slots: (0 slots) slave
   replicates 1e79791c833562821282d9aec31f4d116fc5e1c7  #对应的master的10.1.2.209的ID
S: 285c027937cf19ed100efda1b5116c4bae04e50a 10.1.2.213:6379
   slots: (0 slots) slave
   replicates bbc82d4f8a1c6c062a88711a32d1ac08dc59a647  #对应的master的10.1.2.210的ID
M: 1e79791c833562821282d9aec31f4d116fc5e1c7 10.1.2.209:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: bbc82d4f8a1c6c062a88711a32d1ac08dc59a647 10.1.2.210:6379
   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.   #所有槽位(16384个)分配完成

#从上面的结果,可以得到3组主从
master:10.1.2.208---slave:10.1.2.215
master:10.1.2.209---slave:10.1.2.217
master:10.1.2.210---slave:10.1.2.213

2.4、查看主从状态

#node1节点
root@node1:~# redis-cli -a wm521314 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=10.1.2.215,port=6379,state=online,offset=630,lag=0
master_failover_state:no-failover
master_replid:055976e21e5d2123da62fcc65b2d1ee5ac523545
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:630
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:630

#node2节点
root@node2:~# redis-cli -a wm521314 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=10.1.2.217,port=6379,state=online,offset=658,lag=0
master_failover_state:no-failover
master_replid:7ca9dafd1456cdb45fb7e25c7147bd1e89d36f01
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:658
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:658

#node3节点
root@node3:~# redis-cli -a wm521314 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=10.1.2.213,port=6379,state=online,offset=672,lag=1
master_failover_state:no-failover
master_replid:4d10bc7e1a4ae847d62a198d0b9eef1729a01af8
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:686
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:686

#node4节点
root@node4:~# redis-cli -a wm521314 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:10.1.2.210
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_read_repl_offset:798
slave_repl_offset:798
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:4d10bc7e1a4ae847d62a198d0b9eef1729a01af8
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:798
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:798

#node5节点
root@node5:~# redis-cli -a wm521314 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:10.1.2.208
master_port:6379
master_link_status:up
master_last_io_seconds_ago:6
master_sync_in_progress:0
slave_read_repl_offset:812
slave_repl_offset:812
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:055976e21e5d2123da62fcc65b2d1ee5ac523545
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:812
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:812

#node6节点
root@node6:~# redis-cli -a wm521314 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:10.1.2.209
master_port:6379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_read_repl_offset:840
slave_repl_offset:840
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:7ca9dafd1456cdb45fb7e25c7147bd1e89d36f01
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:840
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:840

#查看集群中几个节点的对应关系
root@node1:~# redis-cli -a wm521314 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
8f463789ccd8638709df9611270b2c0cd83984d5 10.1.2.215:6379@16379 slave 2d04605711615ba0acbf2a098c9c79c59b23f408 0 1653569184000 1 connected
d1112ea7d3bed02b6b4e1d89aa19fd3a38568ffd 10.1.2.217:6379@16379 slave 1e79791c833562821282d9aec31f4d116fc5e1c7 0 1653569183634 2 connected
2d04605711615ba0acbf2a098c9c79c59b23f408 10.1.2.208:6379@16379 myself,master - 0 1653569183000 1 connected 0-5460
285c027937cf19ed100efda1b5116c4bae04e50a 10.1.2.213:6379@16379 slave bbc82d4f8a1c6c062a88711a32d1ac08dc59a647 0 1653569185000 3 connected
1e79791c833562821282d9aec31f4d116fc5e1c7 10.1.2.209:6379@16379 master - 0 1653569185643 2 connected 5461-10922
bbc82d4f8a1c6c062a88711a32d1ac08dc59a647 10.1.2.210:6379@16379 master - 0 1653569184638 3 connected 10923-16383

2.5、验证集群的状态

root@node1:~# redis-cli -a wm521314 cluster info
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6   #集群中的节点数
cluster_size:3  #集群数量
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:779
cluster_stats_messages_pong_sent:786
cluster_stats_messages_sent:1565
cluster_stats_messages_ping_received:781
cluster_stats_messages_pong_received:779
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1565

#查看某个节点的集群状态
root@node1:~# redis-cli -a wm521314 --cluster info 10.1.2.209:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.1.2.209:6379 (1e79791c...) -> 0 keys | 5462 slots | 1 slaves.
10.1.2.210:6379 (bbc82d4f...) -> 0 keys | 5461 slots | 1 slaves.
10.1.2.208:6379 (2d046057...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.

#查看集群的状态文件
root@node1:~# cat /apps/redis/data/nodes-6379.conf
8f463789ccd8638709df9611270b2c0cd83984d5 10.1.2.215:6379@16379 slave 2d04605711615ba0acbf2a098c9c79c59b23f408 0 1653568467000 1 connected
d1112ea7d3bed02b6b4e1d89aa19fd3a38568ffd 10.1.2.217:6379@16379 slave 1e79791c833562821282d9aec31f4d116fc5e1c7 0 1653568468000 2 connected
2d04605711615ba0acbf2a098c9c79c59b23f408 10.1.2.208:6379@16379 myself,master - 0 1653568462000 1 connected 0-5460
285c027937cf19ed100efda1b5116c4bae04e50a 10.1.2.213:6379@16379 slave bbc82d4f8a1c6c062a88711a32d1ac08dc59a647 0 1653568468937 3 connected
1e79791c833562821282d9aec31f4d116fc5e1c7 10.1.2.209:6379@16379 master - 0 1653568467931 2 connected 5461-10922
bbc82d4f8a1c6c062a88711a32d1ac08dc59a647 10.1.2.210:6379@16379 master - 0 1653568467000 3 connected 10923-16383
vars currentEpoch 6 lastVoteEpoch 0

2.6、使用Python脚本实现Redis Cluster集群的写入

#这里需要下载一些相关的包,我这使用的是ubuntu的1804版本系统,系统里面带了python3但是少了一些包需要自己安装
root@node1:~# apt install python3-pip
root@node1:~# pip3 install redis-py-cluster
root@node1:~# vim redis_cluster_test.py
#!/usr/bin/env python3                                                                                                                                                                                          
from rediscluster  import RedisCluster

if __name__ == '__main__':

    startup_nodes = [
        {"host":"10.0.0.8", "port":6379},
        {"host":"10.0.0.18", "port":6379},
        {"host":"10.0.0.28", "port":6379},
        {"host":"10.0.0.38", "port":6379},
        {"host":"10.0.0.48", "port":6379},
        {"host":"10.0.0.58", "port":6379}]
    try:
        redis_conn= RedisCluster(startup_nodes=startup_nodes,password='123456', decode_responses=True)
    except Exception as e:
        print(e)

    for i in range(0, 10000):
        redis_conn.set('key'+str(i),'value'+str(i))
        print('key'+str(i)+':',redis_conn.get('key'+str(i)))

root@node1:~# chmod +x redis_cluster_test.py
root@node1:~# ./redis_cluster_test.py
........
key9995: value9995
key9996: value9996
key9997: value9997
key9998: value9998
key9999: value9999

#验证数据是否写入
root@node1:~# redis-cli -a wm521314 -h 10.1.2.208 dbsize
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(integer) 3331
root@node1:~# redis-cli -a wm521314 -h 10.1.2.209 dbsize
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(integer) 3340
root@node1:~# redis-cli -a wm521314 -h 10.1.2.210 dbsize
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(integer) 3329
root@node1:~# redis-cli -a wm521314 -h 10.1.2.209 get key10
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
"value10"

2.7、模拟master故障,对应的slave节点自动提升为新master

#假设node3节点出问题,需要相应的数秒故障转移时间,先查看一下node3节点没出问题的时候信息
root@node3:~# redis-cli -a wm521314 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=10.1.2.213,port=6379,state=online,offset=138842,lag=1
master_failover_state:no-failover
master_replid:4d10bc7e1a4ae847d62a198d0b9eef1729a01af8
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:138842
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:138842
#向redis服务发出一个shutdown的指令关掉服务
root@node3:~# redis-cli -a wm521314 shutdown
root@node3:~# tail -f /apps/redis/log/redis-6379.log
14814:M 26 May 2022 13:12:16.229 # User requested shutdown...
14814:M 26 May 2022 13:12:16.229 * Saving the final RDB snapshot before exiting.
14814:M 26 May 2022 13:12:16.262 * DB saved on disk
14814:M 26 May 2022 13:12:16.262 * Removing the pid file.
14814:M 26 May 2022 13:12:16.264 # Redis is now ready to exit, bye bye...

#查看端口是没有6379端口的了
root@node3:~# ss -tnl
State                      Recv-Q                      Send-Q                                            Local Address:Port                                              Peer Address:Port                      
LISTEN                     0                           64                                                      0.0.0.0:31400                                                  0.0.0.0:*                         
LISTEN                     0                           128                                                     0.0.0.0:111                                                    0.0.0.0:*                         
LISTEN                     0                           128                                                     0.0.0.0:46544                                                  0.0.0.0:*                         
LISTEN                     0                           128                                                     0.0.0.0:12564                                                  0.0.0.0:*                         
LISTEN                     0                           128                                                     0.0.0.0:11956                                                  0.0.0.0:*                         
LISTEN                     0                           128                                               127.0.0.53%lo:53                                                     0.0.0.0:*                         
LISTEN                     0                           128                                                     0.0.0.0:22                                                     0.0.0.0:*                         
LISTEN                     0                           100                                                     0.0.0.0:25                                                     0.0.0.0:*                         
LISTEN                     0                           128                                                   127.0.0.1:6010                                                   0.0.0.0:*                         
LISTEN                     0                           128                                                   127.0.0.1:6011                                                   0.0.0.0:*                         
LISTEN                     0                           64                                                      0.0.0.0:2049                                                   0.0.0.0:*                         
LISTEN                     0                           128                                                        [::]:15110                                                     [::]:*                         
LISTEN                     0                           64                                                         [::]:12104                                                     [::]:*                         
LISTEN                     0                           128                                                        [::]:31178                                                     [::]:*                         
LISTEN                     0                           128                                                        [::]:20206                                                     [::]:*                         
LISTEN                     0                           128                                                        [::]:111                                                       [::]:*                         
LISTEN                     0                           128                                                        [::]:22                                                        [::]:*                         
LISTEN                     0                           100                                                        [::]:25                                                        [::]:*                         
LISTEN                     0                           128                                                       [::1]:6010                                                      [::]:*                         
LISTEN                     0                           128                                                       [::1]:6011                                                      [::]:*                         
LISTEN                     0                           64                                                         [::]:2049                                                      [::]:*

#现在在查看一下每个节点的对应关系
root@node1:~# redis-cli -a wm521314 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
8f463789ccd8638709df9611270b2c0cd83984d5 10.1.2.215:6379@16379 slave 2d04605711615ba0acbf2a098c9c79c59b23f408 0 1653570977153 1 connected
d1112ea7d3bed02b6b4e1d89aa19fd3a38568ffd 10.1.2.217:6379@16379 slave 1e79791c833562821282d9aec31f4d116fc5e1c7 0 1653570977000 2 connected
2d04605711615ba0acbf2a098c9c79c59b23f408 10.1.2.208:6379@16379 myself,master - 0 1653570976000 1 connected 0-5460
285c027937cf19ed100efda1b5116c4bae04e50a 10.1.2.213:6379@16379 master - 0 1653570978165 7 connected 10923-16383
1e79791c833562821282d9aec31f4d116fc5e1c7 10.1.2.209:6379@16379 master - 0 1653570977000 2 connected 5461-10922
bbc82d4f8a1c6c062a88711a32d1ac08dc59a647 10.1.2.210:6379@16379 master,fail - 1653570736617 1653570733597 3 disconnected

#10.1.2.213这里会发现自动提升为新的master
root@node4:~# redis-cli -a wm521314 INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:0
master_failover_state:no-failover
master_replid:eeab9e0bbcf0c124a6ef96a2bd91a7215b378f36
master_replid2:4d10bc7e1a4ae847d62a198d0b9eef1729a01af8
master_repl_offset:138940
second_repl_offset:138941
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:138940
root@node4:~# tail -f /apps/redis/log/redis-6379.log
15059:S 26 May 2022 13:12:33.778 * FAIL message received from d1112ea7d3bed02b6b4e1d89aa19fd3a38568ffd about bbc82d4f8a1c6c062a88711a32d1ac08dc59a647
15059:S 26 May 2022 13:12:33.871 # Start of election delayed for 892 milliseconds (rank #0, offset 138940).
15059:S 26 May 2022 13:12:34.276 * Connecting to MASTER 10.1.2.210:6379
15059:S 26 May 2022 13:12:34.276 * MASTER <-> REPLICA sync started
15059:S 26 May 2022 13:12:34.277 # Error condition on socket for SYNC: Connection refused
15059:S 26 May 2022 13:12:34.782 # Starting a failover election for epoch 7.
15059:S 26 May 2022 13:12:34.806 # Failover election won: I'm the new master.
15059:S 26 May 2022 13:12:34.806 # configEpoch set to 7 after successful failover
15059:M 26 May 2022 13:12:34.806 * Discarding previously cached master state.
15059:M 26 May 2022 13:12:34.806 # Setting secondary replication ID to 4d10bc7e1a4ae847d62a198d0b9eef1729a01af8, valid up to offset: 138941. New replication ID is eeab9e0bbcf0c124a6ef96a2bd91a7215b378f36

2.8、恢复故障节点node3自动成为slave节点

root@node3:~# systemctl restart redis
root@node3:~# ss -tnl
State                      Recv-Q                      Send-Q                                            Local Address:Port                                              Peer Address:Port                      
LISTEN                     0                           64                                                      0.0.0.0:31400                                                  0.0.0.0:*                         
LISTEN                     0                           511                                                     0.0.0.0:6379                                                   0.0.0.0:*                         
LISTEN                     0                           128                                                     0.0.0.0:111                                                    0.0.0.0:*                         
LISTEN                     0                           128                                                     0.0.0.0:46544                                                  0.0.0.0:*                         
LISTEN                     0                           128                                                     0.0.0.0:12564                                                  0.0.0.0:*                         
LISTEN                     0                           128                                                     0.0.0.0:11956                                                  0.0.0.0:*                         
LISTEN                     0                           128                                               127.0.0.53%lo:53                                                     0.0.0.0:*                         
LISTEN                     0                           128                                                     0.0.0.0:22                                                     0.0.0.0:*                         
LISTEN                     0                           100                                                     0.0.0.0:25                                                     0.0.0.0:*                         
LISTEN                     0                           128                                                   127.0.0.1:6010                                                   0.0.0.0:*                         
LISTEN                     0                           511                                                     0.0.0.0:16379                                                  0.0.0.0:*                         
LISTEN                     0                           64                                                      0.0.0.0:2049                                                   0.0.0.0:*                         
LISTEN                     0                           128                                                        [::]:15110                                                     [::]:*                         
LISTEN                     0                           64                                                         [::]:12104                                                     [::]:*                         
LISTEN                     0                           128                                                        [::]:31178                                                     [::]:*                         
LISTEN                     0                           128                                                        [::]:20206                                                     [::]:*                         
LISTEN                     0                           128                                                        [::]:111                                                       [::]:*                         
LISTEN                     0                           128                                                        [::]:22                                                        [::]:*                         
LISTEN                     0                           100                                                        [::]:25                                                        [::]:*                         
LISTEN                     0                           128                                                       [::1]:6010                                                      [::]:*                         
LISTEN                     0                           64                                                         [::]:2049                                                      [::]:*

#查看集群的状态文件,会发现node3自动上线成为slave节点了
root@node3:~# cat /apps/redis/data/nodes-6379.conf
285c027937cf19ed100efda1b5116c4bae04e50a 10.1.2.213:6379@16379 master - 0 1653571237158 7 connected 10923-16383
bbc82d4f8a1c6c062a88711a32d1ac08dc59a647 10.1.2.210:6379@16379 myself,slave 285c027937cf19ed100efda1b5116c4bae04e50a 0 1653571237146 7 connected
1e79791c833562821282d9aec31f4d116fc5e1c7 10.1.2.209:6379@16379 master - 0 1653571237158 2 connected 5461-10922
2d04605711615ba0acbf2a098c9c79c59b23f408 10.1.2.208:6379@16379 master - 0 1653571237158 1 connected 0-5460
d1112ea7d3bed02b6b4e1d89aa19fd3a38568ffd 10.1.2.217:6379@16379 slave 1e79791c833562821282d9aec31f4d116fc5e1c7 1653571237157 1653571237146 2 connected
8f463789ccd8638709df9611270b2c0cd83984d5 10.1.2.215:6379@16379 slave 2d04605711615ba0acbf2a098c9c79c59b23f408 0 1653571237158 1 connected
vars currentEpoch 7 lastVoteEpoch 0
root@node3:~# redis-cli -a wm521314 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:slave
master_host:10.1.2.213
master_port:6379
master_link_status:up
master_last_io_seconds_ago:3
master_sync_in_progress:0
slave_read_repl_offset:139122
slave_repl_offset:139122
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:eeab9e0bbcf0c124a6ef96a2bd91a7215b378f36
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:139122
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:138941
repl_backlog_histlen:182

#10.1.2.213主机上查看主从关系
root@node4:~# redis-cli -a wm521314 -c INFO replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=10.1.2.210,port=6379,state=online,offset=139164,lag=0
master_failover_state:no-failover
master_replid:eeab9e0bbcf0c124a6ef96a2bd91a7215b378f36
master_replid2:4d10bc7e1a4ae847d62a198d0b9eef1729a01af8
master_repl_offset:139164
second_repl_offset:138941
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:139164

#查看现在集群每个节点的对应关系
root@node1:~# redis-cli -a wm521314 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
8f463789ccd8638709df9611270b2c0cd83984d5 10.1.2.215:6379@16379 slave 2d04605711615ba0acbf2a098c9c79c59b23f408 0 1653571482198 1 connected
d1112ea7d3bed02b6b4e1d89aa19fd3a38568ffd 10.1.2.217:6379@16379 slave 1e79791c833562821282d9aec31f4d116fc5e1c7 0 1653571481192 2 connected
2d04605711615ba0acbf2a098c9c79c59b23f408 10.1.2.208:6379@16379 myself,master - 0 1653571481000 1 connected 0-5460
285c027937cf19ed100efda1b5116c4bae04e50a 10.1.2.213:6379@16379 master - 0 1653571482000 7 connected 10923-16383
1e79791c833562821282d9aec31f4d116fc5e1c7 10.1.2.209:6379@16379 master - 0 1653571483202 2 connected 5461-10922
bbc82d4f8a1c6c062a88711a32d1ac08dc59a647 10.1.2.210:6379@16379 slave 285c027937cf19ed100efda1b5116c4bae04e50a 0 1653571481000 7 connected

2.8、附一键安装redis的脚本

#!/bin/bash

SRC_DIR=/usr/local/src
COLOR="echo -e \\033[01;31m"
END='\033[0m'
CPUS=`lscpu | awk '/^CPU\(s\)/{print $2}'`

URL='https://download.redis.io/releases/'
VERSION=redis-6.2.7
PASSWORD=wm521314
INSTALL_DIR=/apps/redis

os() {
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release;then
        rpm -q redhat-lsb-core &> /dev/null || { ${COLOR}"安装lsb_release工具"${END};yum -y install redhat-lsb-core &> /dev/null; }
    fi
    OS_ID=`lsb_release -is`
}

install() {
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        yum -y install gcc jemalloc-devel || { ${COLOR}"安装软件包失败,请检查网络配置"${END}; exit; }
        rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
    else
        apt -y install make gcc libjemalloc-dev || { ${COLOR}"安装软件包失败,请检查网络配置"${END}; exit; }
    fi

    cd ${SRC_DIR}
    wget ${URL}${VERSION}.tar.gz || { ${COLOR}"Redis源码下载失败"${END}; exit; }
    tar xf ${VERSION}.tar.gz
    cd ${VERSION}
    make -j ${CPUS} PREFIX=${INSTALL_DIR} install && ${COLOR}"Redis编译安装完成"${END} || { ${COLOR}"Redis编译安装失败"${END}; exit; }
    ln -s ${INSTALL_DIR}/bin/redis-* /usr/bin/
    mkdir -p ${INSTALL_DIR}/{etc,log,data,run}
    cp redis.conf ${INSTALL_DIR}/etc/
    sed -i -e 's/bind 127.0.0.1.*/bind 0.0.0.0/' -e "/# requirepass/a requirepass ${PASSWORD}" -e "/^dir .*/c dir ${INSTALL_DIR}/data/" -e "/logfile .*/c logfile ${INSTALL_DIR}/log/redis-6379.log" -e "/^pidfile .*/c pidfile ${INSTALL_DIR}/run/redis_6379.pid" ${INSTALL_DIR}/etc/redis.conf

    if id redis &> /dev/null ;then
        ${COLOR}"Redis用户已经存在"${END}
    else
        useradd -r -s /sbin/nologin redis
        ${COLOR}"Redis用户创建成功"${END}
    fi

    chown -R redis.redis ${INSTALL_DIR}
    cat >> /etc/sysctl.conf <<-EOF
net.core.somaxconn = 1024
vm.overcommit_memory = 1
EOF
    sysctl -p
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.d/rc.local
        chmod +x /etc/rc.d/rc.local
    else
        cat >> /lib/systemd/system/rc-local.service <<-EOF
[Install]
WantedBy=multi-user.target
EOF
        echo '#!/bin/bash' > /etc/rc.local
        echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
        chmod +x /etc/rc.local
    fi

    cat > /lib/systemd/system/redis.service <<-EOF
[Unit]
Description=Redis persistent key-value database
After=network.target

[Service]
ExecStart=${INSTALL_DIR}/bin/redis-server ${INSTALL_DIR}/etc/redis.conf --supervised systemd
ExecStop=/bin/kill -s QUIT \$MAINPID
#Type=notify                                                                                                                                                                                                    
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    systemctl enable --now redis &> /dev/null && ${COLOR}"Redis服务启动成功,Redis信息如下:"${END} || { ${COLOR}"Redis服务启动失败"${END};exit; }
    sleep 1
    redis-cli -a ${PASSWORD} INFO Server 2> /dev/null
}

main() {
    os
    install
}

main
举报

相关推荐

0 条评论