Keepalived enables Redis dual-master backup

Source: Internet
Author: User

Now the production environment basically uses the main preparation method, and how to realize the master of Redis? In general, the Redis implementation of master-slave replication is relatively simple, only need to configure the configuration file from the server "Slaveof" can be. However, there is a problem: when the primary server is hung up, the business address can be temporarily transferred to the slave server, but cannot be written from the server. To solve this problem, we found some information on the Internet. There are several main scenarios

    1. Using keepalived+ monitoring scripts for primary and standby switching

    2. Using Rediscluster to achieve a dual-master switch, the principle is to imitate the bin-log mechanism of MySQL.

This article describes the first scenario

Design ideas

When Master and Slave are working normally, Master is responsible for service, Slave is responsible for standby;
When Master hangs up, Slave is normal, Slave takes over the service and shuts down the master-slave copy function;
When Master returns to normal, the data is synchronized from slave, the master-slave replication function is turned off after the data is synchronized, and the master identity is restored, while slave waits for the master synchronization data to complete before resuming slave identity.
Then loop in turn.

Environment Introduction

master:192.168.3.185

slave:192.168.3.186

vip:192.168.3.188

Implementation steps

1. Install keepalived service on two hosts separately

#yum-y Install keepalived

2. Modify the keepalived configuration file

Master

Global_defs {
Notification_email {
[Email protected]
[Email protected]
[Email protected]
}
Notification_email_from [email protected]
Smtp_server 127.0.0.1
Smtp_connect_timeout 30
router_id Lvs_devel

}


Vrrp_script Chk_redis {

Script "/etc/keepalived/scripts/redis_check.sh"
Interval 2
}

Vrrp_instance Vi_1 {
State MASTER
Interface Em1
VIRTUAL_ROUTER_ID 51
Priority 101
Authentication {
Auth_type PASS
Auth_pass Redis
}
Track_script {
Chk_redis
}
virtual_ipaddress {
192.168.3.188
}
notify_master/etc/keepalived/scripts/redis_master.sh
notify_backup/etc/keepalived/scripts/redis_backup.sh
notify_fault/etc/keepalived/scripts/redis_fault.sh
notify_stop/etc/keepalived/scripts/redis_stop.sh
}


Slave

Global_defs {
Notification_email {
[Email protected]
[Email protected]
[Email protected]
}
Notification_email_from [email protected]
Smtp_server 127.0.0.1
Smtp_connect_timeout 30
router_id Lvs_devel
}

Vrrp_script Chk_redis {
Script "/etc/keepalived/scripts/redis_check.sh"
Interval 2
}

Vrrp_instance Vi_1 {
State BACKUP
Interface Em1
VIRTUAL_ROUTER_ID 51
# mcast_src_ip 192.168.3.186
Priority 100
Authentication {
Auth_type PASS
Auth_pass Redis
}
Track_script {
Chk_redis
}
virtual_ipaddress {
192.168.3.188
}
notify_master/etc/keepalived/scripts/redis_master.sh
notify_backup/etc/keepalived/scripts/redis_backup.sh
notify_fault/etc/keepalived/scripts/redis_fault.sh
notify_stop/etc/keepalived/scripts/redis_stop.sh

}

3. Scripting

First create a Redis-monitoring script on both servers

#!/bin/bash
NETSTAT-NTLP |grep 6379
If ["$?" = = "0"]; Then
echo $?
Exit 0
Else
echo $?
Exit 1
Fi

And then these are the following

notify_master/etc/keepalived/scripts/redis_master.sh
notify_backup/etc/keepalived/scripts/redis_backup.sh
notify_fault/etc/keepalived/scripts/redis_fault.sh
notify_stop/etc/keepalived/scripts/redis_stop.sh

When the keepalived enters the master state, the notify_master is executed;

When the keepalived enters the backup state, the notify_backup is executed;

When the keepalived enters the fault state, the notify_fault is executed;

When the keepalived enters the stop state, the notify_stop is executed;

Write the following two scripts on the primary server

redis_master.sh

#!/bin/bash
Rediscli= "/usr/local/bin/redis-cli"
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 192.168.3.186 6379 >> $LOGFILE 2>&1
Sleep #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run slaveof NO one cmd ..." >> $LOGFILE
$REDISCLI slaveof NO one >> $LOGFILE 2>&1

redis_backup.sh

#!/bin/bash
Rediscli= "/usr/local/bin/redis-cli"
Logfile= "/var/log/keepalived-redis-state.log"
echo "[Backup]" >> $LOGFILE
Date >> $LOGFILE
echo "Being slave ..." >> $LOGFILE 2>&1
Sleep 15 # Delay 15 seconds for data to be synchronized before switching between master and slave roles
echo "Run slaveof cmd ..." >> $LOGFILE
$REDISCLI slaveof 192.168.3.186 6379 >> $LOGFILE 2>&1

Write the following two scripts from the server

#!/bin/bash
Rediscli= "/usr/local/redis/bin/redis-cli"
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 192.168.3.185 6379 >> $LOGFILE 2>&1
Sleep #延迟10秒以后待数据同步完成后再取消同步状态
echo "Run slaveof NO one cmd ..." >> $LOGFILE
$REDISCLI slaveof NO one >> $LOGFILE 2>&1

redis_backup.sh

#!/bin/bash
Rediscli= "/usr/local/redis/bin/redis-cli"
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 192.168.3.185 6379 >> $LOGFILE 2>&1

Master and slave write the following two scripts together

redis_fault.sh

#!/bin/bash
Logfile=/var/log/keepalived-redis-state.log
echo "[Fault]" >> $LOGFILE
Date >> $LOGFILE

redis_stop.sh

#!/bin/bash
Logfile=/var/log/keepalived-redis-state.log
echo "[Stop]" >> $LOGFILE
Date >> $LOGFILE

Add Execute Permissions

#chmod +x *.sh


4. Start and verify

A. Start Redis-server

B. Start keepalived

Use command IP A to view VIPs

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/77/F6/wKiom1ZyRgvwN7sYAAA6KG9E_6Y908.png "title=" Master.png "alt=" Wkiom1zyrgvwn7syaaa6kg9e_6y908.png "/>

C. Access the VIP and write the data

#/usr/local/redis/bin/redis-cli-h 192.168.3.188 Set Hello 1
Ok
D. Simulate the main service down, access the VIP and write the data

Main service crying off Redis process

#pkill-9 Redis

View VIP

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/77/F6/wKiom1ZyRvKQ8geNAAA4KLisfvk988.png "title=" Slave.png "alt=" Wkiom1zyrvkq8genaaa4klisfvk988.png "/>

Access VIP

#/usr/local/redis/bin/redis-cli-h 192.168.3.188 Get Hello
"1"

Write Data

#/usr/local/redis/bin/redis-cli-h 192.168.3.188 Set Hello2 2
Ok


E Restore the Master service and view write add data

#/usr/local/redis/bin/redis-cli-h 192.168.3.188 Get Hello2
"2"


At this point, the dual-master backup is successfully implemented.


This article is from the "Zhijun" blog, make sure to keep this source http://huangzhijun.blog.51cto.com/482881/1725606

Keepalived enables Redis dual-master backup

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.