Redis master-slave + KeepAlived achieve high availability
Redis is a non-relational database that we currently use more frequently. It can support diverse data types, multi-threaded high concurrency support, and redis running in memory with faster read and write. Because of the excellent performance of redis, how can we ensure that redis can deal with downtime during operation?
So today’s summary of the construction of the redis master-slave high-availability system, with reference to online bloggers of some great gods, found that many are pitted, so I share this one time, hoping to help everyone.
Redis Features
Redis is completely open source free, complies with the BSD protocol and is a high performance key-value database.
Redis and other key-value cache products have the following three characteristics:
Redis supports the persistence of data, can keep the data in memory on the disk, and can be loaded again for use when restarted.
Redis not only supports simple key-value types of data, but also provides data structures such as: Strings, Maps, Lists, Sets, and sorted sets. Storage.
Redis supports data backup, that is, data backup in master-slave mode.
The Redis advantage
is extremely high – Redis can read 100K+ times/s and write at 80K+ times/s.
Rich data types – Redis supports Strings, Lists, Hashes, Sets, and Ordered Sets data type operations for binary cases.
Atoms – All operations of Redis are atomic, and Redis also supports atomic execution of all operations after all operations.
Rich features – Redis also supports publish/subscribe, notifications, key expiration, and more.
Prepare environment
CentOS 7 –> 172.16.81.140 –> Master Redis –> Master Keepalived
CentOS7 –> 172.16.81.141 –> From Redis –> Prepared Keepalived
VIP –> 172.16.81.139
Redis (normally 3.0 or later)
KeepAlived (direct online installation)
Redis compile and install
cd /opt
tar -zxvf redis-4.0.6.tar.gz
mv 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/ redisserver
# Configure the redis connection command path
CLIEXE=/usr/local/redis/bin/redis-cli
# Configure
Redis Run PID path PIDFILE=/var/run/redis_6379.pid
# Configure the path of redis configuration file
CONF=”/etc/redis/redis.conf”
# Configure the connection authentication password for redis
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[31m Please use $0 [start|stop|restart] asfirst argument \e[0m”
;;
esac
Grant execution permissions:
chmod +x /etc/init.d/redis
Add boot start:
chkconfig –add redis
chkconfig redis on
See: chkconfig –list | grep redis
The test closed the firewall and selinux beforehand. The production environment is recommended to open the firewall.
3, add redis command environment variables
#vi /etc/profile #Add the
next line of parameters
exportPATH=”$PATH:/usr/local/redis/bin” #Environment
variables become effective
source /etc/profile
4. Start redis service
Service redis start #Check
startup
ps -ef | grep redis
Note: After we perform the same operation on our two servers, the installation completes redis. After the installation is completed, we directly enter the configuration master-slave environment.
Redis master-slave configuration
To extend back to the previous design pattern, our idea is to use 140 as the master, 141 as the slave, and 139 as the VIP elegant address. The application accesses the redis database through the 6379 port of the 139.
In normal operation, when the master node 140 goes down, the VIP floats to 141, and then 141 will take over 140 as the master node, and 140 will become the slave node, continuing to provide read and write operations.
When 140 returns to normal, 140 will perform data synchronization with 141 at this time, 140 the original data will not be lost, and the data that has been written into 141 between synchronization machines will be synchronized. After the data synchronization is completed,
The VIP will return to the 140 node and become the master node because of the weight. 141 will lose the VIP and become the slave node again, and restore to the initial state to continue providing uninterrupted read and write services.
1, configure the redis configuration file
Master-140 configuration file
vim /etc/redis/redis.conf
bind 0.0.0.0
port 6379
daemonize yes
requirepass 123456
slave-serve-stale-data yes
slave-read-only no
Slave-141 configuration file
vim /etc/redis/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 are normal.
Master node 140 terminal login test:
[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
Login test 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, synchronous test
Master node 140
The masters and slaves of this redis have been completed!
KeepAlived configuration to achieve dual hot standby
Use Keepalived to implement VIP, and achieve disaster recovery through notify_master, notify_backup, notify_fault, notify_stop.
1, configure Keepalived configuration file
Master Keepalived Profile
vim /etc/keepalived/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
priority 100
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
}
Spare Keepalived Profile
vim /etc/keepalived/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 the script
Master KeepAlived — 140
Create a 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 # delay 10 seconds later to cancel synchronization after the data synchronization is complete
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 15 seconds to wait until the data is synchronized to the other side and then switch the role of master-slave
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
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 #
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 #
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
systemctl start keepalived
systemctl enable keepalived
ps -ef | grep keepalived
Recent Comments