Redis主从复制和哨兵模式

您所在的位置:网站首页 sqlserver主从复制及热备 Redis主从复制和哨兵模式

Redis主从复制和哨兵模式

2023-03-25 13:46| 来源: 网络整理| 查看: 265

Redis主从复制和哨兵模式

主从复制

主从复制,是指将一台Redis服务器的数据,复制到其他的Redis服务器。

前者称为主节点(master),后者称为从节点(slave);数据的复制是单向的,只能由主节点到从节点。

Redis主从复制和哨兵模式_redis

主从模式配置很简单,只需要在从节点配置主节点的ip和端口号即可。

slaveof # 例如# slaveof 192.168.100.10 6379

启动主从节点的所有服务,查看日志即可以看到主从节点之间的服务连接。

从上面很容易就想到一个问题,既然主从复制,意味着master和slave的数据都是一样的,有数据冗余问题。

在程序设计上,为了高可用性和高性能,是允许有冗余存在的。这点希望大家在设计系统的时候要考虑进去,不用为公司节省这一点资源。

对于追求极致用户体验的产品,是绝对不允许有宕机存在的。

主从模式在很多系统设计时都会考虑,一个master挂在多个slave节点,当master服务宕机,会选举产生一个新的master节点,从而保证服务的高可用性。

主从模式的优点:

一旦 主节点宕机,从节点 作为 主节点 的 备份 可以随时顶上来。扩展 主节点 的 读能力,分担主节点读压力。高可用基石:除了上述作用以外,主从复制还是哨兵模式和集群模式能够实施的基础,因此说主从复制是Redis高可用的基石。也有相应的缺点,比如我刚提到的数据冗余问题:

一旦 主节点宕机,从节点 晋升成 主节点,同时需要修改 应用方 的 主节点地址,还需要命令所有 从节点 去 复制 新的主节点,整个过程需要 人工干预。主节点 的 写能力 受到 单机的限制。主节点 的 存储能力 受到 单机的限制。哨兵模式

刚刚提到了,主从模式,当主节点宕机之后,从节点是可以作为主节点顶上来,继续提供服务的。

但是有一个问题,主节点的IP已经变动了,此时应用服务还是拿着原主节点的地址去访问,这...

于是,在Redis 2.8版本开始引入,就有了哨兵这个概念。

在复制的基础上,哨兵实现了自动化的故障恢复。

Redis主从复制和哨兵模式_哨兵模式_02

如图,哨兵节点由两部分组成,哨兵节点和数据节点:

哨兵节点:哨兵系统由一个或多个哨兵节点组成,哨兵节点是特殊的redis节点,不存储数据。数据节点:主节点和从节点都是数据节点。访问redis集群的数据都是通过哨兵集群的,哨兵监控整个redis集群。

一旦发现redis集群出现了问题,比如刚刚说的主节点挂了,从节点会顶上来。但是主节点地址变了,这时候应用服务无感知,也不用更改访问地址,因为哨兵才是和应用服务做交互的。

Sentinel 很好的解决了故障转移,在高可用方面又上升了一个台阶,当然Sentinel还有其他功能。

比如 主节点存活检测、主从运行情况检测、主从切换。

Redis的Sentinel最小配置是 一主一从。

说下哨兵模式监控的原理

每个Sentinel以 每秒钟 一次的频率,向它所有的 主服务器、从服务器以及其他Sentinel实例 发送一个PING 命令。

Redis主从复制和哨兵模式_Redis_03

如果一个 实例(instance)距离最后一次有效回复 PING命令的时间超过 down-after-milliseconds 所指定的值,那么这个实例会被 Sentinel标记为 主观下线。

如果一个 主服务器被标记为 主观下线,那么正在 监视 这个 主服务器 的所有 Sentinel 节点,要以 每秒一次 的频率确认 该主服务器是否的确进入了 主观下线 状态。

如果一个 主服务器 被标记为 主观下线,并且有 足够数量的 Sentinel(至少要达到配置文件指定的数量)在指定的 时间范围 内同意这一判断,那么这个该主服务器被标记为 客观下线。

