Redis哨兵模式(sentinel)部署记录(主从复制、读写分离、主从切换)

时间:2021-12-18 07:48:21

部署环境: CentOS7.5 

192.168.94.11 (master)

192.168.94.22 (slave0)

192.168.94.33 (slave1)

192.168.94.44 (slave2)

所有节点都需要安装redis+sentinel

[root@master ~]# tar xf redis-4.0..tar.gz -C  /usr/local/src/
[root@master ~]# cd /usr/local/src/redis-4.0./
[root@master redis-4.0.]# make && make PREFIX=/usr/local/redis install
[root@master redis-4.0.]# cd /usr/local/redis/
[root@master redis]# mkdir run conf log dir
[root@master redis]# ln -s /usr/local/redis/bin/* /usr/local/bin/

所有节点修改redis和sentinel配置文件

[root@master redis]# cp /usr/local/src/redis-4.0./redis.conf conf/
bind 0.0.0.0
protected-mode no
port
tcp-backlog
timeout
tcp-keepalive
daemonize yes
supervised no
pidfile /usr/local/redis/run/redis_6379.pid
loglevel notice
logfile "/usr/local/redis/log/redis_6379.log"
databases
always-show-logo yes
save
save
save
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /usr/local/redis/dir
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay
repl-disable-tcp-nodelay no
slave-priority
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit
slowlog-log-slower-than
slowlog-max-len
latency-monitor-threshold
notify-keyspace-events ""
hash-max-ziplist-entries
hash-max-ziplist-value
list-max-ziplist-size -
list-compress-depth
set-max-intset-entries
zset-max-ziplist-entries
zset-max-ziplist-value
hll-sparse-max-bytes
activerehashing yes
client-output-buffer-limit normal
client-output-buffer-limit slave 256mb 64mb
client-output-buffer-limit pubsub 32mb 8mb
hz
aof-rewrite-incremental-fsync yes
# slave上要添加一条: slaveof 192.168.94.11 [root@master redis]# cp /usr/local/src/redis-4.0./sentinel.conf conf/
[root@master ~]# grep -Ev "^#|^$" /usr/local/redis/conf/sentinel.conf
port
pidfile "/usr/local/redis/run/redis-sentinel.pid"
dir "/usr/local/redis/dir/"
daemonize yes
protected-mode no
logfile "/usr/local/redis/log/redis-sentinel.log"
sentinel monitor mymaster 192.168.94.11
sentinel down-after-milliseconds mymaster
sentinel parallel-syncs mymaster
sentinel failover-timeout mymaster

添加redis到系统服务

[root@master redis]# cp /usr/local/src/redis-4.0./utils/redis_init_script /etc/init.d/redis
[root@master ~]# vim /etc/init.d/redis
#!/bin/sh
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem. ### BEGIN INIT INFO
# Provides: redis_6379
# Default-Start:
# Default-Stop:
# Short-Description: Redis data structure server
# Description: Redis data structure server. See https://redis.io
### END INIT INFO REDISPORT=
EXEC=/usr/local/bin/redis-server
CLIEXEC=/usr/local/bin/redis-cli
PIDFILE=/usr/local/redis/run/redis_${REDISPORT}.pid
CONF="/usr/local/redis/conf/redis.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac [root@master redis]# chkconfig --add redis
[root@master redis]# systemctl daemon-reload

所有节点启动redis、sentinel服务器

[root@master redis]# systemctl start redis
[root@master redis]# systemctl status redis
● redis.service - LSB: Redis data structure server
Loaded: loaded (/etc/rc.d/init.d/redis; bad; vendor preset: disabled)
Active: active (running) since 三 -- :: CST; 3s ago
Docs: man:systemd-sysv-generator()
Process: ExecStart=/etc/rc.d/init.d/redis start (code=exited, status=/SUCCESS)
CGroup: /system.slice/redis.service
└─ /usr/local/bin/redis-server 0.0.0.0: 9月 :: master systemd[]: Starting LSB: Redis data structure server...
9月 :: master redis[]: Starting Redis server...
9月 :: master systemd[]: Started LSB: Redis data structure server. [root@master redis]# redis-sentinel /usr/local/redis/conf/sentinel.conf
[root@master redis]# netstat -antup|grep
tcp 0.0.0.0: 0.0.0.0:* LISTEN /redis-sentine
tcp6 ::: :::* LISTEN /redis-sentine

查看redis和sentinel信息

