Redis master-slave replication + sentinel sentinel

One, redis master-slave replication

Modify the configuration file

[root@master src]# vim /usr/local/redis/redis.conf
#master、slave节点都需要修改
#69行 修改监听地址为0.0.0.0(在实验环境使用),现网环境建议绑定从服务器IP地址
#136行 开启守护进程
daemonize yes
#171行 修改日志文件目录
logfile /var/log/redis_6379.log
#263行 修改工作目录
dir /var/lib/redis/6379
#699行 开启AOF持久化功能
appendonly yes

#slave node

#与master节点修改一致,多修改一个同步master节点IP和端口
replicaof 20.0.0.11 6379
服务重启
/etc/init.d/redis_6379 restart

Verify the master-slave effect

master上看日志
tail -f /var/log/redis_6379.log 
Replica 20.0.0.12:6379 asks for synchronization
Replica 20.0.0.13:6379 asks for synchronization

Verify the slave node on the master

127.0.0.1:6379> info replication
#Replication
role:master
connected_slaves:2
slave0:ip=20.0.0.12,port=6379,state=online,offset=1246,lag=0
slave1:ip=20.0.0.13,port=6379,state=online,offset=1246,lag=1

2. Sentinel mode

2.1 Principles of Sentinel Mode

  • The sentinel is a distributed system used to monitor each server in the master-slave structure. When a failure occurs, a new master is selected through a voting mechanism and all slaves are connected to the new master.
  • Therefore, the number of the entire cluster running sentinels must not be less than 3 nodes.
  • The role of sentinel mode
  1. Monitoring
    Constantly check whether the master and slave are running normally.
    Master survival detection, master and slave operation detection

  2. Notification (reminder)
    When there is a problem with the monitored server, send a notification to other (sentinel room, client).

  3. Automatic failover
    Disconnect the master and slave, select a slave as the master, connect other slaves to the new master, and inform the client of the new server address
    PS: The sentinel is also a redis server, but does not provide data services

The startup of the sentinel depends on the master-slave mode, so the sentinel mode must be installed after the master-slave mode is installed. The sentry mode needs to be deployed on all nodes.
The sentinel mode will monitor whether all redis working nodes are normal. When the master has a problem, because other nodes lose contact with the master node, it will vote. After the vote is half of the vote, it is considered that the master does have a problem, and then the sentry room will be notified, and then from Select one of the slaves as the new master

2.2 Sentinel configuration

Sentinel mode configuration

  1. Modification of the configuration file of sentinel mode###(All nodes need to be modified)
vi redis-5.0.4/sentinel.conf
17/protected-mode no  #关闭保护模式
26/daemonize yes #指定sentinel为后台启动
36/logfile "/var/log/sentinel.log" #指定日志存放路径
65/dir "/var/lib/redis/6379" #指定数据库存放路径
84/sentinel monitor mymaster 20.0.0.11 6379 2  #至少几个哨兵检测到主服务器故障了,才会进行故障迁移
113/sentinel down-after-milliseconds mymaster 3000 #判定服务器down掉的时间周期,默认30000毫秒(30秒)
146/sentinel failover-timeout mymaster 100000 #故障节的的最大超时时间为180000180秒)
  1. Activate sentinel mode
先启master,再启slave
redis-sentinel redis-5.0.4/sentinel.conf &
  1. View sentinel information
redis-cli -h 20.0.0.11 -p 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=20.0.0.11:6379,slaves=2,sentinels=3
  1. Failure simulation
  • View the redis-server process number
ps -ef | grep redis
root      57031      1  0 15:20 ?        00:00:07 /usr/local/bin/redis-server 0.0.0.0:6379
root      57742      1  1 16:05 ?        00:00:07 redis-sentinel *:26379 [sentinel]
root      57883  57462  0 16:17 pts/1    00:00:00 grep --color=auto redis
  • Kill the process ID of redis-server on the master
kill -9 57031 #master上redis-server的进程号
  • Verification results
    can be viewed in both ways
tail -f /var/log/sentinel.log    //查看日志文件
57742:X 07 Aug 2020 16:19:21.170 # +failover-state-select-slave master mymaster 20.0.0.12 6379
57742:X 07 Aug 2020 16:19:21.170 # -sdown slave 20.0.0.13:6379 20.0.0.13 6379 @ mymaster 20.0.0.12 6379
57742:X 07 Aug 2020 16:19:21.272 # +selected-slave slave 20.0.0.13:6379 20.0.0.13 6379 @ mymaster 20.0.0.12 6379
57742:X 07 Aug 2020 16:19:21.272 * +failover-state-send-slaveof-noone slave 20.0.0.13:6379 20.0.0.13 6379 @ mymaster 20.0.0.12 6379
57742:X 07 Aug 2020 16:19:21.338 * +failover-state-wait-promotion slave 20.0.0.13:6379 20.0.0.13 6379 @ mymaster 20.0.0.12 6379
57742:X 07 Aug 2020 16:19:21.402 # -failover-abort-slave-timeout master mymaster 20.0.0.12 6379
57742:X 07 Aug 2020 16:19:21.799 # -sdown master mymaster 20.0.0.12 6379
57742:X 07 Aug 2020 16:19:21.826 # +new-epoch 41
57742:X 07 Aug 2020 16:19:21.827 # +vote-for-leader b12178afd9f862e0ead00763c2c7f1ae7f5de22e 41
57742:X 07 Aug 2020 16:19:31.137 * +convert-to-slave slave 20.0.0.13:6379 20.0.0.13 6379 @ mymaster 20.0.0.12 6379
redis-cli -p 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=20.0.0.12:6379,slaves=2,sentinels=3

Guess you like

Origin blog.csdn.net/qq_46480020/article/details/111353748