redis sentinel cluster
Redis sentinel is different from redis master-slave;
redis master-slave; The master hangs up and will never replace the master
redis sentinel master and slave will replace the master MHA
1, Overview of Redis sentinel;
summary; Sentinel: sentinel mode is a distributed system. This process is used to monitor the working status of the Master master server in the redis cluster. When the Master server fails, it can switch between the Master server and Slave server to ensure the high availability of the system. It has been integrated in redis2 In version 6 +, the sentinel mode of redis is stable after version 2.8;
About sentinel's three scheduled tasks:
1. ping other sentinel and redis nodes every second to detect heartbeat
2. Every 10 seconds, each sentinel will execute the info command on the master and slave to find the slave node and determine the master-slave relationship
3. Every 2 seconds, each sentinel exchanges information through the channel of the master node (pub/sub). There is a publish subscribe channel (sentinel:hello) on the master node. Sentinel node passed__ sentinel__: The Hello channel exchanges information (the "views" of the node and its own information) to reach a consensus
2, Working mechanism of Redis sentinel
Working process
Monitoring: sentinel will constantly check whether your master server and slave server are working normally
Notification: when a monitored Redis server has a problem, Sentinel can send a notification to the administrator or other applications through the API
Automatic failover: if the master does not work as expected, Sentinel can start a failover process, in which one replica is promoted to master, and other additional replicas are reconfigured to use the new master, and applications using Redis server will be informed of the new address to use when connecting
Configuration provider: Sentinel , acts as the authoritative source of client service discovery: the client connects to Sentinel , to request the address of the current , Redis , master node responsible for a given service. Sentinels} will report the new address if a failover occurs
Deploy Redis Sentinel} cluster
By default: sentinel runs on TCP port 26379 (please note that 6379 is an ordinary Redis port). Sentinel accepts the command to use Redis protocol. Therefore, you can use Redis cli or any other unmodified Redis client to talk to sentinel. In addition, you can directly query sentinel to check the status of the monitored Redis instance from its perspective, view other sentinel it knows, and so on
System type | IP address | Host name | port |
CentOS7.4 | 192.168.10.207 | master | 6379;26379 |
CentOS7.4 | 192.168.10.208 | slave1 | 6379 |
CentOS7.4 | 192.168.10.209 | slave2 | 6379 |
1. Install redis for three servers before installing sentinel cluster
Master slave1 and salve2 need to be installed
2. Configure master-slave replication
[root@slave1 redis-6.2.6]# echo 'slaveof 192.168.10.207 6379' >> /usr/local/redis/redis.conf [root@slave1 redis-6.2.6]# echo 'masterauth 123.com' >> /usr/local/redis/redis.conf [root@slave1 redis-6.2.6]# /etc/init.d/redis restart [root@slave2 redis-6.2.6]# echo 'slaveof 192.168.10.207 6379' >> /usr/local/redis/redis.conf [root@slave2 redis-6.2.6]# echo 'masterauth 123.com' >> /usr/local/redis/redis.conf [root@slave2 redis-6.2.6]# /etc/init.d/redis restart
3. Verifying master-slave replication
root@master redis-6.2.6]# redis -h 192.168.10.207 -p 6379 -a 123.com Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.10.207:6379> info replication # Replication role:master connected_slaves:2 slave0:ip=192.168.10.208,port=6379,state=online,offset=3304,lag=0 slave1:ip=192.168.10.209,port=6379,state=online,offset=3304,lag=0 master_failover_state:no-failover master_replid:35f6265264dd5d8b1349eb8c63434419c4081e15 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:3304 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:3304 192.168.10.207:6379> set hello world OK 192.168.10.207:6379> get hello "world" [root@slave1 redis-6.2.6]# redis -h 192.168.10.208 -p 6379 -a 123.com Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.10.208:6379> get hello "world" 192.168.10.208:6379> exit
4. Configure Redis sentinel
[root@master ~]# cp redis-6.2.6/src/redis-sentinel /usr/local/redis/ cp: Overwrite"/usr/local/redis/redis-sentinel"? y [root@master ~]# cp redis-6.2.6/sentinel.conf /usr/local/redis/ [root@master ~]# mkdir -p /var/redis/data [root@master ~]# vim /usr/local/redis/sentinel.conf ...... 21 port 26379 26 daemonize yes #Enable background operation 36 logfile"26379.log" #Log run 65 dir /var/redis/data #Specify path 84 sentinel monitor mymaster 192.168.10.207 6379 1 #Switch when a master fails 116 sentinel auth-pass mymaster 123.com #Manual input; Password linking master and slave nodes 125 sentinel down-after-milliseconds mymaster 1000 #Specifies the expiration time of the master, in milliseconds, 1s 225 sentinel failover-timeout mymaster 200000 #The time period of successful switching operation is 200s. If it exceeds, it is considered as switching failure ......
5. Start Redis sentinel
[root@master ~]# /usr/local/redis/redis-sentinel /usr/local/redis/sentinel.conf [root@master ~]# tail -9 /usr/local/redis/sentinel.conf # Generated by CONFIG REWRITE protected-mode no user default on nopass ~* &* +@all sentinel myid 9542c7a9e992338189be4adde2d48daf3dbf3bcf sentinel config-epoch mymaster 0 sentinel leader-epoch mymaster 0 sentinel current-epoch 0 sentinel known-replica mymaster 192.168.10.208 6379 sentinel known-replica mymaster 192.168.10.209 6379 [root@master ~]# netstat -anptu|grep redis-sen tcp6 0 0 :::26379 :::* LISTEN 19231/redis-sentine
6. Test Redis sentinel and switch the cluster after closing the master node
[root@master ~]# /etc/init.d/redis stop [root@master ~]# cat /var/redis/data/26379.log 19231:X 30 Dec 2021 23:39:42.262 * +slave slave 192.168.10.208:6379 192.168.10.208 6379 @ mymaster 192.168.10.209 6379 19231:X 30 Dec 2021 23:39:42.262 * +slave slave 192.168.10.207:6379 192.168.10.207 6379 @ mymaster 192.168.10.209 6379 19231:X 30 Dec 2021 23:39:43.281 # +sdown slave 192.168.10.207:6379 192.168.10.207 6379 @ mymaster 192.168.10.209 6379
7. Test the master-slave synchronization between the new master node and the slave node
[root@slave2 redis-6.2.6]# redis -h 192.168.10.209 -p 6379 -a 123.com Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.10.209:6379> set asad fdsa OK [root@slave1 redis-6.2.6]# redis -h 192.168.10.208 -p 6379 -a 123.com Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.10.208:6379> keys * 1) "asad" 2) "hello"
8. Restore the master node and view the cluster status
[root@master ~]# /etc/init.d/redis start Starting Redis server... Redis is running... [root@master ~]# redis -h 192.168.10.207 -p 6379 -a 123.com Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 192.168.10.207:6379> info replication # Replication role:slave master_host:192.168.10.209 master_port:6379 master_link_status:down master_last_io_seconds_ago:-1 master_sync_in_progress:1 slave_read_repl_offset:1 slave_repl_offset:1 master_sync_total_bytes:-1 master_sync_read_bytes:0 master_sync_left_bytes:-1 master_sync_perc:-0.00 master_sync_last_io_seconds_ago:0 master_link_down_since_seconds:-1 slave_priority:100 slave_read_only:1 replica_announced:1 connected_slaves:0 master_failover_state:no-failover master_replid:944837a7a762ba00e06b9dcba6f2de106497cff1 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