# 查看节点redis的主从关系
[root@master redis]# redis-cli -h 192.168.94.11 -p info|grep role
role:master
[root@master redis]# redis-cli -h 192.168.94.22 -p info|grep role
role:slave
[root@master redis]# redis-cli -h 192.168.94.33 -p info|grep role
role:slave
[root@master redis]# redis-cli -h 192.168.94.44 -p info|grep role
role:slave # 查看master节点信息
[root@master redis]# redis-cli -h 192.168.94.11 -p info Replication
# Replication
role:master
connected_slaves:
slave0:ip=192.168.94.33,port=,state=online,offset=,lag=
slave1:ip=192.168.94.44,port=,state=online,offset=,lag=
slave2:ip=192.168.94.22,port=,state=online,offset=,lag=
master_replid:bf73c242b680e63743aa9d46b510ad1aee56105e
master_replid2:
master_repl_offset:
second_repl_offset:-
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen: # 查看master的sentinel配置文件 , 末尾被写入如下内容
[root@master redis]# grep -Ev '^#|^$' /usr/local/redis/conf/sentinel.conf
port
dir "/usr/local/redis/dir"
sentinel myid 2b92a48338bcd05968782fb3e4af432ad388e1c6
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 192.168.94.11
sentinel down-after-milliseconds mymaster
sentinel failover-timeout mymaster
pidfile "/usr/local/redis/run/redis_sentinel.pid"
logfile "/usr/local/redis/log/redis_sentinel.log"
daemonize yes
sentinel config-epoch mymaster
sentinel leader-epoch mymaster
sentinel known-slave mymaster 192.168.94.33
sentinel known-slave mymaster 192.168.94.22
sentinel known-slave mymaster 192.168.94.44
sentinel known-sentinel mymaster 192.168.94.33 cd7c8822acb184eb1af4f96710fc93f5073cfc65
sentinel known-sentinel mymaster 192.168.94.44 be0477f4855b523160d2e487172b94dc1a4c6181
sentinel known-sentinel mymaster 192.168.94.22 f464afa0fb23eb3561e2a448b10a01cdee130d06
sentinel current-epoch # 查看slave0的节点信息
[root@slave0 ~]# redis-cli -h 192.168.94.33 -p info Replication
# Replication
role:slave
master_host:192.168.94.11
master_port:
master_link_status:up
master_last_io_seconds_ago:
master_sync_in_progress:
slave_repl_offset:
slave_priority:
slave_read_only:
connected_slaves:
master_replid:bf73c242b680e63743aa9d46b510ad1aee56105e
master_replid2:
master_repl_offset:
second_repl_offset:-
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen: # 查看slave的sentinel配置文件 , 在末尾处被写入如下内容
[root@slave0 ~]# grep -Ev '^#|^$' /usr/local/redis/conf/sentinel.conf
port
dir "/usr/local/redis/dir"
sentinel myid cd7c8822acb184eb1af4f96710fc93f5073cfc65
sentinel deny-scripts-reconfig yes
sentinel monitor mymaster 192.168.94.11
sentinel down-after-milliseconds mymaster
sentinel failover-timeout mymaster
pidfile "/usr/local/redis/run/redis_sentinel.pid"
logfile "/usr/local/redis/log/redis_sentinel.log"
daemonize yes
sentinel config-epoch mymaster
sentinel leader-epoch mymaster
sentinel known-slave mymaster 192.168.94.22
sentinel known-slave mymaster 192.168.94.33
sentinel known-slave mymaster 192.168.94.44
sentinel known-sentinel mymaster 192.168.94.22 f464afa0fb23eb3561e2a448b10a01cdee130d06
sentinel known-sentinel mymaster 192.168.94.11 2b92a48338bcd05968782fb3e4af432ad388e1c6
sentinel known-sentinel mymaster 192.168.94.44 be0477f4855b523160d2e487172b94dc1a4c6181
sentinel current-epoch
# 其他slave节点也是一样的

测试客户端写入数据

[root@master redis]# redis-cli -h 192.168.94.11 -p
192.168.94.11:> set mywife limingming
OK
192.168.94.11:> get mywife
"limingming"
192.168.94.11:> exit
# 客户端连接任意slave节点get上面的数据
[root@master redis]# redis-cli -h 192.168.94.22 -p
192.168.94.22:> get mywife
"limingming"
192.168.94.22:> set limingming xiaoxiannv
(error) READONLY You can't write against a read only slave. # 测试可见 , master节点可以写入 , slave节点默认是只读 ,不能写入
到这就已经实现主从复制 , 读写分离了

通过sentinel实现主从切换 , sentinel需要做高可用防止单点

