前言
本环境是基于 Centos 7.8 系统构建Redis学习环境
具体构建,请参考 Redis-5.0.9环境部署
一、哨兵架构设计
二、环境准备
role | host | ip | Redis-Version |
---|---|---|---|
master | node01 | 192.168.5.11 | Redis-5.0.9 |
slave1 | node02 | 192.168.5.12 | Redis-5.0.9 |
slave2 | node03 | 192.168.5.13 | Redis-5.0.9 |
注:本环境基于redis-yum方式安装
三、配置主从同步
master
[root@node01 ~]# vim /etc/redis.conf
bind 192.168.5.11
daemonize yes
appendonly yes
[root@node01 ~]# systemctl enable --now redis
slave1
[root@node02 ~]# vim /etc/redis.conf
bind 192.168.5.12
daemonize yes
appendonly yes
replicaof 192.168.5.11 6379
[root@node02 ~]# systemctl enable --now redis
slave2
[root@node03 ~]# vim /etc/redis.conf
bind 192.168.5.13
daemonize yes
appendonly yes
replicaof 192.168.5.11 6379
[root@node03 ~]# systemctl enable --now redis
检查主从同步状态
master
slave1
slave2
主从同步配置成功!!!
四、配置哨兵服务
修改l配置文件
master
[root@node01 ~]# vim /etc/redis-sentinel.conf
daemonize yes
sentinel monitor mymaster 192.168.5.11 6379 2
[root@node01 ~]# systemctl enable --now redis-sentinel.service
Created symlink from /etc/systemd/system/multi-user.target.wants/redis-sentinel.service to /usr/lib/systemd/system/redis-sentinel.service.
[root@node01 ~]# netstat -lnutp | grep 6379
tcp 0 0 192.168.5.11:26379 0.0.0.0:* LISTEN 11911/redis-sentine
tcp 0 0 192.168.5.11:6379 0.0.0.0:* LISTEN 1698/redis-server 1
slave1
[root@node02 ~]# vim /etc/redis-sentinel.conf
bind 192.168.5.11
daemonize yes
sentinel monitor mymaster 192.168.5.11 6379 2
[root@node02 ~]# systemctl enable --now redis-sentinel.service
Created symlink from /etc/systemd/system/multi-user.target.wants/redis-sentinel.service to /usr/lib/systemd/system/redis-sentinel.service.
[root@node02 ~]# netstat -lnutp | grep 6379
tcp 0 0 192.168.5.12:26379 0.0.0.0:* LISTEN 11884/redis-sentine
tcp 0 0 192.168.5.12:6379 0.0.0.0:* LISTEN 1717/redis-server 1
slave2
[root@node03 ~]# vim /etc/redis-sentinel.conf
bind 192.168.5.13
daemonize yes
sentinel monitor mymaster 192.168.5.11 6379 2
[root@node03 ~]# systemctl enable --now redis-sentinel.service
Created symlink from /etc/systemd/system/multi-user.target.wants/redis-sentinel.service to /usr/lib/systemd/system/redis-sentinel.service.
[root@node03 ~]# netstat -lnutp | grep 6379
tcp 0 0 192.168.5.13:26379 0.0.0.0:* LISTEN 11791/redis-sentine
tcp 0 0 192.168.5.13:6379 0.0.0.0:* LISTEN 1648/redis-server 1
查看sentinel状态
追踪sentinel日志文件
查看sentinel信息
查看master sentinel信息
查看salve sentinel信息
五、模拟 sentinel 故障切换
停止master redis服务
[root@node01 ~]# systemctl stop redis
[root@node01 ~]# netstat -lnutp | grep redis
tcp 0 0 192.168.5.11:26379 0.0.0.0:* LISTEN 11911/redis-sentine
3min后查看状态主从状态
master切换到node02
追踪日志
查看主从状态
master写入、删除数据
故障切换成功!!!
启动node01 redis服务
[root@node01 ~]# systemctl start redis
[root@node01 ~]# netstat -lnutp | grep redis
tcp 0 0 192.168.5.11:6379 0.0.0.0:* LISTEN 12031/redis-server
tcp 0 0 192.168.5.11:26379 0.0.0.0:* LISTEN 11911/redis-sentine
查看主从状态
master写入数据,salve查看同步状况
master写入数据
node01查看
node03查看
故障转移、故障切换功能实现!!!
故障恢复成功(非抢占式选举角色)
注:master发送故障后,sentinel 通过更改节点配置文件的方式,切换校色的,从而达到故障转移、故障切换的要求
Redis 哨兵配置成功!!!