数据库&&Redis&&集群搭建


Redis集群主要有三种集群模式:主从复制模式(Master-Slave)、哨兵模式(Sentinel)和Cluster模式

【Redis】三种集群模式(主从复制、哨兵模式、Cluster)
那些年用过的Redis集群架构(含面试解析)
面试官:为什么 Redis 要有哨兵?
Redis ==> 集群的三种模式

主从复制模式(Master-Slave)

工作原理

Redis的主从复制是一种数据复制技术,在主从复制模式中,有一个主节点和多个从节点。主节点负责处理写请求,而从节点则负责处理读请求。这种模式下的数据同步是单向的,即只能从主节点向从节点同步数据。当主节点出现故障时,其中一个从节点可以被提升为新的主节点,以保证服务的持续可用。

在这里插入图片描述
同步(SYNC)
同步(SYNC)用来将从服务器的状态 更新到 和主服务器 一致。白话文解释就是从服务器主动获取 主服务器的数据。保持数据一致。具体实现是,主服务器收到SYNC命令后,生成RDB快照文件,然后发送给从服务器。
命令传播 (command propagate)
用于在主服务器数据被修改后,主从不一致,为了让从服务器保持和主服务器状态一致,而做的命令传播。白话文解释就是主服务器收到客户端修改数据命令后,数据库数据发生变化,同时将命令缓存起来,然后将缓存命令发送到从服务器,从服务器通过载入缓存命令来达到主从数据一致。这就是所谓的命令传播。
在这里插入图片描述
在这里插入图片描述

  1. 从数据库连接主数据库,发送SYNC命令;
  2. 主数据库接收到SYNC命令后,开始执行BGSAVE命令生成RDB文件并使用缓冲区记录此后执行的所有写命令;
  3. 主数据库BGSAVE执行完后,向所有从数据库发送快照文件,并在发送期间继续记录被执行的写命令;
  4. 从数据库收到快照文件后丢弃所有旧数据,载入收到的快照;
  5. 主数据库快照发送完毕后开始向从数据库发送缓冲区中的写命令;
  6. 从数据库完成对快照的载入,开始接收命令请求,并执行来自主数据库缓冲区的写命令;(从数据库初始化完成)
  7. 主数据库每执行一个写命令就会向从数据库发送相同的写命令,从数据库接收并执行收到的写命令(从数据库初始化完成后的操作)
  8. 出现断开重连后,2.8之后的版本会将断线期间的命令传给重数据库,增量复制。
  9. 主从刚刚连接的时候,进行全量同步;全同步结束后,进行增量同步。当然,如果有需要,slave 在任何时候都可以发起全量同步。Redis 的策略是,无论如何,首先会尝试进行增量同步,如不成功,要求从机进行全量同步。

优点:

  • 支持主从复制,主机会自动将数据同步到从机,可以进行读写分离;
  • 为了分载Master的读操作压力,Slave服务器可以为客户端提供只读操作的服务,写服务仍然必须由Master来完成;
  • Slave同样可以接受其它Slaves的连接和同步请求,这样可以有效的分载Master的同步压力;
  • Master Server是以非阻塞的方式为Slaves提供服务。所以在Master-Slave同步期间,客户端仍然可以提交查询或修改请求;
  • Slave Server同样是以非阻塞的方式完成数据同步。在同步期间,如果有客户端提交查询请求,Redis则返回同步之前的数据;

缺点:

  • master宕机期间,需要手动切换主机,同时会有部分数据不能及时同步从服务器,造成数据不一致(需要人工手动介入)。
  • slave宕机后,多个slave恢复后,大量的SYNC同步会造成master IO压力倍增(可以手动规避启动时间);
  • 在线扩容较复杂。

部署搭建

vi redis-6380/redis.conf
port 6380
pidfile /var/run/redis-6380.pid
logfile "redis-6380.log"
dbfilename dump-6380.rdb
daemonize yes

vi redis-6381/redis.conf
port 6381
pidfile /var/run/redis-6381.pid
logfile "redis-6381.log"
dbfilename dump-6381.rdb
daemonize yes
# 如果不通过修改配置文件,也可以在客户端中输入“SLAVEOF 127.0.0.1 6380”即刻生效!!
# 也可以在客户端中输入“SLAVEOF NO ONE”来断开主从关系
replicaof 127.0.0.1 6380

vi redis-6382/redis.conf
port 6382
pidfile /var/run/redis-6382.pid
logfile "redis-6382.log"
dbfilename dump-6382.rdb
daemonize yes
replicaof 127.0.0.1 6380

