Edition
CentOS 8.1.1911(VMWare) redis 5.0.8
Essential information
Server IP: 192.168.31.68 Redis port: 7000 (primary) 7001,7002 (slave) Sentinel port: 27000 27001 27002
- The key code or command represents this
Configure Redis
- master7000.conf
bind 0.0.0.0 port 7000 daemonize yes dir /home/misty/redis-data/sentinel01 pidfile /var/run/redis_7000.pid logfile "7000.log" dbfilename dump7000.rdb
- slave7001.conf
bind 0.0.0.0 port 7001 daemonize yes dir /home/misty/redis-data/sentinel01 pidfile /var/run/redis_7001.pid logfile "7001.log" dbfilename dump7001.rdb slaveof 192.168.31.68 7000
- slave7002.conf
bind 0.0.0.0 port 7002 daemonize yes dir /home/misty/redis-data/sentinel01 pidfile /var/run/redis_7002.pid logfile "7002.log" dbfilename dump7002.rdb slaveof 192.168.31.68 7000
Start Redis Service
- Start redis
redis-server master7000.conf redis-server slave7001.conf redis-server slave7002.conf
Validation service started
$ ps -ef | grep redis-server | grep -v grep misty 4633 2639 0 03:16 ? 00:00:00 redis-server 0.0.0.0:7000 misty 4649 2639 0 03:16 ? 00:00:00 redis-server 0.0.0.0:7001 misty 4663 2639 0 03:16 ? 00:00:00 redis-server 0.0.0.0:7002
Redis automatically establishes a master-slave relationship
redis:7000> info replication # Replication role:master connected_slaves:2 slave0:ip=192.168.31.68,port=7001,state=online,offset=630,lag=0 slave1:ip=192.168.31.68,port=7002,state=online,offset=630,lag=1 master_replid:187ff0474ccd0303571db1ca94174c46c2476dba master_replid2:0000000000000000000000000000000000000000 master_repl_offset:630
The master knows two slave node information.Notable is offset, where the master and slave nodes determine whether they are synchronized or not.
redis:7001> info replication # Replication role:slave master_host:192.168.31.68 master_port:7000 master_link_status:up master_last_io_seconds_ago:6 master_sync_in_progress:0 slave_repl_offset:882 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:187ff0474ccd0303571db1ca94174c46c2476dba master_replid2:0000000000000000000000000000000000000000 master_repl_offset:882
Note here that slave_priority, in sentinel mode, if master is down, sentinel first decides who to promote to master based on slave_priority.
By default, slave_priority is the same for all nodes, so the largest choice is based on offset.
If no successor can be selected, choose master with the smallest runID (oldest seniority).
The runID is viewed through info server.
At this point we think shutdown drops the master node, and the slave node shows that the master node is down.The slave node is not automatically promoted to master.
# Replication role:slave master_host:192.168.31.68 master_port:7000 master_link_status:down master_last_io_seconds_ago:-1
Restart master node and restore state to up
Configure Sentinel
- sentinel27000.conf
port 27000 daemonize yes pidfile /var/run/redis-sentinel.pid logfile "sentinel27000.log" dir /home/misty/redis-data/sentinel01 sentinel monitor mymaster 192.168.31.68 7000 2 sentinel down-after-milliseconds mymaster 30000 sentinel parallel-syncs mymaster 1 sentinel failover-timeout mymaster 180000 sentinel deny-scripts-reconfig yes
Since the profiles only have different port numbers, only one profile is posted.Can be created in batches with the sed command
- sentinel27001.conf
sed "s/27000/27001/g" sentinel27000.conf > sentinel27001.conf
We will note that sentinel's configuration information does not identify the presence of other sentinels, only the master node's information.When sentinel service is started, the presence of other sentinel and slave nodes is automatically perceived through the master node between sentinel nodes.
sentinel will modify the configuration file after it is started. It is recommended that you back up the configuration file.
- Start sentinel
redis-sentinel sentinel27000.conf redis-sentinel sentinel27001.conf redis-sentinel sentinel27002.conf
Verify sentinel started successfully:
> ps -ef | grep redis-sentinel | grep -v grep misty 5516 2639 0 03:59 ? 00:00:00 redis-sentinel *:27000 [sentinel] misty 5528 2639 0 04:00 ? 00:00:00 redis-sentinel *:27001 [sentinel] misty 5539 2639 0 04:00 ? 00:00:00 redis-sentinel *:27002 [sentinel]
Connect to any sentinel using redis-cli and run info sentinel to get the same information
# 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.31.68:7000,slaves=2,sentinels=3
A sentinel group can manage multiple redis master-slave groups, distinguished by master:name
Test Master-Slave Switching
We shutdown down the 7000 nodes and wait a moment to see that the primary node has switched to 7001.
sentinel:27000> info sentinel ... master0:name=mymaster,status=ok,address=192.168.31.68:7001,slaves=2,sentinels=3
Show that there are still two slaves, when looking at the 7001 node, we found that there is only one slave with 7002 left
redis:7001> info replication # Replication ... slave0:ip=192.168.31.68,port=7002,state=online,offset=227471,lag=0
When the redis:7000 service is restarted, it is automatically set to slave redis:7001
redis:7001> info replication # Replication ... slave0:ip=192.168.31.68,port=7002,state=online,offset=299880,lag=0 slave1:ip=192.168.31.68,port=7000,state=online,offset=299880,lag=1
complete
At this point, a basic EDIS Sentinel group has been set up.
The redis sentinel node is only responsible for monitoring, not for the forwarding of commands such as add-delete checks, and the client still needs to connect to the actual Redis master node for operation.(different from Cluster)
Appendix: Java Connection Redis Sentinel
import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import redis.clients.jedis.JedisSentinelPool; import java.util.HashSet; public class Sentinel { public static void main(String[] args) { var poolConfig = new GenericObjectPoolConfig<>(); poolConfig.setMaxTotal(10); var sentinels = new HashSet<String>(); sentinels.add("192.168.31.68:27000"); sentinels.add("192.168.31.68:27001"); sentinels.add("192.168.31.68:27002"); var sentinelPool = new JedisSentinelPool("mymaster", sentinels, poolConfig, 30000); try (var jedis = sentinelPool.getResource()) { jedis.set("hello", "world"); System.out.println(jedis.get("hello")); } catch (Exception e) { e.printStackTrace(); } } }
If Redis is unable to connect, modify the server firewall to open related ports
Client Principles
- Select an available Sentinel node
- Visit sentinel to get master information: sentinel get-master-addr-by-name mymaster
- Verify master node:role
- Create connection pool
- Listen for sentinel and rebuild connection pool after master node changes