Redis sentinel mode configuration

1 Environmental resources

Virtual machine server IP
Master 192.168.51.4
Slave1 192.168.51.5
Slave2 192.168.51.6

2 Configure sentry

2.1 master

2.1.1 Modify the configuration file

[root@localhost ~]# cd /usr/local/redis/
[root@localhost redis]# ll
总用量 68
-rw-r--r--. 1 root root 61847 2月   8 17:14 6379.conf
drwxr-xr-x. 2 root root   134 9月  24 2017 bin
-rw-r--r--. 1 root root   854 2月  16 14:00 sentinel.conf
drwxr-xr-x. 2 root root    44 2月  15 22:02 working
[root@localhost redis]#  vi sentinel.conf
# Base configuration
# 端口号
port 26379
# pid文件位置
pidfile "/usr/local/redis/sentinel/redis-sentinel.pid"
# 工作目录
dir "/usr/local/redis/sentinel"
# 是否后台运行
daemonize yes
# 是否启用安全模式(非外部访问)
protected-mode no
# 日志文件位置
logfile "/usr/local/redis/sentinel/redis-sentinel.log"

# Core configuration
# 配置哨兵
sentinel monitor auskat-master 192.168.51.4 6379 2
# 密码
sentinel auth-pass auskat-master auskat
# master被sentinel认定为失效的间隔时间
sentinel down-after-milliseconds auskat-master 30000
# 剩余的slaves重新和新的master做同步的并行个数
sentinel parallel-syncs auskat-master 1
# 主备切换的超时时间,哨兵要去做故障转移,这个时候哨兵也是一个进程,如果他没有去执行,超过这个时间后,会由其他的哨兵来处理
sentinel failover-timeout auskat-master 180000

2.1.2 Create Directory

[root@localhost redis]# mkdir /usr/local/redis/sentinel -p

2.1.3 Start Sentry

[root@localhost redis]# redis-sentinel sentinel.conf
[root@localhost redis]# ps -ef | grep redis
root      16596      1  0 07:38 ?        00:00:42 /usr/local/bin/redis-server 0.0.0.0:6379
root      18725      1  2 14:11 ?        00:00:00 redis-sentinel *:26379 [sentinel]
root      18730  18414  0 14:12 pts/1    00:00:00 grep --color=auto redis

2.1.4 Copy configuration file

If scp is not installed, execute the command yum -y install scp

Copy toslave1

[root@localhost redis]# scp sentinel.conf  [email protected]:/usr/local/redis
[email protected]'s password: 
sentinel.conf 

Copy toslave2

[root@localhost redis]# scp sentinel.conf  [email protected]:/usr/local/redis
[email protected]'s password: 
sentinel.conf 

2.2 slave1

2.2.1 Create a directory

[root@localhost ~]# cd /usr/local/redis/
[root@localhost redis]# mkdir /usr/local/redis/sentinel -p

2.2.2 Start Sentry

[root@localhost redis]# redis-sentinel sentinel.conf
[root@localhost redis]# ps -ef | grep redis
root      16596      1  0 07:38 ?        00:00:42 /usr/local/bin/redis-server 0.0.0.0:6379
root      18725      1  2 14:11 ?        00:00:00 redis-sentinel *:26379 [sentinel]
root      18730  18414  0 14:12 pts/1    00:00:00 grep --color=auto redis

2.3 slave2

2.3.1 Create Directory

[root@localhost ~]# cd /usr/local/redis/
[root@localhost redis]# mkdir /usr/local/redis/sentinel -p

2.3.2 Start Sentry

[root@localhost redis]# redis-sentinel sentinel.conf
[root@localhost redis]# ps -ef | grep redis
root      16596      1  0 07:38 ?        00:00:42 /usr/local/bin/redis-server 0.0.0.0:6379
root      18725      1  2 14:11 ?        00:00:00 redis-sentinel *:26379 [sentinel]
root      18730  18414  0 14:12 pts/1    00:00:00 grep --color=auto redis

3 Function test

3.1 View sentinel information

master机器上连接redis,查看信息

[root@localhost ~]# redis-cli
127.0.0.1:6379> auth auskat
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.51.6,port=6379,state=online,offset=55875,lag=0
slave1:ip=192.168.51.5,port=6379,state=online,offset=55875,lag=0
master_replid:89019a6d7bb98860f7d727d8361e778a6cad3e92
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:55875
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:55875
127.0.0.1:6379> 

slave1上连接redis,查看信息

[root@localhost redis]# redis-cli
127.0.0.1:6379> auth auskat
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.51.4
master_port:6379
master_link_status:up
master_last_io_seconds_ago:2
master_sync_in_progress:0
slave_repl_offset:519987
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:89019a6d7bb98860f7d727d8361e778a6cad3e92
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:519987
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:519987

slave2上连接redis,查看信息

