Redis Redis implemented using the Sentinel is automatically switched from the master

1. Preparing the Environment

Three hosts to achieve: Make sure the version of the three servers redis to host1-based services, other from
host1 192.168.1.9 Master
host2 192.168.1.106 slave1
host3 192.168.1.110 slave2

2. Configure host1 sentinels profile sentinel.conf

[the root @ localhost ~] # Vim /app/redis/etc/sentinel.conf
the bind 192.168.1.9
Port 6379
to daemonize Yes
logfile "sentinel_26379.log"
the dir "/ App / Redis / log"
Sentinel Monitor mymaster 192.168.1.9 # 2 6379 when there are two or more sentinels conclude master downtime will elect a new master
Sentinel auth-Pass mymaster 123456
Sentinel Down-the After-# 30000 milliseconds mymaster subjective offline time
sentinel parallel-syncs mymaster 1 # sync data to the new master number slave, the smaller the number of the total synchronization time longer
sentinel failover - timeout mymaster 180000 # pointing to all slave timeout time required for the new Master
Sentinel deny-scripts-reconfig yes

3. Configure host2 sentinels profile sentinel.conf, just modify the binding of the other main same ip

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

Configuring host3 sentinel profile sentinel.conf, simply modify the binding of the other main same ip

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

5. In the command line of the primary serving host2 and point to host1 host3

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 service open simultaneously Sentinel Service

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

7. host1 main hang clothes outages simulation test, test whether there is a salve which will be automatically promoted to the primary 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 this service was promoted to the main service
127.0.0.1:6379> info Replication
#Keyspace
DB0: Keys = 7, the Expires = 0, = 0 avg_ttl
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: 679 773
repl_backlog_histlen: 1048576

host2 server: A main designated as host2 server
127.0.0.1:6379> info Replication
#Replication
Role: Slave
MASTER_HOST: 192.168.1.106 # main services host2
MASTER_PORT: 6379
master_link_status: up
master_last_io_seconds_ago: 0
master_sync_in_progress: 0
slave_repl_offset: 1,758,995
slave_priority : 100
slave_read_only: 1
......
......

8. When host1 normal service, restart will be automatically switched from the serving

1) to modify redis.conf before restarting the configuration file, add authentication password to connect to the main services
[root @ localhost ~] # vim /app/redis/etc/redis.conf
 masterauth 123456
2) in redis restart host1 services
[ ~ @ localhost the root] # Redis-Server /app/redis/etc/redis.conf
. 3) to view the state that it can be seen from the service switching
Replication 127.0.0.1:6379> info
#Replication
Role: Slave
MASTER_HOST : 192.168.1.106 # the main service point 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. After the connection state view of the new master promoted host2

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:1854211
second_repl_offset:63848
……
……

Guess you like

Origin www.linuxidc.com/Linux/2019-08/159729.htm