# 关闭任意slave节点 , 查看状态
[root@master redis]# redis-cli -h 192.168.94.22 -p
192.168.94.22:> get mywife
"limingming"
192.168.94.22:> SHUTDOWN
not connected> exit # 查看剩余节点信息
[root@master redis]# redis-cli -h 192.168.94.11 -p info replication
# Replication
role:master
connected_slaves: # 由3变成2了 , 减少了一台slave
slave0:ip=192.168.94.33,port=,state=online,offset=,lag=
slave1:ip=192.168.94.44,port=,state=online,offset=,lag=
master_replid:bf73c242b680e63743aa9d46b510ad1aee56105e
master_replid2:
master_repl_offset:
second_repl_offset:-
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen: # 在任意节点查看sentinel日志 , 发现slave已经是sdown状态
[root@master redis]# tailf /usr/local/redis/log/redis_sentinel.log
:X Sep ::26.759 * +sentinel sentinel f464afa0fb23eb3561e2a448b10a01cdee130d06 192.168.94.22 @ mymaster 192.168.94.11
:X Sep ::27.287 * +sentinel sentinel be0477f4855b523160d2e487172b94dc1a4c6181 192.168.94.44 @ mymaster 192.168.94.11
:X Sep ::27.551 * +sentinel sentinel cd7c8822acb184eb1af4f96710fc93f5073cfc65 192.168.94.33 @ mymaster 192.168.94.11
:X Sep ::35.097 * +slave slave 192.168.94.33: 192.168.94.33 @ mymaster 192.168.94.11
:X Sep ::35.098 * +slave slave 192.168.94.44: 192.168.94.44 @ mymaster 192.168.94.11
:X Sep ::36.796 # +sdown sentinel f464afa0fb23eb3561e2a448b10a01cdee130d06 192.168.94.22 @ mymaster 192.168.94.11
:X Sep ::37.327 # +sdown sentinel be0477f4855b523160d2e487172b94dc1a4c6181 192.168.94.44 @ mymaster 192.168.94.11
:X Sep ::37.560 # +sdown sentinel cd7c8822acb184eb1af4f96710fc93f5073cfc65 192.168.94.33 @ mymaster 192.168.94.11
:X Sep ::45.100 * +slave slave 192.168.94.22: 192.168.94.22 @ mymaster 192.168.94.11
:X Sep ::46.255 # +sdown slave 192.168.94.22: 192.168.94.22 @ mymaster 192.168.94.11 # 然后重启上面被关闭的节点 , 所有节点的sentinel都可以检测到slave已处于可用状态,此时再来查看节点的主从信息
[root@master redis]# redis-cli -h 192.168.94.11 -p info replication
# Replication
role:master
connected_slaves:
slave0:ip=192.168.94.33,port=,state=online,offset=,lag=
slave1:ip=192.168.94.44,port=,state=online,offset=,lag=
slave2:ip=192.168.94.22,port=,state=online,offset=,lag=
master_replid:bf73c242b680e63743aa9d46b510ad1aee56105e
master_replid2:
master_repl_offset:
second_repl_offset:-
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen: # 日志已经显示slave进入-sdown状态
[root@master redis]# tailf /usr/local/redis/log/redis_sentinel.log
:X Sep ::26.759 * +sentinel sentinel f464afa0fb23eb3561e2a448b10a01cdee130d06 192.168.94.22 @ mymaster 192.168.94.11
:X Sep ::27.287 * +sentinel sentinel be0477f4855b523160d2e487172b94dc1a4c6181 192.168.94.44 @ mymaster 192.168.94.11
:X Sep ::27.551 * +sentinel sentinel cd7c8822acb184eb1af4f96710fc93f5073cfc65 192.168.94.33 @ mymaster 192.168.94.11
:X Sep ::35.097 * +slave slave 192.168.94.33: 192.168.94.33 @ mymaster 192.168.94.11
:X Sep ::35.098 * +slave slave 192.168.94.44: 192.168.94.44 @ mymaster 192.168.94.11
:X Sep ::36.796 # +sdown sentinel f464afa0fb23eb3561e2a448b10a01cdee130d06 192.168.94.22 @ mymaster 192.168.94.11
:X Sep ::37.327 # +sdown sentinel be0477f4855b523160d2e487172b94dc1a4c6181 192.168.94.44 @ mymaster 192.168.94.11
:X Sep ::37.560 # +sdown sentinel cd7c8822acb184eb1af4f96710fc93f5073cfc65 192.168.94.33 @ mymaster 192.168.94.11
:X Sep ::45.100 * +slave slave 192.168.94.22: 192.168.94.22 @ mymaster 192.168.94.11
:X Sep ::46.255 # +sdown slave 192.168.94.22: 192.168.94.22 @ mymaster 192.168.94.11
:X Sep ::01.203 * +reboot slave 192.168.94.22: 192.168.94.22 @ mymaster 192.168.94.11
:X Sep ::01.261 # -sdown slave 192.168.94.22: 192.168.94.22 @ mymaster 192.168.94.11