[root@localhost redis]# redis-cli 
127.0.0.1:6379> auth auskat
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.51.4
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:528985
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:89019a6d7bb98860f7d727d8361e778a6cad3e92
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:528985
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1663
repl_backlog_histlen:527323

3.2 View log information

[root@localhost redis]# tail -f /usr/local/redis/sentinel/redis-sentinel.log
18724:X 16 Feb 2021 14:11:59.828 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18724:X 16 Feb 2021 14:11:59.828 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=18724, just started
18724:X 16 Feb 2021 14:11:59.828 # Configuration loaded
18725:X 16 Feb 2021 14:11:59.920 * Increased maximum number of open files to 10032 (it was originally set to 1024).
18725:X 16 Feb 2021 14:11:59.922 * Running mode=sentinel, port=26379.
18725:X 16 Feb 2021 14:11:59.922 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
18725:X 16 Feb 2021 14:11:59.932 # Sentinel ID is eb24234aa458e8cc09278fd35e0de1b2d05ec47a
18725:X 16 Feb 2021 14:11:59.932 # +monitor master auskat-master 192.168.51.4 6379 quorum 2
18725:X 16 Feb 2021 14:11:59.948 * +slave slave 192.168.51.6:6379 192.168.51.6 6379 @ auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:11:59.949 * +slave slave 192.168.51.5:6379 192.168.51.5 6379 @ auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:13:54.628 * +sentinel sentinel a22ea4b2dbb61c34f337b533bbb2e04e376bbdaa 192.168.51.5 26379 @ auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:14:19.326 * +sentinel sentinel 8f7db63bce26663c1d0c2616024ac66fa9d560c4 192.168.51.6 26379 @ auskat-master 192.168.51.4 6379

3.3 Switch master

3.3.1 Stop master

[root@localhost ~]# /etc/init.d/redis_init_script stop
Stopping ...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Waiting for Redis to shutdown ...
Redis stopped

日志信息

18725:X 16 Feb 2021 14:55:55.371 # +sdown master auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:55.443 # +odown master auskat-master 192.168.51.4 6379 #quorum 2/2
18725:X 16 Feb 2021 14:55:55.443 # +new-epoch 1
18725:X 16 Feb 2021 14:55:55.443 # +try-failover master auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:55.444 # +vote-for-leader eb24234aa458e8cc09278fd35e0de1b2d05ec47a 1
18725:X 16 Feb 2021 14:55:55.446 # 8f7db63bce26663c1d0c2616024ac66fa9d560c4 voted for eb24234aa458e8cc09278fd35e0de1b2d05ec47a 1
18725:X 16 Feb 2021 14:55:55.492 # a22ea4b2dbb61c34f337b533bbb2e04e376bbdaa voted for eb24234aa458e8cc09278fd35e0de1b2d05ec47a 1
18725:X 16 Feb 2021 14:55:55.511 # +elected-leader master auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:55.511 # +failover-state-select-slave master auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:55.588 # +selected-slave slave 192.168.51.5:6379 192.168.51.5 6379 @ auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:55.588 * +failover-state-send-slaveof-noone slave 192.168.51.5:6379 192.168.51.5 6379 @ auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:55.641 * +failover-state-wait-promotion slave 192.168.51.5:6379 192.168.51.5 6379 @ auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:56.157 # +promoted-slave slave 192.168.51.5:6379 192.168.51.5 6379 @ auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:56.157 # +failover-state-reconf-slaves master auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:56.232 * +slave-reconf-sent slave 192.168.51.6:6379 192.168.51.6 6379 @ auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:56.588 # -odown master auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:57.169 * +slave-reconf-inprog slave 192.168.51.6:6379 192.168.51.6 6379 @ auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:58.205 * +slave-reconf-done slave 192.168.51.6:6379 192.168.51.6 6379 @ auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:58.288 # +failover-end master auskat-master 192.168.51.4 6379
18725:X 16 Feb 2021 14:55:58.289 # +switch-master auskat-master 192.168.51.4 6379 192.168.51.5 6379
18725:X 16 Feb 2021 14:55:58.289 * +slave slave 192.168.51.6:6379 192.168.51.6 6379 @ auskat-master 192.168.51.5 6379
18725:X 16 Feb 2021 14:55:58.289 * +slave slave 192.168.51.4:6379 192.168.51.4 6379 @ auskat-master 192.168.51.5 6379
18725:X 16 Feb 2021 14:56:28.329 # +sdown slave 192.168.51.4:6379 192.168.51.4 6379 @ auskat-master 192.168.51.5 6379

slave1信息

[root@localhost redis]# redis-cli
127.0.0.1:6379> auth auskat
OK
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.51.6,port=6379,state=online,offset=584942,lag=1
master_replid:c4e391b43553d7ad27c2ac68701e1d793ecae4f0
master_replid2:89019a6d7bb98860f7d727d8361e778a6cad3e92
master_repl_offset:585230
second_repl_offset:566952
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:585230