在一般情况下, 每个 Sentinel 会以每 10秒一次的频率,向它已知的所有 主服务器 和 从服务器 发送 INFO 命令。

当一个 主服务器被 Sentinel标记为 客观下线 时,Sentinel 向 下线主服务器 的所有 从服务器 发送 INFO 命令的频率,会从10秒一次改为 每秒一次。

Sentinel和其他 Sentinel 协商 主节点的状态,如果 主节点处于 **SDOWN`\**状态,则投票\自动选出新的主节点。将剩余的 从节点 指向 新的主节点 进行 数据复制。

当没有足够数量的 Sentinel 同意 主服务器 下线时, 主服务器 的 客观下线状态就会被移除。当 主服务器 重新向 Sentinel的PING命令返回 有效回复 时,主服务器 的 主观下线状态 就会被移除。

哨兵模式的优缺点

它的优点:

哨兵模式是基于主从模式的,所有主从的优点,哨兵模式都具有。主从可以自动切换,系统更健壮,可用性更高。Sentinel 会不断的检查 主服务器 和 从服务器 是否正常运行。当被监控的某个 Redis 服务器出现问题,Sentinel 通过API脚本向管理员或者其他的应用程序发送通知。它的缺点:

Redis较难支持在线扩容,对于集群,容量达到上限时在线扩容会变得很复杂。

接下来我们来搭建Redis主从复制和哨兵模式

拓扑图:

Redis主从复制和哨兵模式_centos_04

推荐步骤:

➢ 在 Centos01 配置 ntp 服务器,Centos02 和 Centos02 和 Centos03 以及 Centos04 配置同步时间,在 Centos01和 Centos02 以及 Centos03 和 Centos04 安装 Redis 服务器

➢ 配置 Redis主从复制群集,在Centos01 配置主 Redis 节点,Centos02 为第一台从 Redis 节点,Centos03 为第二台从节点,验证主从复制

➢ 在 Centos04上安装 Redis配置Redis 哨兵模式,监控主从 Redis 节点运行状态,主 Redis 故障自动切换到新主

Redis 服务器

实验步骤:

一、在 Centos01 配置 p ntp 服务器, Centos02 和 和 Centos02 和和Centos03 以及Centos 04 配置同步时间在 Centos 01和Centos 02以及Centos 03和 Centos 04安装Redis 服务器

1、配置时间服务器

1)修改时间服务器配置文件

[root@centos01 ~]# vim /etc/ntp.conf

restrict 192.168.100.0 mask 255.255.255.0 nomodify notrap

server127.127.1.0

fudge 127.127.1.0 stratum 8

2)重新启动时间服务器设置开机自动启动

[root@centos01 ~]# systemctl restart ntpd

[root@centos01 ~]# systemctl enable ntpd

3)从 redis 服务器同步时间

[root@centos02 ~]# ntpdate 192.168.100.10

21 Mar 21:35:04 ntpdate[1351]: adjust time server 192.168.100.10 offset -0.061565 sec

[root@centos03 ~]# ntpdate 192.168.100.10

21 Mar 21:35:14 ntpdate[1348]: adjust time server 192.168.100.10 offset -0.089094 sec

[root@centos04 ~]# ntpdate 192.168.100.10

21 Mar 21:35:18 ntpdate[1347]: adjust time server 192.168.100.10 offset 0.424229 sec

2、在 Centos01 安装 Redis 服务器

1)解压移动 Redis 安装位置

[root@centos01 ~]# mount /dev/cdrom /mnt/

mount: /dev/sr0 写保护,将以只读方式挂载

[root@centos01 ~]# ls /mnt/

redis-3.2.0.gem redis-3.2.9.tar.gz

[root@centos01 ~]# tar zxf /mnt/redis-3.2.9.tar.gz -C /usr/src/

[root@centos01 ~]# mv /usr/src/redis-3.2.9/ /usr/local/redis

2)配置安装 Redis 服务器

[root@centos01 ~]# cd /usr/local/redis/

[root@centos01 redis]# make && make install

3)初始化配置 Redis 服务器

[root@centos01 redis]# cd utils/

[root@centos01 utils]# ./install_server.sh

4)停止服务

[root@centos01 ~]# /etc/init.d/redis_6379 stop

Stopping ...

Redis stopped

3、在 Centos02 安装 Redis 服务器

1)解压移动 Redis 安装位置

[root@centos02 ~]# tar zxf ./redis-3.2.9.tar.gz -C /usr/src/

[root@centos02 ~]# mv /usr/src/redis-3.2.9/ /usr/local/redis

2)配置安装 Redis 服务器

[root@centos02 ~]# cd /usr/local/redis/

[root@centos02 redis]# make && make install

3)初始化配置 Redis 服务器

[root@centos02 redis]# cd utils/

[root@centos02 utils]# ./install_server.sh

4)停止服务

[root@centos02 utils]# cd

[root@centos02 ~]# /etc/init.d/redis_6379 stop

Stopping ...

Redis stopped

4、在 Centos03 安装 Redis 服务器

1)解压移动 Redis 安装位置

[root@centos03 ~]# tar zxf ./redis-3.2.9.tar.gz -C /usr/src/

[root@centos03 ~]# mv /usr/src/redis-3.2.9/ /usr/local/redis

2)配置安装 Redis 服务器

[root@centos03 redis]# make && make install

3)初始化配置 Redis 服务器

[root@centos03 redis]# cd utils/

[root@centos03 utils]# ./install_server.sh

Welcome to the redis service installer

This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379]

Selecting default: 6379

Please select the redis config file name [/etc/redis/6379.conf]

Selected default - /etc/redis/6379.conf

Please select the redis log file name [/var/log/redis_6379.log]

Selected default - /var/log/redis_6379.log

Please select the data directory for this instance [/var/lib/redis/6379]

Selected default - /var/lib/redis/6379

Please select the redis executable path [/usr/local/bin/redis-server]

Selected config:

Port : 6379

Config file : /etc/redis/6379.conf

Log file : /var/log/redis_6379.log

Data dir : /var/lib/redis/6379

Executable : /usr/local/bin/redis-server

Cli Executable : /usr/local/bin/redis-cli

Is this ok? Then press ENTER to go on or Ctrl-C to abort.

Copied /tmp/6379.conf => /etc/init.d/redis_6379

Installing service...

Successfully added to chkconfig!

Successfully added to runlevels 345!

Starting Redis server...

Installation successful!

4)停止服务

[root@centos03 ~]# /etc/init.d/redis_6379 stop

Stopping ...

Redis stopped

5、在 Centos04 安装 Redis 服务器

1)解压移动 Redis 安装位置

[root@centos04 ~]# tar zxf ./redis-3.2.9.tar.gz -C /usr/src/

[root@centos04 ~]# mv /usr/src/redis-3.2.9/ /usr/local/redis

2)配置安装 Redis 服务器

[root@centos04 redis]# make && make intall

3)初始化配置 Redis 服务器

[root@centos04 redis]# cd utils/

[root@centos04 utils]# ./install_server.sh

Welcome to the redis service installer

This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379]

Selecting default: 6379

Please select the redis config file name [/etc/redis/6379.conf]

Selected default - /etc/redis/6379.conf

Please select the redis log file name [/var/log/redis_6379.log]

Selected default - /var/log/redis_6379.log

Please select the data directory for this instance [/var/lib/redis/6379]

Selected default - /var/lib/redis/6379

Please select the redis executable path [/usr/local/bin/redis-server]

Selected config:

Port : 6379

Config file : /etc/redis/6379.conf

Log file : /var/log/redis_6379.log

Data dir : /var/lib/redis/6379

Executable : /usr/local/bin/redis-server

Cli Executable : /usr/local/bin/redis-cli

Is this ok? Then press ENTER to go on or Ctrl-C to abort.

Copied /tmp/6379.conf => /etc/init.d/redis_6379

Installing service...

Successfully added to chkconfig!

Successfully added to runlevels 345!

Starting Redis server...

Installation successful!

4)停止服务

[root@centos04 ~]# /etc/init.d/redis_6379 stop

Stopping ...

Redis stopped

二、 配置 Redis主从复制 群集,在 Centos1 01 配置 主 Redis 节点,Centos02 为第一台 从Redis 节点Centos03 为第二台 从 节点 ,验证主从复制

1、修改主 Redis 配置文件

1)修改主 Redis 服务器配置文件

[root@centos01 ~]# vim /etc/redis/6379.conf

63 bind 192.168.100.10

86 port 6379

130 daemonize yes

483 requirepass pwd@123

2)启动服务

[root@centos01 ~]# /etc/init.d/redis_6379 start

Starting Redis server...

[root@centos01 ~]# netstat -anptu | grep redis-server

tcp 0 0 192.168.100.10:6379 0.0.0.0:* LISTEN 5093/redis-server

2、修改第一台从 Redis 服务器配置文件

1)修改第一台从 Redis 主配置文件

[root@centos02 ~]# vim /etc/redis/6379.conf

62 bind 192.168.100.20

85 port 6379

129 daemonize yes

482 requirepass pwd@123

483 masterauth pwd@123

484 slaveof 192.168.100.10 6379

2)启动 Redis 服务

[root@centos02 ~]# /etc/init.d/redis_6379 start

Starting Redis server...

[root@centos02 ~]# netstat -anptu | grep redis-server

tcp 0 0 192.168.100.20:6379 0.0.0.0:* LISTEN 5098/redis-server 1

tcp 0 0 192.168.100.20:38371 192.168.100.10:6379 ESTABLISHED 5098/redis-server

3、修改第二台从 Redis 服务器配置文件

1)修改第二台从 Redis 主配置文件

[root@centos03 ~]# vim /etc/redis/6379.conf

62 bind 192.168.100.30

85 port 6379

129 daemonize yes

482 requirepass pwd@123

483 masterauth pwd@123

484 slaveof 192.168.100.10 6379

2)启动 Redis 服务

[root@centos03 ~]# /etc/init.d/redis_6379 start

Starting Redis server...

[root@centos03 ~]# netstat -anptu | grep redis-server

tcp 0 0 192.168.100.30:6379 0.0.0.0:* LISTEN 5182/redis-server 1

tcp 0 0 192.168.100.30:35434 192.168.100.10:6379 ESTABLISHED 5182/redis-server 1

4、查看配置主从复制

1)登录主 Redis 查看复制群集

[root@centos01 ~]# redis-cli -h 192.168.100.10 -a pwd@123 -p 6379

2)查看配置 Redis 复制群集

192.168.100.10:6379> info replication

\# Replication

role:master

connected_slaves:2

slave0:ip=192.168.100.20,port=6379,state=online,offset=2633,lag=0

slave1:ip=192.168.100.30,port=6379,state=online,offset=2633,lag=1

master_repl_offset:2633

repl_backlog_active:1

repl_backlog_size:1048576

repl_backlog_first_byte_offset:2

repl_backlog_histlen:2632

三、在 Centos 04 上 安装 Redis 配置 Redis 哨兵模式 ,监控主从 Redis节点运行状态,主Redis故障自动切换到新主Redis服务器

1、修改配置配置哨兵模式

1)修改 Redis 配置文件

[root@centos04 ~]# vim /usr/local/redis/sentinel.conf

16 bind 0.0.0.0

22 port 26379

70 sentinel monitor mymaster 192.168.100.10 6379 1

73 sentinel auth-pass mymaster pwd@123

100 sentinel down-after-milliseconds mymaster 30000

2)启动哨兵默认服务

[root@centos04 ~]# redis-sentinel /usr/local/redis/sentinel.conf &> /var/log/redis_sentinel.log &

[1] 5550

3)查看哨兵模式运行状态

[root@centos04 ~]# netstat -anptu | grep redis-sentinel

tcp 0 0 0.0.0.0:26379 0.0.0.0:* LISTEN 5550/redis-sentinel

tcp 0 0 192.168.100.40:52878 192.168.100.20:6379 ESTABLISHED 5550/redis-sentinel

tcp 0 0 192.168.100.40:56914 192.168.100.30:6379 ESTABLISHED 5550/redis-sentinel

tcp 0 0 192.168.100.40:34558 192.168.100.10:6379 ESTABLISHED 5550/redis-sentinel

tcp 0 0 192.168.100.40:60333 192.168.100.30:6379 ESTABLISHED 5550/redis-sentinel

tcp 0 0 192.168.100.40:40573 192.168.100.20:6379 ESTABLISHED 5550/redis-sentinel

tcp 0 0 192.168.100.40:58481 192.168.100.10:6379 ESTABLISHED 5550/redis-sentinel

2、模拟主 redis 数据库故障

1)停止主 Redis 服务

[root@centos01 ~]# redis-cli -h 192.168.100.10 -a pwd@123 -p 6379 shutdown

2)查看 redis-sentinel 监控

[root@centos04 ~]# tail -f /var/log/redis_sentinel.log

5550:X 21 Mar 23:16:39.488 * +failover-state-wait-promotion slave 192.168.100.30:6379 192.168.100.30 6379 @ mymaster 192.168.100.10 6379

5550:X 21 Mar 23:16:39.490 # +promoted-slave slave 192.168.100.30:6379 192.168.100.30 6379 @ mymaster 192.168.100.10 6379

5550:X 21 Mar 23:16:39.490 # +failover-state-reconf-slaves master mymaster 192.168.100.10 6379

5550:X 21 Mar 23:16:39.578 * +slave-reconf-sent slave 192.168.100.20:6379 192.168.100.20 6379 @ mymaster 192.168.100.10 6379

5550:X 21 Mar 23:16:40.511 * +slave-reconf-inprog slave 192.168.100.20:6379 192.168.100.20 6379 @ mymaster 192.168.100.10 6379

5550:X 21 Mar 23:16:40.511 * +slave-reconf-done slave 192.168.100.20:6379 192.168.100.20 6379 @ mymaster 192.168.100.10 6379

5550:X 21 Mar 23:16:40.600 # +failover-end master mymaster 192.168.100.10 6379

5550:X 21 Mar 23:16:40.600 # +switch-master mymaster 192.168.100.10 6379 192.168.100.30 6379

5550:X 21 Mar 23:16:40.601 * +slave slave 192.168.100.20:6379 192.168.100.20 6379 @ mymaster 192.168.100.30 6379

5550:X 21 Mar 23:16:40.601 * +slave slave 192.168.100.10:6379 192.168.100.10 6379 @ mymaster 192.168.100.30 6379

5550:X 21 Mar 23:17:10.659 # +sdown slave 192.168.100.10:6379 192.168.100.10 6379 @ mymaster 192.168.100.30 6379

Redis主从复制和哨兵模式_哨兵模式_05

3)登录到备份节点查看状态

[root@centos02 ~]# redis-cli -h 192.168.100.20 -a pwd@123 -p 6379

192.168.100.20:6379> info replication

\# Replication

role:slave

master_host:192.168.100.30

master_port:6379

master_link_status:up

master_last_io_seconds_ago:2

master_sync_in_progress:0

slave_repl_offset:15190

slave_priority:100

slave_read_only:1

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0

3、模拟故障节点启动

1)启动故障节点的 redis

[root@centos01 ~]# /etc/init.d/redis_6379 start

Starting Redis server...

[root@centos01 ~]# netstat -anptu | grep redis-server

tcp 0 0 192.168.100.10:6379 0.0.0.0:* LISTEN 5567/redis-server 1

tcp 0 0 192.168.100.10:6379 192.168.100.40:38628 ESTABLISHED 5567/redis-server 1

tcp 0 0 192.168.100.10:6379 192.168.100.40:45594 ESTABLISHED 5567/redis-server 1

2)查看主从信息

[root@centos01 ~]# redis-cli -h 192.168.100.10 -a pwd@123 -p 6379

192.168.100.10:6379> info replication

\# Replication

role:slave

master_host:192.168.100.30

master_port:6379

master_link_status:down

master_last_io_seconds_ago:-1

master_sync_in_progress:0

slave_repl_offset:1

master_link_down_since_seconds:1679412147

slave_priority:100

slave_read_only:1

connected_slaves:0

master_repl_offset:0

repl_backlog_active:0

repl_backlog_size:1048576

repl_backlog_first_byte_offset:0

repl_backlog_histlen:0



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3