关掉master节点 , 观察slave节点

[root@master ~]# redis-cli -h 192.168.94.11 -p
192.168.94.11:> SHUTDOWN # 在任意节点查看sentinel日志 , 发现master已经是+sdown的状态
[root@slave2 ~]# tailf /usr/local/redis/log/redis_sentinel.log
:X Sep ::30.806 * +slave-reconf-done slave 192.168.94.33: 192.168.94.33 @ mymaster 192.168.94.11
:X Sep ::30.869 * +slave-reconf-sent slave 192.168.94.22: 192.168.94.22 @ mymaster 192.168.94.11
:X Sep ::31.821 * +slave-reconf-inprog slave 192.168.94.22: 192.168.94.22 @ mymaster 192.168.94.11
:X Sep ::31.821 * +slave-reconf-done slave 192.168.94.22: 192.168.94.22 @ mymaster 192.168.94.11
:X Sep ::31.910 # +failover-end master mymaster 192.168.94.11
:X Sep ::31.911 # +switch-master mymaster 192.168.94.11 192.168.94.44
:X Sep ::31.911 * +slave slave 192.168.94.33: 192.168.94.33 @ mymaster 192.168.94.44
:X Sep ::31.911 * +slave slave 192.168.94.22: 192.168.94.22 @ mymaster 192.168.94.44
:X Sep ::31.911 * +slave slave 192.168.94.11: 192.168.94.11 @ mymaster 192.168.94.44
:X Sep ::41.995 # +sdown slave 192.168.94.11: 192.168.94.11 @ mymaster 192.168.94.44 [root@slave2 ~]# redis-cli -h 192.168.94.22 -p info |grep role
role:slave
[root@slave2 ~]# redis-cli -h 192.168.94.33 -p info |grep role
role:slave
[root@slave2 ~]# redis-cli -h 192.168.94.44 -p info |grep role
role:master [root@slave2 ~]# redis-cli -h 192.168.94.44 -p info replication
# Replication
role:master
connected_slaves:
slave0:ip=192.168.94.33,port=,state=online,offset=,lag=
slave1:ip=192.168.94.22,port=,state=online,offset=,lag=
master_replid:6b197e5e8a9f6b146558a21552e7eb9f30926289
master_replid2:086f725685de634b2b3efd546d150a370e461d07
master_repl_offset:
second_repl_offset:
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen: # 可以看出 , 当master节点(192.168.94.11)的redis关闭后 , slave节点(192.168.94.44)变成了新的master # 在这个新的master上进行写的操作是可以执行的
[root@slave1 ~]# redis-cli -h 192.168.94.44 -p
192.168.94.44:> set mywife limingming
OK
192.168.94.44:> get mywife
"limingming" # 重启192.168.94.11节点的redis , 等待所有节点的sentinel都检测到后 ,查看所有节点的主从信息 , 现有的master节点是不会被强占去的 , 而原来192.168.94.11节点的角色由master变成了slave
[root@slave1 ~]# redis-cli -h 192.168.94.44 -p info | grep role
role:master
[root@slave1 ~]# redis-cli -h 192.168.94.22 -p info | grep role
role:slave
[root@slave1 ~]# redis-cli -h 192.168.94.33 -p info | grep role
role:slave
[root@slave1 ~]# redis-cli -h 192.168.94.11 -p info | grep role
role:slave [root@slave1 ~]# redis-cli -h 192.168.94.44 -p info replication
# Replication
role:master
connected_slaves:
slave0:ip=192.168.94.33,port=,state=online,offset=,lag=
slave1:ip=192.168.94.22,port=,state=online,offset=,lag=
slave2:ip=192.168.94.11,port=,state=online,offset=,lag=
master_replid:6b197e5e8a9f6b146558a21552e7eb9f30926289
master_replid2:086f725685de634b2b3efd546d150a370e461d07
master_repl_offset:
second_repl_offset:
repl_backlog_active:
repl_backlog_size:
repl_backlog_first_byte_offset:
repl_backlog_histlen:
# 所有slave将同步新的master上的信息
主从故障切换已经实现了