slave2 信息

[root@localhost redis]# redis-cli 
127.0.0.1:6379> auth auskat
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.51.5
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:582336
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:c4e391b43553d7ad27c2ac68701e1d793ecae4f0
master_replid2:89019a6d7bb98860f7d727d8361e778a6cad3e92
master_repl_offset:582336
second_repl_offset:566952
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1663
repl_backlog_histlen:580674

3.3.2 Start master

[root@localhost ~]# /etc/init.d/redis_init_script start
Starting Redis server...
18888:C 16 Feb 2021 15:01:50.343 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18888:C 16 Feb 2021 15:01:50.343 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=18888, just started
18888:C 16 Feb 2021 15:01:50.343 # Configuration loaded

原来的master

[root@localhost ~]# redis-cli
127.0.0.1:6379> auth auskat
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.51.5
master_port:6379
master_link_status:down
master_last_io_seconds_ago:-1
master_sync_in_progress:0
slave_repl_offset:1
master_link_down_since_seconds:1613459043
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:535fba18b992a3e677a777ad678d313a581ee991
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:0
second_repl_offset:-1
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

3.3.3 Solve the problem that the original master restores out of synchronization

master_link_status:down , The synchronization status is not ok

This is because we only set the masterauth of 5 and 6, which is used to synchronize the data of the master, but 4 is the master is not affected at the beginning, when the master is converted to a slave, because he does not have auth, he cannot re-start The master synchronizes the data, and the synchronization status is down when the info replication occurs, so you only need to modify the masterauth in redis.conf to auskat.

Generally, the check of master data cannot be synchronized to slave is as follows:

  • For network communication problems, ensure that each other can ping each other and the internal network can communicate.
  • Turn off the firewall and develop the corresponding port (it is recommended to turn off the firewall permanently in the virtual machine, and the intranet intercommunication must be ensured for the cloud server).
  • Unify all passwords, don't miss that a node is not set.

修改mater配置

[root@localhost redis]# vi /usr/local/redis/6379.conf
# masterauth <master-password>
masterauth auskat
[root@localhost redis]# /etc/init.d/redis_init_script stop
Stopping ...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Waiting for Redis to shutdown ...
Redis stopped
[root@localhost redis]# /etc/init.d/redis_init_script start
Starting Redis server...
18913:C 16 Feb 2021 15:13:32.193 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18913:C 16 Feb 2021 15:13:32.193 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=18913, just started
18913:C 16 Feb 2021 15:13:32.193 # Configuration loaded

原master查看

18725:X 16 Feb 2021 14:55:58.289 * +slave slave 192.168.51.6:6379 192.168.51.6 6379 @ auskat-master 192.168.51.5 6379
18725:X 16 Feb 2021 14:55:58.289 * +slave slave 192.168.51.4:6379 192.168.51.4 6379 @ auskat-master 192.168.51.5 6379
18725:X 16 Feb 2021 14:56:28.329 # +sdown slave 192.168.51.4:6379 192.168.51.4 6379 @ auskat-master 192.168.51.5 6379
18725:X 16 Feb 2021 15:01:51.284 # -sdown slave 192.168.51.4:6379 192.168.51.4 6379 @ auskat-master 192.168.51.5 6379
18725:X 16 Feb 2021 15:13:32.481 * +reboot slave 192.168.51.4:6379 192.168.51.4 6379 @ auskat-master 192.168.51.5 6379
[root@localhost redis]# redis-cli
127.0.0.1:6379> auth auskat
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.51.5
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:845873
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:c4e391b43553d7ad27c2ac68701e1d793ecae4f0
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:845873
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:792365
repl_backlog_histlen:53509

现在的master查看

127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.51.6,port=6379,state=online,offset=808901,lag=0
slave1:ip=192.168.51.4,port=6379,state=online,offset=808757,lag=1
master_replid:c4e391b43553d7ad27c2ac68701e1d793ecae4f0
master_replid2:89019a6d7bb98860f7d727d8361e778a6cad3e92
master_repl_offset:808901
second_repl_offset:566952
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:808901

3.3.4 Summary

When the master goes down, due to sentinel monitoring, the remaining slaves will be elected, and one of the slaves will be promoted to master. When the down master is restored, it will automatically become a slave.

3.4 Sentinel information check

# 查看auskat-master下的master节点信息
sentinel master auskat-master
# 查看auskat-master下的slaves节点信息
sentinel slaves auskat-master
# 查看auskat-master下的哨兵节点信息
sentinel sentinels auskat-master

4 Related information

  • The blog post is not easy, everyone who has worked so hard to pay attention and praise, thank you

Guess you like

Origin blog.csdn.net/qq_15769939/article/details/113875863