# 启动redis服务
# 验证
./redis-cli -p 6380
> ping
> INFO replication

哨兵模式(Sentinel)

工作原理

哨兵模式是为了解决主从复制集群中主机宕机后,主备切换的复杂性而演变出来的。在这种模式下,有一个或多个哨兵节点,它们的主要作用就是监控主从集群,自动切换主备,完成集群故障转移。
Redis的哨兵模式是一种特殊的高可用解决方案。哨兵能够实时检测master的状态,当master处于故障状态时,哨兵会根据一定的选举算法选出一个新的主节点,并通过发布订阅模式将其他从节点切换到新的主节点上。Redis的哨兵模式中,为了确保其健壮性,至少需要部署3个实例。这三个实例分别扮演不同的角色:主节点、从节点和哨兵节点。
在这里插入图片描述

  1. 每个Sentinel(哨兵)进程以每秒钟一次的频率向整个集群中的Master主服务器、Slave从服务器以及其他Sentinel(哨兵)进程发送一个 PING 命令。
  2. 如果一个实例(instance)距离最后一次有效回复 PING 命令的时间超过 down-after-milliseconds 选项所指定的值, 则这个实例会被 Sentinel(哨兵)进程标记为主观下线(SDOWN)。
  3. 如果一个Master主服务器被标记为主观下线(SDOWN),则正在监视这个Master主服务器的所有 Sentinel(哨兵)进程要以每秒一次的频率确认Master主服务器的确进入了主观下线状态。
  4. 当有足够数量的 Sentinel(哨兵)进程(大于等于配置文件指定的值)在指定的时间范围内确认Master主服务器进入了主观下线状态(SDOWN), 则Master主服务器会被标记为客观下线(ODOWN)。
  5. 此时就会进行哨兵选举,选出一个领头的哨兵对主从数据库发起故障的修复(就是选一个从数据库作为新的Master)。
  6. 在一般情况下, 每个 Sentinel(哨兵)进程会以每 10 秒一次的频率向集群中的所有Master主服务器、Slave从服务器发送 INFO 命令。
  7. 当Master主服务器被 Sentinel(哨兵)进程标记为客观下线(ODOWN)时,Sentinel(哨兵)进程向下线的 Master主服务器的所有 Slave从服务器发送 INFO 命令的频率会从 10 秒一次改为每秒一次。
  8. 若没有足够数量的 Sentinel(哨兵)进程同意 Master主服务器下线, Master主服务器的客观下线状态就会被移除。若 Master主服务器重新向 Sentinel(哨兵)进程发送 PING 命令返回有效回复,Master主服务器的主观下线状态就会被移除。

哨兵监控

  • 如果 Redis 节点未在 [master-down-after-milliseconds] 指定的时间内,对向它发送 PING 命令的哨兵返回一个有效回复,该哨兵就会将这个服务器标记为下线。
  • 当某个哨兵先检测到主节点不可用时,系统并不会马上进行 failover 过程【最终只有一个 sentinel 节点作为 failover 的发起者,就需要选举一个 leader(采用类似 Raft 协议实现选举算法)】。该哨兵会通知其它哨兵,发送 [SENTINEL is-master-down-by-addr] 来询问其他哨兵是否认为主节点的服务器已下线并提议选举自己为领导者哨兵。
  • 如果在规定的时间内接收到多个哨兵(哨兵数量大于3且为奇数)的同意时,领导者哨兵产生。

哨兵通知及故障转移

  • 领导者哨兵选出一个从节点,并将其升级为主节点。
  • 向被选中的从节点发送 SLAVEOF NO ONE 命令,让它转变为主节点。
  • 通过发布与订阅功能,将更新后的配置传播给所有其他 Sentinel,其他 Sentinel 对他们自己的配置进行更新。
  • 向其他从节点发送 SLAVE OF 命令,让它们去复制新的主节点。
  • 当所有的从节点都已经开始复制新的主节点时,领导者哨兵终止此次故障迁移操作。
  • 每当一个 Redis 实例被重新配置(reconfigured),无论是被设置成主节点、从节点、又或者被设置成其他主节点的从节点,哨兵都会向被重新配置的实例发送一个 CONFIG REWRITE 命令,从而确保这些配置会持久化到硬盘。

哨兵配置

在这里插入图片描述
缺点:

  • Redis较难支持在线扩容,在线扩容比较复杂。

部署搭建

搭建 一主两从 共 3 个节点的 Redis 主从复制节点。

