Note: Make sure you have Redis and keepalived installed, this article is not about how to install them.
1. Description of the version to be used
Redis version: 5.0.2
Keepalived version: 1.3.5
Linux version: Centos 7.9
View the Redis version:
/usr/local/redis/bin/redis-cli -v
View Keepalived version information:
rpm -qa|grep keepalived or keepalived -v
2. Description of the functional realization:
- Using Keepalived to Provide Virtual IP External Access to Redis
- Redis builds master-slave data synchronization, where the master is used to read and write data, and the slave is used to synchronize backups of the master's data.
- When the master goes down, the Keepalived virtual IP automatically points to the slave server. The slave server temporarily becomes the master and continues to work.
- After the master server is restarted, the Keepalived virtual IP is redirected to the master server. The master server synchronizes the slave server data and continues to work. The slave server changes from a temporary master to a slave and continues to synchronize the master data for backup.
3. Illustrative diagrams
Keepalived generates a virtual IP. the client needs to access the virtual IP for Redis connections:.
3.1, primary and backup servers in operation
3.2. Main down, backup server in operation
3.3. Main recovery, standby server in operation
4, build Redis master and slave
First make sure that both servers have Redis service installed, Redis port number and password for both servers must be consistent. Here I am using port number: 6379 and password: 1234qwer for both servers.
Server IP:
Main server: 192.168.42.130
Backup server: 192.168.42.133
4.1 Modification of configuration files
First of all, you need to modify the redis configuration file of the standby server to mount the standby server redis under the master server redis to realize the master-slave configuration.
Enter the redis directory
cd /usr/local/redis/
modify a document
vim
Locate the replicaof and masterauth attributes to configure
# replicaof <masterip> <masterport>
replicaof 192.168.42.130 6379
# If the master is password protected (using the "requirepass" configuration
# directive below) it is possible to tell the replica to authenticate before
# starting the replication synchronization process, otherwise the master will
# refuse the replica request.
#
# masterauth <master-password>
masterauth "1234qwer"
replicaof: master server IP and port number
masterauth: used to protect the data security of the Redis master node during master-slave replication. The password is to set the same password for the master and slave nodes. To ensure that only authorized slave nodes can connect to the master node. If not configured, it will result in node connection failure:master_link_status:down
By logging into redis input:info replication
You can view Redis cluster configuration information. If the build is successful, node information is displayed.
Attention: In Redis 5.0 and above, the SLAVEOF command is deprecated and using it on the server results in a command failure. Therefore, the correct way to set up replication in Redis 5.0 and above is to use the REPLICAOF command. For compatibility with older versions, slaveof is still supported by configuration, but not by command.
4.2 Validating the master-slave replication function
Verification: Log into Redis on the primary server and insert a key. Log in to Redis on the backup server and query the key to see if the data is retrieved. If the data is successfully retrieved, it means that the Redis master-slave replication is successfully built.
Go to the master server and log into redis
/usr/local/redis/bin/redis-cli
Perform password authentication
auth 1234qwer
Output OK to indicate successful authentication. Stored data
set verify-key "Test Verify is Success"
The output OK indicates that the insertion was successful. Next, log in to the standby server to view the data. Enter the backup server and log into redis:
/usr/local/redis/bin/redis-cli
Perform password authentication
auth 1234qwer
Output OK, indicating successful authentication. Get key data
get verify-key
Output: "Test Verify is Success", indicating that the Redis master-slave build was successful.
5. Configure Keepalived information
The /etc/keepalived directory holds the files. Create a scripts_redis folder in that directory, directory /etc/keepalived/scripts_redis, and put redis_stop.sh, redis_master.sh, redis_fault.sh, redis_check.sh, and redis_backup. sh into the scripts_redis file directory.
5.1 Master Server Configuration
compile
! Configuration File for keepalived
global_defs {
router_id redis-master #unique identifier Note that the master and backup service names cannot be the same.
script_user root
enable_script_security
}
vrrp_script redis_check { #name of the script to check, the following calls must be consistent with this name
script "/etc/keepalived/scripts_redis/redis_check.sh" #Listen for redis to start up script path
interval 4 #Listen for heartbeats
weight -5
fall 3
rise 2
}
vrrp_instance VI_redis {
state MASTER #The current keepalived state MASTER or BACKUP.
interface eth0 #NIC name can be configured via ifconfig.
virtual_router_id 21
priority 110 #Priority The master service should be higher than the backup service.
garp_master_refresh 10
garp_master_refresh_repeat 2
advert_int 1
nopreempt
unicast_src_ip 192.168.42.130 #unicast mode current server master IP address
unicast_peer {
192.168.42.133 # standby server Ip
}
authentication { #authentication account, password for communication between keepalived
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.42.161 #virtual_ipaddress Client's uniform access address
}
garp_master_delay 1
garp_master_refresh 5
track_interface {
eth0 #network_card
}
track_script {
redis_check #name of the script check call
}
notify_master /etc/keepalived/scripts_redis/redis_master.sh #master script Triggered when the state of keepalived is set to master, or when backup is upgraded to master after master is stopped.
notify_backup /etc/keepalived/scripts_redis/redis_backup.sh #backup script Triggered when the state of keepalived is set to backup.
notify_fault /etc/keepalived/scripts_redis/redis_fault.sh #fault scripts
notify_stop /etc/keepalived/scripts_redis/redis_stop.sh #stop script Triggered when keepalived stops.
}
Write redis_master.sh. When the master script starts, you need to synchronize the redis data of the standby server first before setting the master node to start:
#!/bin/bash
LOGFILE=/var/log/
REDISCLI="/usr/local/redis/bin/redis-cli"
echo "Running redis_master.sh..." >>$LOGFILE
echo "[Master]" >> $LOGFILE
date >> $LOGFILE
echo "Being Master..." >> $LOGFILE
echo "Running SLAVEOF cmd..." >> $LOGFILE
$REDISCLI -h 192.168.42.130 -p 6379 -a 1234qwer CONFIG SET masterauth "1234qwer" 2>&1
$REDISCLI -h 192.168.42.130 -p 6379 -a 1234qwer REPLICAOF 192.168.42.133 6379 2>&1
sleep 5s
echo "Run slaveof no one cmd..." >>$LOGFILE
$REDISCLI -h 192.168.42.130 -p 6379 -a 1234qwer REPLICAOF NO ONE >>$LOGFILE 2>&1
echo "Finished running redis_master.sh..." >>$LOGFILE
Write redis_backup.sh.
#!/bin/bash
LOGFILE=/var/log/
REDISCLI="/usr/local/redis/bin/redis-cli"
echo "Running redis_bakcup.sh..." >>$LOGFILE
echo "[Backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being Slave..." >> $LOGFILE
echo "Run SLAVEOF cmd..." >> $LOGFILE
$REDISCLI -h 192.168.42.130 -p 6379 -a 1234qwer CONFIG SET masterauth "1234qwer" >>$LOGFILE 2>&1
$REDISCLI -h 192.168.42.130 -p 6379 -a 1234qwer REPLICAOF 192.168.42.133 6379 >>$LOGFILE 2>&1
echo "Finished running redis_backup.sh..." >>$LOGFILE
5.2 Standby server configuration
compile
! Configuration File for keepalived
global_defs {
router_id redis-slave #unique identification Note that the master and backup service names cannot be the same
script_user root
enable_script_security
}
vrrp_script redis_check {
script "/etc/keepalived/scripts_redis/redis_check.sh" #monitorredisWhether to start the script path
interval 4 #monitor心跳
weight -5
fall 3
rise 2
}
vrrp_instance VI_redis {
state BACKUP #be facing (us)keepalivedstate of affairs set toBACKUP
interface eth0
virtual_router_id 21
priority 100
garp_master_refresh 10
garp_master_refresh_repeat 2
advert_int 1
nopreempt
unicast_src_ip 192.168.42.133 #unicast mode be facing (us)服务器IPaddress
unicast_peer {
192.168.42.130 #master serverIp
}
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.42.161 #theoreticalIPaddress 客户端统一的访问address
}
garp_master_delay 1
garp_master_refresh 5
track_interface {
eth0
}
track_script {
redis_check
}
notify_master /etc/keepalived/scripts_redis/redis_master.sh
notify_backup /etc/keepalived/scripts_redis/redis_backup.sh
notify_fault /etc/keepalived/scripts_redis/redis_fault.sh
notify_stop /etc/keepalived/scripts_redis/redis_stop.sh
}
Write redis_master.sh
#!/bin/bash
# LOGFILEDocumentation needs to be changed accordingly
LOGFILE=/var/log/
REDISCLI="/usr/local/redis/src/redis-cli"
echo "Running redis_master.sh..." >>$LOGFILE
echo "[Master]" >> $LOGFILE
date >> $LOGFILE
echo "Begin Master ..." >> $LOGFILE
echo "Run slaveof no one cmd...">>$LOGFILE
# SLAVEOF 5.0The above has been abandoned REPLICAOF
$REDISCLI -h 192.168.42.133 -p 6379 -a 1234qwer REPLICAOF NO ONE >>$LOGFILE 2>&1
echo "Finished running redis_master.sh..." >>$LOGFILE
Write redis_backup.sh
/bin/bash /bin/bash
LOGFILE=/var/log/
REDISCLI="/usr/local/redis/src/redis-cli"
echo "Running redis_bakcup.sh..." >>$LOGFILE
echo "[Backup]" >> $LOGFILE
date >> $LOGFILE
echo "Being Slave..." >> $LOGFILE
sleep 15s #Sleep for 15 seconds to ensure that the master script redis_master.sh finishes executing before executing the master-slave command.
echo "Run SLAVEOF cmd..." >> $LOGFILE
# SLAVEOF 5.0 has been deprecated to: REPLICAOF
$REDISCLI -h 192.168.42.133 -p 6379 -a 1234qwer CONFIG SET masterauth "1234qwer" >> $LOGFILE 2>&1
$REDISCLI -h 192.168.42.133 -p 6379 -a 1234qwer REPLICAOF 192.168.42.130 6379 >>$LOGFILE 2>&1
echo "Finished running redis_backup.sh..." >>$LOGFILE
5.3 Write a script that verifies whether Redis is started.
Write redis_check.sh script to determine by listening port number (master and standby are the same)
#!/bin/bash
LOGFILE=/var/log/
echo "Running redis_check.sh..." >> $LOGFILE
date >> $LOGFILE
CHECK=$(ss -tnlp|grep 6379)
if [ $? -ne 0 ]; then
echo "redis-server is not running..." >> $LOGFILE
systemctl stop
exit 1
else
echo "redis-server is running..." >> $LOGFILE
exit 0
fi
echo "Finished running redis_check.sh..." >> $LOGFILE
5.4 Other scripts
Write redis_fault.sh (master and backup are consistent)
#!/bin/bash
LOGFILE=/var/log/
echo "Running redis_fault.sh..." >>$LOGFILE
echo "[Fault]" >> $LOGFILE
date >> $LOGFILE
echo "Finished running redis_fault.sh..." >> $LOGFILE
Write redis_stop.sh (master and standby are consistent)
#!/bin/bash
LOGFILE=/var/log/
echo "Running redis_stop.sh...." >>$LOGFILE
echo "[Stop]" >> $LOGFILE
date >> $LOGFILE
echo "Finished running redis_stop.sh...." >>$LOGFILE
5.5 Granting Executable Privileges to Scripts
chmod +x /etc/keepalived/scripts_redis/*.sh
5.6. keepalived related commands
Keepalived Installation Commands
yum install keepalived -y
The directory where the Keepalived configuration resides
/etc/keepalived
Keepalived log files
/var/log/message
The Start Keepalived command
systemctl start
Restart Keepalived command
systemctl restart
View Keepalived Status Command
systemctl status
View Keepalived virtual VIP ip
ip addr
Shutting down the Keepalived command
systemctl stop
6、Verify the primary backup dual-activity
We can connect to the tool RedisDesktopManager to test dual-active master and backup. First of all, the connection address fill in the virtual IP address generated by keepalived: 192.168.42.161, enter the port number: 6379 and password: 1234qwer
After a successful connection, insert a piece of data for data testing. After that, stop the main server redis to simulate server downtime, and test the connection to continue data insertion. Then the main server redis start, keepalived also need to start. After the startup is completed, check whether the data is consistent. If it is consistent, it means that the master and backup dual-activation setup is successful.