Redis Master/Slave + KeepAlived for High Availability
Redis is a popular non-relational database that supports a wide range of data types, multi-thread high concurrency support, redis runs in memory with faster read/write. Because redis is doing so well, how can we ensure redis can cope with downtime failures during operation,
So today I have summarized the establishment of redis master-slave high availability. I have read some blog articles from the Internet and found many of them are pitfall. So I will share this article here, hoping to help you.
Redis features
Redis is completely open-source and free. It complies with the BSD Protocol and is a high-performance key-value database.
Redis and other key-value cache products have the following three features:
Apsaradb for Redis supports data persistence. Data in the memory can be stored on the disk and loaded again during restart.
Redis not only supports simple key-value data, but also provides such data as String, Map, list, and sets) and sorted sets.
Redis supports data backup, that is, data backup in master-slave mode.
Redis advantages
Extremely high performance-Redis can read at a speed of 100 K + times/s and write at a speed of 80 K + times/s.
Rich data types-Redis supports binary case Strings, Lists, Hashes, Sets, and Ordered Sets data type operations.
All the operations of atomic Redis are atomic, and Redis also supports atomic execution after the sum of several operations.
Rich features-Redis also supports features such as publish/subscribe, notification, and key expiration.
Prepare the environment
CentOS7 --> 172.16.81.140 --> master Redis --> master Keepalived
CentOS7 --> 172.16.81.141 --> from Redis --> Backup Keepalived
VIP --> 172.16.81.139
Redis (generally 3.0 or later)
KeepAlived (directly installed online)
Redis compilation and Installation
1, prepared redis software in advance under the/opt Directory: redis-4.0.6.tar.gz
Cd/opt
Tar-zxvf redis-4.0.6.tar.gz
Music redis-4.0.6 redis
Cd redis
MakeMALLOC = libc
Make PREFIX =/usr/local/redis install
2. Configure the redis Startup Script
Vim/etc/init. d/redis
#! /Bin/sh
# Chkconfig: 2345 80 90
# Simple Redisinit. d script conceived to work on Linux systems
# As it doeSUSE of the/proc filesystem.
# Configure the redis port number
REDISPORT = 6379
# Configure the redis startup command path
EXE =/usr/local/redis/bin/redis-server
# Configure the redis connection command path
CLIEXE =/usr/local/redis/bin/redis-cli
# Configure the redis running PID path
PIDFILE =/var/run/redis_62.16.pid
# Configure the redis configuration file path
CONF = "/etc/redis. conf"
# Configure the redis connection authentication Password
REDISPASSWORD = 123456
Function start (){
If [-f $ PIDFILE]
Then
Echo "$ PIDFILE exists, process is already running or crashed"
Else
Echo "Starting Redisserver ..."
$ EXE $ CONF &
Fi
}
Function stop (){
If [! -F $ PIDFILE]
Then
Echo "$ PIDFILE does not exist, process is not running"
Else
PID = $ (cat $ PIDFILE)
Echo "Stopping ..."
$ CLIEXE-p $ REDISPORT-a $ REDISPASSWORD shutdown
While [-x/proc/$ {PID}]
Do
Echo "Waiting forRedis to shutdown ..."
Sleep 1
Done
Echo "Redis stopped"
Fi
}
Function restart (){
Stop
Sleep 3
Start
}
Case "$1" in
Start)
Start
;;
Stop)
Stop
;;
Restart)
Restart
;;
*)
Echo-e "\ e [31 m Please use $0 [start | stop | restart] asfirst argument \ e [0 m"
;;
Esac
Grant execution permission:
Chmod + x/etc/init. d/redis
Add boot start:
Chkconfig -- add redis
Chkconfig redis on
View: chkconfig -- list | grep redis
In this test, the firewall and selinux are disabled beforehand. We recommend that you enable the firewall in the production environment.
3. Add the redis command environment variable
# Vi/etc/profile
# Add parameters for the next line
ExportPATH = "$ PATH:/usr/local/redis/bin"
# Environment variables take effect
Source/etc/profile
4. Start the redis Service
Service redis start
# Check startup status
Ps-ef | grep redis
Note: perform the same operation on the two servers to install apsaradb for redis. After the installation is complete, you can directly enter the master-slave environment configuration.
Redis Master/Slave Configuration
Back to the previous design model, our idea is to use 140 as the master, 141 as the slave, 139 as the VIP elegant address, and the application accesses the redis database through port 139 of 6379.
In normal operation, when the master node is down 140 and the VIP is migrated to 141, 141 will take over 140 and become the master node. 140 will become the slave node and continue to provide read and write operations.
After 140 is restored to normal, 140 will synchronize data with 141. 140 of the original data will not be lost, and 141 of the data has been written to of the downtime, after data synchronization is complete,
The VIP will return to the 140 node and become the master node because of the weight. The 141 VIP will become the slave node again, and resume to the initial state to continue providing uninterrupted read/write services.
1. Configure the redis configuration file
Configuration file for Master-140
Vim/etc/redis. conf
Bind 0.0.0.0
Port 6379
Daemonize yes
Requrequirepass 123456
Slave-serve-stale-data yes
Slave-read-only no
Slave-141 configuration file
Vim/etc/redis. conf
Bind 0.0.0.0
Port 6379
Daemonize yes
Slaveof 172.16.81.140 6379
Masterauth 123456
Slave-serve-stale-data yes
Slave-read-only no
2. Restart the redis service after the configuration is complete! Verify that the master and slave nodes are normal.
Test the logon of the master node 140 terminal:
[Root @ localhost ~] # Redis-cli a 123456
127.0.0.1: 6379> INFO
.
.
.
# Replication
Role: master
Connected_slaves: 1
Slave0: ip = 172.16.81.141, port = 6379, state = online, offset = 105768, lag = 1
Master_replid: f83fcc3c98614d770f2205831fef1e877fa3f482
Master_replid2: 1f25604997a4ad3eb8344e8155990e78acd93312
Master_repl_offset: 105768
Second_repl_offset: 447
Repl_backlog_active: 1
Repl_backlog_size: 1048576
Repl_backlog_first_byte_offset: 447
Repl_backlog_histlen: 105322
Test logon from node 141 terminal:
[Root @ localhost ~] # Redis-cli a 123456
127.0.0.1: 6379> info
.
.
.
# Replication
Role: slave
Master_host: 172.16.81.140
Master_port: 6379
Master_link_status: up
Master_last_io_seconds_ago: 5
Master_sync_in_progress: 0
Slave_repl_offset: 105992
Slave_priority: 100
Slave_read_only: 0
Connected_slaves: 0
Master_replid: f83fcc3c98614d770f2205831fef1e877fa3f482
Master_replid2: 1f25604997a4ad3eb8344e8155990e78acd93312
Master_repl_offset: 105992
Second_repl_offset: 447
Repl_backlog_active: 1
Repl_backlog_size: 1048576
Repl_backlog_first_byte_offset: 239
Repl_backlog_histlen: 105754
3. synchronization test
Master 140
Slave 141
The Master/Slave of redis has been completed!
KeepAlived configuration for hot standby
Use Keepalived to implement VIP and implement disaster tolerance through yy_master, notify_backup, notify_fault, and notify_stop.
1. Configure the Keepalived configuration file
Master Keepalived configuration file
Vim/etc/keepalived. conf
! Configuration File for keepalived
Global_defs {
Notification_email {
Acassen@firewall.loc
Failover@firewall.loc
Sysadmin@firewall.loc
}
Notification_email_from Alexandre.Cassen@firewall.loc
Smtp_server 192.168.200.1
Smtp_connect_timeout 30
Router_id redis01
}
Vrrp_script chk_redis {
Script "/etc/keepalived/script/redis_check.sh"
Interval 2
}
Vrrp_instance VI_1 {
State MASTER
Interface eno16777984
Virtual_router_id 51
Priority100
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 1111
}
Track_script {
Chk_redis
}
Virtual_ipaddress {
172.16.81.139
}
Notify_master/etc/keepalived/script/redis_master.sh
Notify_backup/etc/keepalived/script/redis_backup.sh
Notify_fault/etc/keepalived/script/redis_fault.sh
Notify_stop/etc/keepalived/script/redis_stop.sh
}
Backup Keepalived configuration file
Vim/etc/keepalived. conf
! Configuration File for keepalived
Global_defs {
Notification_email {
Acassen@firewall.loc
Failover@firewall.loc
Sysadmin@firewall.loc
}
Notification_email_from Alexandre.Cassen@firewall.loc
Smtp_server 192.168.200.1
Smtp_connect_timeout 30
Router_id redis02
}
Vrrp_script chk_redis {
Script "/etc/keepalived/script/redis_check.sh"
Interval 2
}
Vrrp_instance VI_1 {
State BACKUP
Interface eno16777984
Virtual_router_id 51
Priority 99
Advert_int 1
Authentication {
Auth_type PASS
Auth_pass 1111
}
Track_script {
Chk_redis
}
Virtual_ipaddress {
172.16.81.139
}
Notify_master/etc/keepalived/script/redis_master.sh
Notify_backup/etc/keepalived/script/redis_backup.sh
Notify_fault/etc/keepalived/script/redis_fault.sh
Notify_stop/etc/keepalived/script/redis_stop.sh
}
2. Configure scripts
Master KeepAlived -- 140
Create and store the script directory: mkdir-p/etc/keepalived/script/
Cd/etc/keepalived/script/
[Root @ localhost script] # cat redis_check.sh
#! /Bin/bash
ALIVE = '/usr/local/redis/bin/redis-cli-a 123456 PING'
If ["$ ALIVE" = "PONG"]; then
Echo $ ALIVE
Exit 0
Else
Echo $ ALIVE
Exit 1
Fi
[Root @ localhost script] # cat redis_master.sh
#! /Bin/bash
REDISCLI = "/usr/local/redis/bin/redis-cli-a 123456"
LOGFILE = "/var/log/keepalived-redis-state.log"
Sleep 15
Echo "[master]" >>$ LOGFILE
Date> $ LOGFILE
Echo "Being master..." >>$ LOGFILE 2> & 1
Echo "Run SLAVEOF cmd..."> $ LOGFILE
$ Rediscli slaveof 172.16.81.141 6379 >>$ LOGFILE 2> & 1
If [$? -Ne 0]; then
Echo "data rsync fail." >>$ LOGFILE 2> & 1
Else
Echo "data rsync OK." >>$ LOGFILE 2> & 1
Fi
Sleep 10 # The synchronization will be canceled after a delay of 10 seconds.
Echo "Run slaveof no one cmd..."> $ LOGFILE
$ Rediscli slaveof no one >>$ LOGFILE 2> & 1
If [$? -Ne 0]; then
Echo "Run slaveof no one cmd fail." >>$ LOGFILE 2> & 1
Else
Echo "Run slaveof no one cmd OK." >>$ LOGFILE 2> & 1
Fi
[Root @ localhost script] # cat redis_backup.sh
#! /Bin/bash
REDISCLI = "/usr/local/redis/bin/redis-cli-a 123456"
LOGFILE = "/var/log/keepalived-redis-state.log"
Echo "[backup]" >>$ LOGFILE
Date> $ LOGFILE
Echo "Being slave..."> $ LOGFILE 2> & 1
Sleep 15 # delay of 15 seconds before switching the Master/Slave role after the data is synchronized by the other party
Echo "Run SLAVEOF cmd..."> $ LOGFILE
$ Rediscli slaveof 172.16.81.141 6379 >>$ LOGFILE 2> & 1
[Root @ localhost script] # cat redis_fault.sh
#! /Bin/bash
LOGFILE =/var/log/keepalived-redis-state.log
Echo "[fault]" >>$ LOGFILE
Date> $ LOGFILE
[Root @ localhost script] # cat redis_stop.sh
#! /Bin/bash
LOGFILE =/var/log/keepalived-redis-state.log
Echo "[stop]" >>$ LOGFILE
Date> $ LOGFILE
Slave KeepAlived -- 141
Create and store the script directory: mkdir-p/etc/keepalived/script/
Cd/etc/keepalived/script/
[Root @ localhost script] # cat redis_check.sh
#! /Bin/bash
ALIVE = '/usr/local/redis/bin/redis-cli-a 123456 PING'
If ["$ ALIVE" = "PONG"]; then
Echo $ ALIVE
Exit 0
Else
Echo $ ALIVE
Exit 1
Fi
[Root @ localhost script] # cat redis_master.sh
#! /Bin/bash
REDISCLI = "/usr/local/redis/bin/redis-cli-a 123456"
LOGFILE = "/var/log/keepalived-redis-state.log"
Echo "[master]" >>$ LOGFILE
Date> $ LOGFILE
Echo "Being master..." >>$ LOGFILE 2> & 1
Echo "Run SLAVEOF cmd..."> $ LOGFILE
$ Rediscli slaveof 172.16.81.140 6379 >>$ LOGFILE 2> & 1
Sleep 10 # The synchronization will be canceled after a delay of 10 seconds.
Echo "Run slaveof no one cmd..."> $ LOGFILE
$ Rediscli slaveof no one >>$ LOGFILE 2> & 1
[Root @ localhost script] # cat redis_backup.sh
#! /Bin/bash
REDISCLI = "/usr/local/redis/bin/redis-cli-a 123456"
LOGFILE = "/var/log/keepalived-redis-state.log"
Echo "[backup]" >>$ LOGFILE
Date> $ LOGFILE
Echo "Being slave..."> $ LOGFILE 2> & 1
Sleep 15 # delay of 15 seconds before switching the Master/Slave role after the data is synchronized by the other party
Echo "Run SLAVEOF cmd..."> $ LOGFILE
$ Rediscli slaveof 172.16.81.140 6379 >>$ LOGFILE 2> & 1
[Root @ localhost script] # cat redis_fault.sh
#! /Bin/bash
LOGFILE =/var/log/keepalived-redis-state.log
Echo "[fault]" >>$ LOGFILE
Date> $ LOGFILE
[Root @ localhost script] # cat redis_stop.sh
#! /Bin/bash
LOGFILE =/var/log/keepalived-redis-state.log
Echo "[stop]" >>$ LOGFILE
Date> $ LOGFILE
3. Start the service
Systemctl start keepalived
Systemctl enable keepalived
4. test whether the service is normal.
Ps-ef | grep keepalived
Ping 172.16.81.139
View VIP address
Test whether the connection to redis is normal
Redis-cli-h 172.16.81.139-p 6379-a 123456
The Keepalived test is complete !!
Test failover status
Disable the primary redis service and check whether the VIP address is changed from redis to the primary address? Then, insert data on the new primary redis141 to test whether the data exists when 140 is restored? Will 141 be changed to slave nodes?
1. Shut down redis in master 140
Service redis stop
2. view the 141 status
Test the remote connection of a VIP connection
You can view the status information through INFO.
We can see that 141 of the node has been changed to the master node.
3. insert data
4. Enable master node 140
Service redis start
5. view the Master/Slave status of 140 and 141
141 status, changed from
140 status, changed back to master
We can view the new data just inserted on 140.
Data exists, proving that the master-slave switchover is normal !!!
I personally tested the above. If you have any questions, please leave a message !!!
This article permanently updates link: https://www.bkjia.com/Linux/2018-03/151377.htm