#8000 为主节点端口,8001和8002为对应从节点
8000>./redis-server redis.conf
8001>./redis-server redis.conf
8002>./redis-server redis.conf

新建3个哨兵节点并配置哨兵 sentinel.conf 文件。新建端口为18000、18001、18002 共三个哨兵节点,启动:./redis-sentinel sentinel.conf

vi sentinel.conf
bind 127.0.0.1
port 18000 #其他两个分别为 18002、18003
protected-mode no  #若想从远程连接redis集群,需要将sentinel的protected-mode修改为no
sentinel myid sentinel-1  #设定sentinel myid 每个都不一样,使用yum安装的时候,直接就生成了
sentinel monitor mymaster 127.0.0.1 8000 2 #数字2,表示主机挂掉后salve投票看让谁接替成为主机,得票数多少后成为主机。
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 15000
sentinel parallel-syncs mymaster 2
sentinel auth-pass mymaster 123456  #设置用于与主服务器和从服务器进行身份验证的密码

使用 Redis-cli 连接

$redis-cli -h 127.0.0.1 -p 8000 -a 123456

Cluster模式

在集群模式下,Cluster实现了分布式存储,数据被分片存储在多个节点上,每个节点负责一部分数据。这种模式下的数据同步是双向的,即可以在各个节点之间进行数据同步。此外,当某个节点出现故障时,其负责的数据可以被其他节点接管,以保证服务的持续可用。
Cluster 模式使用了Sharding 技术,实现了高可用、读写分离和真正的分布式存储。
在这里插入图片描述

部署搭建

# 配置节点配置文件
cp redis.conf  6380.conf
vim 6380.conf
    # 设置节点端口
    port 6380
    # 开启当前节点的集群模式
    cluster-enabled yes
    # 设置保存节点配置文件的路径
    cluster-config-file nodes-6380.conf
    cluster-node-timeout 5000
    appendonly yes
    
# 通过多次复制,创建多个节点配置文件,修改使其监听不同的端口
[root@k8s-node01 redis-cluster]# cp 6380.conf 6381.conf 
[root@k8s-node01 redis-cluster]# cp 6380.conf 6382.conf 
[root@k8s-node01 redis-cluster]# cp 6380.conf 6383.conf 
[root@k8s-node01 redis-cluster]# cp 6380.conf 6384.conf 
[root@k8s-node01 redis-cluster]# cp 6380.conf 6385.conf

[root@k8s-node01 redis-cluster]# cd ../redis-cluster/
[root@k8s-node01 redis-cluster]# mkdir 638{0..5}
[root@k8s-node01 redis-cluster]# ls
6380  6380.conf  6381  6381.conf  6382  6382.conf  6383  6383.conf  6384  6384.conf  6385  6385.conf  redis.conf

[root@k8s-node01 redis-cluster]# mv 6380.conf 6380
[root@k8s-node01 redis-cluster]# mv 6381.conf 6381
[root@k8s-node01 redis-cluster]# mv 6382.conf 6382
[root@k8s-node01 redis-cluster]# mv 6383.conf 6383
[root@k8s-node01 redis-cluster]# mv 6384.conf 6384
[root@k8s-node01 redis-cluster]# mv 6385.conf 6385
[root@k8s-node01 redis-cluster]# ls
6380  6381  6382  6383  6384  6385  redis.conf

# 使用编译安装好的redis服务的redis-server程序依次启动6个节点的配置文件
[root@k8s-node01 ~]# cd /usr/local/src/redis-5.0.8
[root@k8s-node01 redis-5.0.8]# ./src/redis-server  ../redis-cluster/6380/6380.conf 
24861:C 16 Jun 2020 13:09:20.641 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
24861:C 16 Jun 2020 13:09:20.642 # Redis version=5.0.8, bits=64, commit=00000000, modified=0, pid=24861, just started
24861:C 16 Jun 2020 13:09:20.642 # Configuration loaded
24861:M 16 Jun 2020 13:09:20.642 * Increased maximum number of open files to 10032 (it was originally set to 1024).
24861:M 16 Jun 2020 13:09:20.643 * No cluster configuration found, I'm 467be73fbfb59aef9a78ab6734084e4e4a12d96d
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 5.0.8 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in cluster mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6380
 |    `-._   `._    /     _.-'    |     PID: 24861
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

24861:M 16 Jun 2020 13:09:22.220 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
24861:M 16 Jun 2020 13:09:22.220 # Server initialized
24861:M 16 Jun 2020 13:09:22.220 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
24861:M 16 Jun 2020 13:09:22.221 * Ready to accept connections
#/usr/local/src/redis-5.0.8/src/redis-server  /usr/local/src/redis-cluster/6381/6381.conf

