redis哨兵实现
原理:
基于一主两从架构实现哨兵
哨兵的前提是已经实现了一个redis的主从复制的运行环境,从而实现一个一主两从基于哨兵的高可用
redis架构
注意: master 的配置文件中masterauth 和slave 都必须相同
所有节点安装redis:
tar xf redis-5.0.7.tar.gz -C /usr/local/src/ cd /usr/local/src/redis-5.0.7 make PREFIX=/apps/redis install ll /apps/redis/bin/ echo 'PATH=/apps/redis/bin:$PATH' > /etc/profile.d/redis.sh . /etc/profile.d/redis.sh mkdir /apps/redis/{etc,log,data,run} cp /usr/local/src/redis-5.0.7/redis.conf /apps/redis/etc/
创建 redis 用户和数据目录
useradd -r -s /sbin/nologin redis chown -R redis.redis /apps/redis/
编辑 redis 服务启动文件
vim /usr/lib/systemd/system/redis.service [Unit] Description=Redis persistent key-value database After=network.target [Service] ExecStart=/apps/redis/bin/redis-server /apps/redis/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
scp /usr/lib/systemd/system/redis.service 10.0.0.18:/usr/lib/systemd/system/
scp /usr/lib/systemd/system/redis.service 10.0.0.28:/usr/lib/systemd/system/
验证 redis 启动
systemctl daemon-reload
systemctl start redis
systemctl status redis
所有主节点的redis.conf中关键配置
bind 0.0.0.0
masterauth "123456"
requirepass "123456"
logfile "/apps/redis/log/redis.log"
dir /apps/redis/data
scp /apps/redis/etc/redis.conf 10.0.0.18:/apps/redis/etc/
scp /apps/redis/etc/redis.conf 10.0.0.28:/apps/redis/etc/
配置master
配置slave1
REPLICAOF 10.0.0.8 6379 #启用主从同步复制,并把ip指向master(10.0.0.8)
配置slave2
编辑哨兵的配置文件
sentinel配置
Sentinel实际上是一个特殊的redis服务器,有些redis指令支持,但很多指令并不支持.默认监听在
26379/tcp端口.
哨兵可以不和Redis服务器部署在一起,但一般部署在一起,所有redis节点使用相同的以下示例的配置
文件
我这里才用的是源码编译安装,在源码目录有sentinel.conf,复制到安装目录即可
cp sentinel.conf /apps/redis/etc/
#如果是编译安装,在所有哨兵服务器执行下面操作启动哨兵
vi /apps/redis/etc/sentinel.conf #修改以下内容
bind 0.0.0.0
port 26379
daemonize yes
pidfile /apps/redis/run/redis-sentinel.pid
logfile "/apps/redis/log/sentinel_26379.log"
dir /apps/redis/data
sentinel monitor mymaster 10.0.0.8 6379 2
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 15000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
#修改sentinel.conf文件权限(3台主机都执行)
chown redis.redis /apps/redis/etc/sentinel.conf
#创建sentinel.service启动文件
vi /usr/lib/systemd/system/redis-sentinel.service
[Unit]
Description=Redis Sentinel
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
ExecStart=/apps/redis/bin/redis-sentinel /apps/redis/etc/sentinel.conf --supervised systemd
ExecStop=/apps/redis/bin/redis-shutdown redis-sentinel
Type=notify
User=redis
Group=redis
RuntimeDirectory=redis
RuntimeDirectoryMode=0755
[Install]
WantedBy=multi-user.target
scp /usr/lib/systemd/system/redis-sentinel.service 10.0.0.18:/usr/lib/systemd/system/
scp /usr/lib/systemd/system/redis-sentinel.service 10.0.0.28:/usr/lib/systemd/system/
#启动哨兵
[root@redis-master ~]#systemctl daemon-reload
[root@redis-master ~]#systemctl start redis-sentinel.service
确保每个哨兵主机的myid不同
[root@redis-slave1 ~]#vi /apps/redis/etc/sentinel.conf
sentinel myid 782765adb0402fd686313ff99f49862f3c7f3d9c
[root@redis-slave2 ~]#vi /apps/redis/etc/sentinel.conf
sentinel myid 782765adb0402fd686313ff99f49862f3c7f3d9d
[root@redis-master ~]#systemctl enable --now redis-sentinel.service
[root@redis-slave1 ~]#systemctl enable --now redis-sentinel.service
[root@redis-slave2 ~]#systemctl enable --now redis-sentinel.service
当前sentinel状态
在sentinel状态中尤其是最后一行,涉及到masterIP是多少,有几个slave,有几个sentinels,必须是
符合全部服务器数量
[root@redis-master ~]#redis-cli -p 26379 127.0.0.1:26379> INFO sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=10.0.0.8:6379,slaves=2,sentinels=3 #两个 slave,三个sentinel服务器
停止Redis Master测试故障转移
[root@redis-master ~]#killall redis-server
查看各节点上哨兵信息:
[root@redis-master ~]#redis-cli -a 123456 -p 26379 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:26379> INFO sentinel # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=10.0.0.18:6379,slaves=2,sentinels=2