• notice
  • Congratulations on the launch of the Sought Tech site

Using Redis Sentinel to realize automatic redis master-slave switch

1.Environmental preparation

Implementation of three hosts: ensure that the redis versions of the three servers are the same, host1 is the main service, and the others are slave
host1 192.168.1.9 master
host2
192.168.1.106 slave1 host3 192.168.1.110 slave2

2.Configure the sentinel configuration file sentinel.conf for host1

[root@localhost ~]# vim /app/redis/etc/sentinel.conf
bind 192.168.1.9
port 6379
daemonize yes
logfile "sentinel_26379.log"
dir "/app/redis/log"
sentinel monitor mymaster 192.168.1.9 6379 2 # When two or more sentinels determine that the master is down, a new master will be elected
sentinel auth-pass mymaster 123456
sentinel down-after-milliseconds mymaster 30000 # subjective offline time
sentinel parallel-syncs mymaster 1 #Sync data to the new master The number of slaves, the smaller the number, the longer the total synchronization time
sentinel failover-timeout mymaster 180000 #The timeout time required for all slaves to point to the new master
sentinel deny-scripts-reconfig yes

3.Configure the sentinel configuration file sentinel.conf of host2, just modify the bound ip and the others are the same as the main

[root@localhost ~]# vim /app/redis/etc/sentinel.conf
bind 192.168.1.106 
……
……

4.Configure the sentinel configuration file sentinel.conf for host3, just modify the bound ip and the others are the same as the main

[root@localhost ~]# vim /app/redis/etc/sentinel.conf
bind 192.168.1.110 
……
……

5.Point the main service of host2 and host3 to host1 on the command line

host2:
[root@localhost ~]# redis-cli
127.0.0.1:6379> auth 12345
OK
127.0.0.1:6379> slaveof 192.168.1.9 6379
127.0.0.1:6379> config set masterauth 123456
127.0.0.1:6379> info replication
#Replication
role:master
connected_slaves:1
slave:ip=192.168.1.106,port=6379,state=online,offset=1541332,lag=1
……
……

host3:
[root@localhost ~]# redis-cli
127.0.0.1:6379> auth 12345
OK
127.0.0.1:6379> slaveof 192.168.1.9 6379
127.0.0.1:6379> config set masterauth 123456
127.0.0.1:6379> info replication
#Replication
role:master
connected_slaves:1
slave0:ip=192.168.1.106,port=6379,state=online,offset=1541332,lag=1
slave1:ip=192.168.1.110,port=6379,state=online,offset=1541332 ,lag=1
……
……

6.Three services open the sentry service at the same time

[root@localhost ~]# redis-server /app/redis/etc/redis.conf

7.Hang the host1 main server to simulate a downtime test, and test whether one of the salves will be automatically promoted to the main service

hsot1 server:

[root@localhost ~]# ss -tnlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port             
LISTEN 0 511 192.168.1.9:26379 *:*       
users:(("redis-sentinel",pid=2413, fd=6))
LISTEN 0 511 192.168.1.9:6379 *:* 
users:(("redis-server",pid=2369,fd=7))
LISTEN 0 511 127.0.0.1:6379 *:*                 
 users:(( "redis-server",pid=2369,fd=6))

root@localhost ~]# kill -9 2413

host2 server: You can see that this service has been promoted to the primary service
127.0.0.1:6379> info replication
#Keyspace
db0:keys=7,expires=0,avg_ttl=0
127.0.0.1:6379> info replication
#Replication
role:master # role switching based
connected_slaves: 1 # from the parent when the number of connected services
slave0: IP = 192.168.1.110, Port = 6379, State = Online, offset = 1728348, LAG.1 =
master_replid: 7c8eb18c7c05b8f2d9f29028d016f9c40e8c2ce0
master_replid2: a8efce354ba8249ff264dcba60ac21030253b829
master_repl_offset: 1728348
second_repl_offset: 63848
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:679773
repl_backlog_histlen:1048576

host2 server: The master at this time is designated as host2 server
127.0.0.1:6379> info replication
#Replication
role:slave
master_host:192.168.1.106 #The main service is host2
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:1758995
slave_priority :100
slave_read_only:1
……
……

8.When the host1 service returns to normal and restarts, it will be automatically switched to the slave service

1) Modify the redis.conf configuration file before restarting and add the authentication password to connect to the main service
[root@localhost ~]# vim /app/redis/etc/redis.conf
 masterauth 123456
2) Restart the redis service of host1
[ root@localhost ~]# redis-server /app/redis/etc/redis.conf
3) Check its status, you can see the switched slave service
127.0.0.1:6379> info replication
#Replication
role:slave
master_host :192.168.1.106 #The main service points to host2
master_port:6379
master_link_status:up
master_last_io_seconds_ago:0
master_sync_in_progress:0
slave_repl_offset:1829345
slave_priority:100
slave_read_only:1
……
……

9.Check the connection status after host2 is promoted to the new master

127.0.0.1:6379> info replication
#Replication
role:master
connected_slaves:2
slave0:ip=192.168.1.110,port=6379,state=online,offset=1854211,lag=0
slave1:ip=192.168.1.9,port=6379 , State = Online, offset = 1854072, LAG.1 =
master_replid: 7c8eb18c7c05b8f2d9f29028d016f9c40e8c2ce0
master_replid2: a8efce354ba8249ff264dcba60ac21030253b829
master_repl_offset: 1,854,211
second_repl_offset: 63848
......
......


Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+