Redis High Availability
Demand
(1) Redis is run in a Docker container, (2) to realize the dual-machine hot standby, when the redis-master is abnormal, Redis-slave quickly switch to master to provide services.
(3) When the Redis-master data is updated, the Redis-slave data can be updated synchronously. Solution Solutions
Use keepalived to achieve high availability of redis. Keepalived uses shell scripts to periodically detect if the Redis service is functioning properly. When the Redis service is abnormal, the drift of the virtual IP is used to implement failover. Deployment Implementation
1. Environmental statement
Object |
version |
Notes |
Redis-master Host |
CentOS 7 |
Ip:xx. xx.93.179 |
Redis-slave Host |
CentOS 7 |
Ip:xx. xx.93.182 |
keepalived software version: 1.2.13
Skylar-redis Image Pack version: 1600 for Dynasky 6
Virtual IP address: xx.xx.93.180. Note: To select a virtual IP address that is not occupied
2. Installation
(1) on master, slave host, need to install keepalived, Ipvsadm
$ yum-y Install Ipvsadm keepalived
To see if the keepalived installed successfully:
$ keepalived-v
keepalived v1.2.13 (03/06,2015)
(2) on master, slave host, need to start Skylar-redis container
$ docker Run--name=redis--restart=always-d--net=host--privileged=true-v/data/redis:/var/lib/redis/skylar_redis
(3) Configure keepalived to add shell scripts.
master, slave host/etc/keepavlied/keepavlied.conf need to be modified, the content is slightly different.
The interface, virtual_ipaddress, MASTER_IP, slave_ip in this profile need to be modified when you actually install it
On the master host, the keepalived.conf content is as follows:
$ cat/etc/keepalived/keepalived.conf! Configuration File for keepalived global_defs {router_id redis100} vrrp_script Chk_redis {script '/etc/keepali ved/scripts/redis_check.sh 127.0.0.1 6379 "Interval 2 timeout 2 fall 3} vrrp_instance Redis {State M
ASTER # Master set to SLAVE also interface eno16777984 # # modified to actual NIC name virtual_router_id 150 Nopreempt # seize,must Add advert_int 1 Authentication {#all node must same # Auth_type PASS # Auth_pass 1111} virtual_ipaddress {XX. XX.93.180 # # modified to the actual assigned virtual IP address} track_script {Chk_redis} notify_master "/etc/keepalived/script s/redis_master.sh 127.0.0.1 10.16.93.182 6379 "# # 10.16.93.182 should be modified to the actual slave IP address notify_backup"/etc/keepalived/ scripts/redis_backup.sh 127.0.0.1 10.16.93.182 6379 "# # 10.16.93.182 should be modified to the actual slave IP address Notify_fault/etc/keepali ved/scripts/redis_fault.sh Notify_stop/ETC/KEEPALIVED/SCRIPTS/REDIS_STOP.SH}
On the slave host, the keepalived.conf content is as follows:
$ cat/etc/keepalived/keepalived.conf! Configuration File for keepalived global_defs {router_id redis101} vrrp_script Chk_redis {script '/etc/keepali ved/scripts/redis_check.sh 127.0.0.1 6379 "Interval 2 timeout 2 fall 3} vrrp_instance Redis {State BA Ckup Interface eno16777984 # #需要修改为实际网卡名称 virtual_router_id 1 Advert_int n {#all node must same Auth_type PASS auth_pass 1111} virtual_ipaddress {XX. xx.93.180 # #需要修改为实际分配的虚拟IP} track_script {Chk_redis} notify_master "/etc/keepalived/script s/redis_master.sh 127.0.0.1 XX. xx.93.179 6379 "# # XX. xx.93.179 needs to be modified to the actual master host IP notify_backup "/etc/keepalived/scripts/redis_backup.sh 127.0.0.1 XX. xx.93.179 6379 "# # XX. xx.93.179 needs to be modified to the actual master host IP notify_fault/etc/keepalived/scripts/redis_fault.sh notify_stop/etc/keepalived/script S/REDIS_STOP.SH}
On the master and slave hosts, add/etc/keepalived/scripts directories and redis_master.sh, redis_backup.sh, redis_fault.sh, and redis_stop.sh files. The final directory structure is as follows:
$ pwd
/etc/keepalived
$ tree
.
├──keepalived.conf
└──scripts
├──redis_backup.sh
├──redis_check.sh
├──redis_fault.sh
├── redis_master.sh
└──redis_stop.sh
1 directory, 6 files
redis_master.sh, redis_backup.sh, redis_fault.sh, redis_stop.sh files can be downloaded from github, download links:
Https://github.com/qinguanri/Redis_HA
The following is a brief list of the contents of several shell files:
$ cat redis_master.sh #!/bin/bash rediscli= "Docker exec redis/usr/bin/redis-cli-h $1-p $" logfile= "/var/log/keepali Ved-redis-state.log "echo" [Master] >> $LOGFILE date >> $LOGFILE echo "Being master ..." >> $LOGFILE EC Ho "Run MASTER cmd ..." >> $LOGFILE $REDISCLI slaveof $ >> $LOGFILE sleep #delay s wait data async C Ancel sync echo "Run slaveof no one cmd ..." >> $LOGFILE $REDISCLI slaveof no one >> $LOGFILE $ cat Redis_ba
ckup.sh #!/bin/bash rediscli= "Docker exec redis/usr/bin/redis-cli" logfile= "/var/log/keepalived-redis-state.log" echo "[Backup]" >> $LOGFILE date >> $LOGFILE echo "Run slaveof cmd ..." >> $LOGFILE $REDISCLI slaveof $ >> $LOGFILE 2>&1 sleep #delay s Wait Data sync Exchange role $ cat redis_check.sh #!/bin/bash A Live= ' Docker exec redis/usr/bin/redis-cli-h $1-p $ PING ' logfile= '/var/log/keepalived-redis-check.log ' echo ' [check] ' >> $LOGFILE Date >> $LOGFILE if ["$ALIVE" = = "PONG"]; Then:echo "Success:redis-cli-h $1-p $ PING $ALIVE" >> $LOGFILE 2>&1 exit 0 Else Echo "faile
D:redis-cli-h $1-p $ PING $ALIVE ">> $LOGFILE 2>&1 exit 1 fi
3. Start keepalived
Execute the following command on the master host, the slave host, to start keepalived
$ systemctl Start Keepalived.service
4. Verify \ Simulate failure