最近看了Redis自动故障转移Sentinel(哨兵),故将笔记记在次处
注意,实际环境中是,多台机器,本文只是测试,所以在一台机器上配置多个Redis实例,以达到实验的目的
Redis Sentinel(哨兵)
7000配置文件
port 7000
daemonize yes
pidfile /var/run/redis-7000.pid
logfile "7000.log"
dbfilename "dump-7000.rdb"
appendonly yes
appendfilename "appendonly-7000.aof"
dir "/data/redis"
配置文件编写
sed 's/7000/7001/g' redis-7000.conf >>redis-7001.conf
sed 's/7000/7002/g' redis-7000.conf >>redis-7002.conf
cat redis-7001.conf
cat redis-7002.conf
cat redis-7000.conf
设置主从关系
echo "slaveof 192.168.1.108 7000" >>redis-7001.conf
echo "slaveof 192.168.1.108 7000" >>redis-7002.conf
启动三台redis server
redis-server /etc/redis/redis-7000.conf
redis-server /etc/redis/redis-7002.conf
redis-server /etc/redis/redis-7001.conf
ps -ef | grep redis
查看redis 7000端口的复制信息
PHP
127.0.0.1:7000> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.108,port=7001,state=online,offset=43,lag=1
slave1:ip=192.168.1.108,port=7002,state=online,offset=43,lag=1
master_repl_offset:43
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:42
配置sentinel
vim redis-sentinel-26379.conf
port 26379
daemonize yes
pidfile /var/run/redis-26379.pid
logfile "26379.log"
dir /data/redis
sentinel monitor mymaster 192.168.1.108 7000 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
生成配置文件
sed 's/26379/26380/g' redis-sentinel-26379.conf >> redis-sentinel-26380.conf
sed 's/26379/26381/g' redis-sentinel-26379.conf >> redis-sentinel-26381.conf
启动哨兵
PHP
[root@CentOS redis]# redis-sentinel redis-sentinel-26379.conf
[root@CentOS redis]# redis-sentinel redis-sentinel-26380.conf
[root@CentOS redis]# redis-sentinel redis-sentinel-26381.conf
[root@CentOS redis]# ps -ef | grep redis-sentinel| grep -v 'grep'
root 2677 1 0 02:36 ? 00:00:00 redis-sentinel *:26379 [sentinel]
root 2681 1 0 02:36 ? 00:00:00 redis-sentinel *:26380 [sentinel]
root 2685 1 0 02:36 ? 00:00:00 redis-sentinel *:26381 [sentinel]
接下来可以测试哨兵的功能了,代码太多就不粘贴上来了,至此Redis自动故障转移简单模型已经完成