为什么要部署哨兵,因为我们在使用redis主从复制的时候,如果主库宕机了我们应该怎么办,这个时候我们就想到了哨兵,哨兵是redis集群架构中非常重要的一个组件,主要的功能有以下几点
1.集群监控:负责监控redis master和slave进程是否正常工作。
2.消息通知:如果某个redis实例有故障,那么哨兵负责发送消息作为报警通知给管理员。
3.故障转移:如果master node挂掉了,会自动转移到slave node上。
4.配置中心:如果故障转移发生了,通知client客户端的master地址。
然后我们接下来开始操作。
首先我们跟前面redis主从复制一样在根目录建立配置文件的目录,建立三个分别是26379,26380,26381
[root@VM-4-6-centos redis]# ls
26379 26380 26381 6396 6397 6398
[root@VM-4-6-centos 26379]# pwd
/redis/26379
[root@VM-4-6-centos 26379]#
分别往配置文件写入相应的参数
port 26379
sentinel monitor sentinel79 服务器ip 6396 1
port 26380
sentinel monitor sentinel79 服务器ip 6396 1
port 26381
sentinel monitor sentinel79 47.94.223.116 6396 1
--port是指定哨兵的端口
--sentinel monitor 名称 服务器ip 要监听的端口 多少个哨兵认为宕机才会选举
创建哨兵的容器
docker run -itd -p 26398:6379 --name=sentinel98 --sysctl net.core.somaxconn=1024 -v /redis/26398/sentinel98.conf:/etc/sentinel.conf -v /root/usr/local/redis/data:/data -e TIME_ZONE="Asia/Shanghai" -e TZ="Asia/Shanghai" -d redis
docker run -itd -p 26397:6379 --name=sentinel97 --sysctl net.core.somaxconn=1024 -v /redis/26397/sentinel97.conf:/etc/sentinel.conf -v /root/usr/local/redis/data:/data -e TIME_ZONE="Asia/Shanghai" -e TZ="Asia/Shanghai" -d redis
docker run -itd -p 26396:6379 --name=sentinel97 --sysctl net.core.somaxconn=1024 -v /redis/26396/sentinel96.conf:/etc/sentinel.conf -v /root/usr/local/redis/data:/data -e TIME_ZONE="Asia/Shanghai" -e TZ="Asia/Shanghai" -d redis
我们进入刚刚创建好的容器,开启哨兵进行监听主库,将三个哨兵依次开启,然后我们将主库的容器关闭,进行选举
[root@VM-4-6-centos 26379]# docker exec -it 9d0 bash
root@9d047c608726:/data# redis-sentinel /etc/sentinel.conf
47:X 23 Sep 2022 22:38:59.706 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
47:X 23 Sep 2022 22:38:59.706 # Redis version=7.0.4, bits=64, commit=00000000, modified=0, pid=47, just started
47:X 23 Sep 2022 22:38:59.706 # Configuration loaded
47:X 23 Sep 2022 22:38:59.706 * monotonic clock: POSIX clock_gettime
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 7.0.4 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in sentinel mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 26381
| `-._ `._ / _.-' | PID: 47
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | https://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'47:X 23 Sep 2022 22:38:59.712 # Could not rename tmp config file (Device or resource busy)
47:X 23 Sep 2022 22:38:59.712 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy
47:X 23 Sep 2022 22:38:59.712 # Sentinel ID is 33a9e959634aebb112e6cf991cac5e47768a2233
47:X 23 Sep 2022 22:38:59.712 # +monitor master sentinel79 121.4.33.132 6396 quorum 1
[root@VM-4-6-centos 26379]# docker stop 33c
33c
[root@VM-4-6-centos 26379]#
当哨兵选举的时候出现以下情况证明选举成功
:X 23 Sep 2022 22:46:34.234 # Could not rename tmp config file (Device or resource busy)
36:X 23 Sep 2022 22:46:34.234 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy
36:X 23 Sep 2022 22:47:18.476 # +sdown master sentinel79 121.4.33.132 6396
36:X 23 Sep 2022 22:47:18.476 # +odown master sentinel79 121.4.33.132 6396 #quorum 1/1
36:X 23 Sep 2022 22:47:18.476 # +new-epoch 1
36:X 23 Sep 2022 22:47:18.476 # +try-failover master sentinel79 121.4.33.132 6396
36:X 23 Sep 2022 22:47:18.484 # Could not rename tmp config file (Device or resource busy)
36:X 23 Sep 2022 22:47:18.484 # WARNING: Sentinel was not able to save the new configuration on disk!!!: Device or resource busy
36:X 23 Sep 2022 22:47:18.484 # +vote-for-leader 239b12b82a52e28965e7258d57762fbeb8b1b4e4 1
36:X 23 Sep 2022 22:47:18.484 # 95b303107a21fa578476537036131af4d837250b voted for 95b303107a21fa578476537036131af4d837250b 1
36:X 23 Sep 2022 22:47:18.491 # cdb46edf39b844d95545a3dd69c60898495863fa voted for 95b303107a21fa578476537036131af4d837250b 1
36:X 23 Sep 2022 22:47:29.091 # -failover-abort-not-elected master sentinel79 121.4.33.132 6396
36:X 23 Sep 2022 22:47:29.153 # Next failover delay: I will not start a failover before Fri Sep 23 22:53:19 2022
36:X 23 Sep 2022 22:47:55.755 * +reboot master sentinel79 121.4.33.132 6396
36:X 23 Sep 2022 22:47:55.812 # -sdown master sentinel79 121.4.33.132 6396
36:X 23 Sep 2022 22:47:55.812 # -odown master sentinel79 121.4.33.132 6396
到这里我们使用Docker布置Redis主从和哨兵就布置完毕了。