Memcached+keepalived+magent for master-slave replication and high availability

Source: Internet
Author: User
Tags memcached server port

Memcached+keepalived+magent implementation of Master-slave replication and high availability of experimental introduction

Memcached is not able to communicate directly before, so memcached itself does not fully back up all the data. At this time, I can use Magent proxy memcached to implement master-slave backup to ensure the integrity of the cached data.

This works in the middle as follows:

    • The data will go through magent before reaching the memcached server, and each time the data is written to the primary memcached server, the magent will be written to the slave server as well as the algorithm written by the master server.
    • When the primary memcached server goes down, Magent reads the data from the memcached server
    • When the memcached is restored, Magent will re-read the data from the main memcached, at which point the data is not available during the outage because the primary memcached has just returned to normal, which is also a drawback of magent.

Memcache master-slave replication refers to the primary memcached server to modify the data will be synchronized to the slave server, but memcached API client is unable to determine which memcached server to connect to, so you need to set the VIP address through keepalived, Provides access to Memcached API clients as well as magent connections.

Experimental environment
name IP Address
Memcached Primary Server
Memcached from the server
Experiment implementation Configuration memcached master-Slave server node (to be done on the master-slave server)

Installing memcached requires libevent support, so you need to install Libevent first. First extract the Libevent value to specify the directory:

[[email protected] mnt]# tar xf libevent-2.1.8-stable.tar.gz -C /opt/[[email protected] mnt]# cd /opt/libevent-2.1.8-stable/[[email protected] libevent-2.1.8-stable]# ./configure --prefix=/usr#进行配置[[email protected] libevent-2.1.8-stable]# make && make install#进行编译和安装

Install memcached:

[[email protected] mnt]# tar xf memcached-1.5.6.tar.gz -C /opt/#将软件包解压到指定目录中[[email protected] memcached-1.5.6]# ./configure --with-libevent=/usr#配置,开启libevent功能,后面加上上面安装的路径[[email protected] memcached-1.5.6]# make && make install#编译和安装

Set up the Libevent function module to a soft link to a directory that the system can identify

[[email protected] lib64]# ln -s /usr/lib/ /usr/lib64/

The above is the master and slave server need to install software, magent only need to install on the primary server, generate an executable file, copy this file to the slave server.

[[email protected] mnt]# tar xf magent-0.5.tar.gz -C /opt/#将magent解压到指定目录下[[email protected] opt]# vim ketama.h#修改这个文件#ifndef SSIZE_MAX#define SSIZE_MAX 32767#endif#修改文件最大值,将最后一行的#endif移动到头两行下面

[[email protected] opt]# vim Makefile #修改MakefileLIBS = -levent -lm#这句话是连接函数库,Linux的库命名是一致的,一般为,或libxxx.a,,那么你要链接某个库就用-lxxx,去掉头lib及"."后面的so,la,a等即可。#因为数学函数位于libm.so库文件中(通常在/lib目录下),-lm选项告诉编译器,我们程序中用到的数学函数要到这个库文件里找.CFLAGS = -Wall -O2 -gCC = gcc

[[email protected] opt]# make#生成一个可执行文件[[email protected] opt]# ln -s /opt/magent /usr/bin#把生成的mgent程序让系统识别,加入到环境变量中,方便系统识别,记得建立软链接一定要用绝对路径[[email protected] opt]# scp magent [email protected]:/usr/bin/#使用scp将生成的magent文件复制到从服务器/usr/bin目录下

Deploying keepalived
[[email protected] opt]# yum install keepalived -y#通过yum仓库安装keepalived[[email protected] opt]# vim /etc/keepalived/keepalived.conf #修改keepalived.conf配置文件

Other redundant configurations are removed directly, and the keepalived configuration file from the server is also modified.

Writing the master server Magent script
[[email protected] ~]# mkdir -p /opt/shell[[email protected] ~]# vim /opt/shell/!/bin/bashK=`ps -ef | grep keepalived | grep -v grep | wc -l`if [ $K -gt 0 ]; then        magent -u root -n 51200 -l -p 12000 -s -b -9 magentfi-n 51200 #定义用户最大连接数-l #指定虚拟IP-p 12000  #指定端口号-s #指定主缓存服务器-b #指定从缓存服务器#这个脚本意思就是一旦检测出系统进程中有keepalived进程,就执行magent这条命令,如果没有,则将magent进程杀死。总得来说就是,加入主服务器上没有keepslived进程,magent就会和从服务器连接。[[email protected] ~]# chmod +x /opt/shell/ #给脚本添加执行权限
Writing a magent script from the server
[[email protected] ~]# mkdir -p /opt/shell[[email protected] ~]# vim /opt/shell/!/bin/bashK=`ip addr | grep | grep -v grep | wc -l`if [ $K -gt 0 ]; then        magent -u root -n 51200 -l -p 12000 -s -b -9 magentfi  #从服务器的脚本意思就是一旦检测到虚拟ip漂移到自己身上,就会执行magent这条命令,目的就是在主服务器宕掉的时候,从服务器能够正常启动。~       

Start the keepalived and memcached services for the master and slave servers to view the primary server status.

View the status from the server.

Simulate downtime

At this time everyone must wonder why the main server port 12000 is still open, I think because we simulate the outage method is to stop the keepalived service, so the keepalived in the detection script will not be executed, so the inside of the pkill-9 Magent This command will not be carried out again.

Memcached+keepalived+magent for master-slave replication and high availability

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: 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.