# 检测
[root@k8s-node01 redis-5.0.8]# netstat -anlp| grep redis
tcp        0      0 127.0.0.1:16380         0.0.0.0:*               LISTEN      24861/./src/redis-s 
tcp        0      0 127.0.0.1:16381         0.0.0.0:*               LISTEN      24902/redis-server  
tcp        0      0 127.0.0.1:16382         0.0.0.0:*               LISTEN      24906/redis-server  
tcp        0      0 127.0.0.1:16383         0.0.0.0:*               LISTEN      24911/redis-server  
tcp        0      0 127.0.0.1:16384         0.0.0.0:*               LISTEN      24915/redis-server  
tcp        0      0 127.0.0.1:16385         0.0.0.0:*               LISTEN      24939/redis-server  
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      21252/./redis-serve 
tcp        0      0 127.0.0.1:6380          0.0.0.0:*               LISTEN      24861/./src/redis-s 
tcp        0      0 127.0.0.1:6381          0.0.0.0:*               LISTEN      24902/redis-server  
tcp        0      0 127.0.0.1:6382          0.0.0.0:*               LISTEN      24906/redis-server  
tcp        0      0 127.0.0.1:6383          0.0.0.0:*               LISTEN      24911/redis-server  
tcp        0      0 127.0.0.1:6384          0.0.0.0:*               LISTEN      24915/redis-server  
tcp        0      0 127.0.0.1:6385          0.0.0.0:*               LISTEN      24939/redis-server  
tcp6       0      0 :::6379                 :::*                    LISTEN      21252/./redis-serve

# 建立集群
[root@k8s-node01 redis-5.0.8]# /usr/local/src/redis-5.0.8/src/redis-cli --cluster create  127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384  127.0.0.1:6385 127.0.0.1:6380 --cluster-replicas 1 127.0.0.1:6380
>>> 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:6385 to 127.0.0.1:6381
Adding replica 127.0.0.1:6380 to 127.0.0.1:6382
Adding replica 127.0.0.1:6384 to 127.0.0.1:6383
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 632eba86cf41797baaf730a13ae8d29249a7de01 127.0.0.1:6381
   slots:[0-5460] (5461 slots) master
M: 3597b8d02624087464b0312032d6eb447fed3b92 127.0.0.1:6382
   slots:[5461-10922] (5462 slots) master
M: 7317d5b19bdc066a71187fff42070c8635fce381 127.0.0.1:6383
   slots:[10923-16383] (5461 slots) master
S: 80499d1b9f025af6f691d13e5074378e26408c1e 127.0.0.1:6384
   replicates 7317d5b19bdc066a71187fff42070c8635fce381
S: ada02b816691e1ea6cc4c8b0c26766f673ec507f 127.0.0.1:6385
   replicates 632eba86cf41797baaf730a13ae8d29249a7de01
S: 467be73fbfb59aef9a78ab6734084e4e4a12d96d 127.0.0.1:6380
   replicates 3597b8d02624087464b0312032d6eb447fed3b92
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:6381)
M: 632eba86cf41797baaf730a13ae8d29249a7de01 127.0.0.1:6381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 467be73fbfb59aef9a78ab6734084e4e4a12d96d 127.0.0.1:6380
   slots: (0 slots) slave
   replicates 3597b8d02624087464b0312032d6eb447fed3b92
M: 7317d5b19bdc066a71187fff42070c8635fce381 127.0.0.1:6383
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 3597b8d02624087464b0312032d6eb447fed3b92 127.0.0.1:6382
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 80499d1b9f025af6f691d13e5074378e26408c1e 127.0.0.1:6384
   slots: (0 slots) slave
   replicates 7317d5b19bdc066a71187fff42070c8635fce381
S: ada02b816691e1ea6cc4c8b0c26766f673ec507f 127.0.0.1:6385
   slots: (0 slots) slave
   replicates 632eba86cf41797baaf730a13ae8d29249a7de01
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

# 检测
127.0.0.1:6381> set dfq duanfuqiang
OK
127.0.0.1:6381> get dfq
"duanfuqiang"
127.0.0.1:6381> CLUSTER INFO
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:561
cluster_stats_messages_pong_sent:568
cluster_stats_messages_sent:1129
cluster_stats_messages_ping_received:563
cluster_stats_messages_pong_received:561
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:1129
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值