Redis Sentinel is a high-availability solutions provided by the official (except Sentinel, Redis Cluster is another program), it can automatically monitor the operating status of Redis master / slave, and if found master can not access, it will start failover to one slave station has access to switched to master.
(1) .Sentinel (Sentinel) effect
Detecting the Master state, if abnormality Master, Master-Slave switch, in which as a Master and a Slave will be, as the previous Master Slave. When the Master-Slave switch, master-redis.conf, slave-redis.conf and sentinel.conf content will change, i.e., master-redis.conf will be more than one row slaveof configuration, with the monitoring target will sentinel.conf the exchange.
(2) .Sentinel (Sentinel) works
Redis Sentinel supports the client (for example, Java's Jedis) will ask ip master to the Sentinel in connection Redis server time, and will receive automatically re-connect to the new master after master switch pub / sub event.
(3) Experimental environment
youxi1 192.168.1.6 Master,Sentinel1
youxi2 192.168.1.7 Slave,Sentinel2
youxi3 192.168.1.8 Slave,Sentinel3
(4) Experiment
1) First of all servers are installed Redis, set up and boot from Kai
Detailed view: yum install Redis under CentOS7
2) modify the Master server configuration file youxi1
[root @ youxi1 ~] # vim /etc/redis.conf the bind 0.0.0.0 // line 69, set the listen address. 0.0.0.0 means listening to all addresses protected-mode no // line 88, turn off safe mode, allowing the external network access
If the firewall is turned on, remember to add the port number
[root@youxi1 ~]# firewall-cmd --permanent --zone=public --add-port=6379/tcp && firewall-cmd --reload success success
3) Modify Slave1 youxi2 server configuration file
[root @ youxi2 ~] # vim /etc/redis.conf the bind 0.0.0.0 // line 69, set the listen address. Listen to all addresses 0.0.0.0 represents protected-mode no // line 88, the security mode off, allowing access to the external network replicaof 192.168.1.6 6379 // line 286, setting the IP address and port number of the master
If the firewall is turned on, remember to add the port number
[root@youxi1 ~]# firewall-cmd --permanent --zone=public --add-port=6379/tcp && firewall-cmd --reload success success
4) Modify Slave2 youxi3 server configuration file
[root @ youxi3 ~] # vim /etc/redis.conf the bind 0.0.0.0 // line 69, set the listen address. Listen to all addresses 0.0.0.0 represents protected-mode no // line 88, the security mode off, allowing access to the external network replicaof 192.168.1.6 6379 // line 286, setting the IP address and port number of the master
If the firewall is turned on, remember to add the port number
[root@youxi3 ~]# firewall-cmd --permanent --zone=public --add-port=6379/tcp && firewall-cmd --reload success success
5) Configure the Sentinel (Sentinel)
[root @ youxi1 ~] # vim /etc/redis-sentinel.conf protected the MODE-NO // line 17, cancel the installation mode, allowing the external network access port line 2126379 // check the port number daemonize yes // line 26 instead yes, running in the background daemon sentinel monitor mymaster 192.168.1.6 6379 2 // line 84, mymaster is the cluster name; 192.168.1.6 is the primary server IP address; 2 is the value of voting, two sentries can not connect master, then the master that the hanging, in order to avoid using the split brain odd 10000 // Down line-After-Sentinel 113 milliseconds mymaster Sentinel the syncs mymaster. 1 // Parallel-121 line sentinel failover-timeout mymaster 60000 // line 146 , failover timeout in milliseconds
Description:
down-after-milliseconds: sentinel will be sent to the master heartbeat PING to confirm the master is alive, if the master does not respond within a "time horizon" PONG or return an error message, the sentinel will subjectively (unilaterally) the master has been considered unusable (subjectively down, also referred to as SDOWN). And this down-after-milliseconds is used to specify the "certain time" in milliseconds.
parallel-syncs: failover occurs when the primary switchover, this option specifies a maximum number of simultaneous new master slave synchronization, the smaller the number, the more time required to complete failover long, but if the figure large, it means that the more slave because replication is not available. This value can be set to 1 to ensure that only one slave state command request can not be processed.
All servers are so configured, and then restart redis, start redis-sentinel and set the post.
[root@youxi1 ~]# systemctl restart redis && systemctl start redis-sentinel [root@youxi1 ~]# systemctl enable redis-sentinel Created symlink from /etc/systemd/system/multi-user.target.wants/redis-sentinel.service to /usr/lib/systemd/system/redis-sentinel.service. [root@youxi2 ~]# systemctl restart redis && systemctl start redis-sentinel [root@youxi2 ~]# systemctl enable redis-sentinel Created symlink from /etc/systemd/system/multi-user.target.wants/redis-sentinel.service to /usr/lib/systemd/system/redis-sentinel.service. [root@youxi3 ~]# systemctl restart redis && systemctl start redis-sentinel [root@youxi3 ~]# systemctl enable redis-sentinel Created symlink from /etc/systemd/system/multi-user.target.wants/redis-sentinel.service to /usr/lib/systemd/system/redis-sentinel.service.
If the firewall is turned on, remember to add the port number
firewall-cmd --permanent --zone=public --add-port=26379/tcp && firewall-cmd --reload
State 6) View from master server
The state of the primary server youxi1
[youxi1 the root @ ~] # Redis-CLI 127.0.0.1:6379> Replication info # the Replication Role: the role of master // this server connected_slaves: 2 // Number of connections from the server slave0: ip = 192.168.1.7, port = 6379 , state = online, offset = 546 , lag = 0 // status from the server Slave1: IP = 192.168.1.8, Port = 6379, state = Online, offset = 546, 0 = LAG master_replid: d3839b055eb0705dda0b2782d587e0d0f4a3177c master_replid2: 0000000000000000000000000000000000000000 master_repl_offset: 546 second_repl_offset : -1 repl_backlog_active:. 1 repl_backlog_size: 1048576 repl_backlog_first_byte_offset:. 1 repl_backlog_histlen: 546
Youxi2 state from the server
[youxi2 the root @ ~] # Redis-CLI 127.0.0.1:6379> Replication info # the Replication Role: Role of the present Slave // server master_host: IP address 192.168.1.6 // master server master_port: // master server port 6379 number master_link_status: up // master from the connected state master_last_io_seconds_ago:. 1 master_sync_in_progress: 0 slave_repl_offset: 658 slave_priority: 100 slave_read_only:. 1 connected_slaves: 0 master_replid: d3839b055eb0705dda0b2782d587e0d0f4a3177c master_replid2: 0000000000000000000000000000000000000000 master_repl_offset: 658 second_repl_offset: -1 repl_backlog_active:. 1 repl_backlog_size: 1048576 repl_backlog_first_byte_offset: 71 is repl_backlog_histlen: 588
Youxi3 state from the server
[youxi3 the root @ ~] # Redis-CLI 127.0.0.1:6379> Replication info # the Replication Role: the role of this slave // server master_host: IP address 192.168.1.6 // master server master_port: 6379 // master server port number master_link_status: up // master from the connected state master_last_io_seconds_ago:. 7 master_sync_in_progress: 0 slave_repl_offset: 686 slave_priority: 100 slave_read_only:. 1 connected_slaves: 0 master_replid: d3839b055eb0705dda0b2782d587e0d0f4a3177c master_replid2: 0000000000000000000000000000000000000000 master_repl_offset: 686 second_repl_offset: -1 repl_backlog_active:. 1 repl_backlog_size: 1048576 repl_backlog_first_byte_offset: 281 is repl_backlog_histlen:406
7) Analog Master is down, the test
Turning off the Redis youxi1
[root@youxi1 ~]# systemctl stop redis
To view the status of the youxi2
[root@youxi2 ~]# redis-cli 127.0.0.1:6379> info replication # Replication role:slave master_host:192.168.1.8 //可以看到Master服务器变成来192.168.1.8 master_port:6379 master_link_status:up master_last_io_seconds_ago:0 master_sync_in_progress:0 slave_repl_offset:86480 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:24fe501f3d7f77e790bc82d9c80e6c920522137e master_replid2:b74fb4029bf6e6a4acb09748dfe63317192cc322 master_repl_offset:86480 second_repl_offset:49216 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:6589 repl_backlog_histlen:79892
At this time in the open youxi1 of Redis, view status
[root@youxi1 ~]# systemctl start redis [root@youxi1 ~]# redis-cli 127.0.0.1:6379> info replication # Replication role:slave //角色变成来slave master_host:192.168.1.8 master_port:6379 master_link_status:up master_last_io_seconds_ago:0 master_sync_in_progress:0 slave_repl_offset:110509 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:24fe501f3d7f77e790bc82d9c80e6c920522137e master_replid2:0000000000000000000000000000000000000000 master_repl_offset:110509 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:106486 repl_backlog_histlen:4024
8) Check the status Sentinel
View 26379 Sentinel require remote port number, namely access Sentinel rather than Redis, for example, now use to access the main server from the server now youxi1 youxi3
[root@youxi1 ~]# redis-cli -h 192.168.1.8 -p 26379 192.168.1.8: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=192.168.1.8:6379,slaves=2,sentinels=3
Note: After using the Sentinel is connected.
. (5) extensions: Redis subjective and objective offline offline
Subjective offline: Subjectively Down, referred SDOWN, referring offline Sentinel determine the current instance of the server made a redis.
Objective offline: Objectively Down, referred ODOWN, it refers to a plurality of instances Sentinel SDOWN making judgment on Master Server, and interact with each other by a SENTINEL is-master-down-by-addr command later, offline derived Master Server Analyzing and open failover.
Reference: https://segmentfault.com/a/1190000002680804