Memcached + magent + preserved high availability cluster

I. Preface

Magent is an open-source proxy service software, through which we can realize the synchronization of cache data. Of course, the synchronization here does not mean that memcached can communicate with each other, but magent can connect multiple memcached nodes at the same time. Through the VIP bound by magent, log in to memcached from the client and write data, and the memcached data of other nodes will also be synchronized.

II. Architecture deployment

Environmental description:

1. Configure the memcache primary and secondary cache nodes (the configuration of the two servers is the same)

yum install gcc gcc-c++ make -y
1,tar zxvf memcached-1.5.6.tar.gz -C /opt/

2,tar zxvf libevent-2.1.8-stable.tar.gz -C /opt/

3,mkdir /opt/magent
tar zxvf magent-0.5.tar.gz -C /opt/magent/

2. Compile and install

cd    /opt/libevent-2.1.8-stable
./configure  --prefix=/usr/
make && make install

cd    /opt/memcached-1.5.6
./configure \
make && make install

3. Create a soft connection

ln   -s  /usr/lib/   /usr/lib64/

4. Deploy the master server - install the magent agent. The slave server does not need to
cd /opt/magent

vim ketama.h
 Add or remove the following code at the beginning:
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767

vim Makefile
LIBS = -levent -lm   //Plus -lm

5. make compilation after modification
6. At this time, after making is completed, the make executable program will be generated

7. Copy the magent program to the path environment variable

cp magent /usr/bin/

8. The installed magent can be copied to the slave server, so that the slave server does not need to be configured again

yum install openssh-clients -y   //Setup Toolkit 
scp magent root@

9. Install keepalived on both master and slave servers and deploy
(1) main server

yum install keepalived -y    //Install keepalived

To modify a profile:

vim /etc/keepalived/keepalived.conf

//Define a function. It is recommended to write it first
vrrp_script magent {
        script "/opt/shell/"
        interval 2

//Make the following modifications:
router_id MAGENT_HA        //Modify id name
interface ens33            //Modify network card information

virtual_ipaddress {     //Define the virtual ip address

vrrp_instance VI_1 {
//The following three lines of code are written in the vrrp module
track_script {

(2) from server

vim /etc/keepalived/keepalived.conf

//Make the following modifications:
router_id MAGENT_HB         //id name is different from the first one
state BACKUP               //from server
virtual_router_id 52       //id number is different from the first one
priority 90                 //Low priority and primary server

10. Configure the master and slave server scripts
(1) create a script

mkdir   /opt/shell

(2) preparation


K=`ps -ef | grep keepalived | grep -v grep | wc -l`
if [ $K -gt 0 ]; then
    magent -u root -n 51200 -l -p 12000 -s -b
pkill -9 magent

-n 51200             //Define the maximum number of user connections
-l   //Specify virtual IP
-p 12000             //Specify port number
-s                   //Specify primary cache server
-b                   //Specify from cache server

(3) add an execution permission to the script

chmod +x
systemctl stop firewalld.service   //Must be turned off or magent will not start
setenforce 0

(4) enable the maintained service

systemctl start keepalived.service

netstat -anpt | grep 12000 //Confirm that the magent is running and the port is running normally

(5) verification master and slave:
1. Primary server - view the / var/log/messages file and find the keyword: Transition to MASTER STATE

2. Find the keyword from the server: Entering BACKUP STATE

(6) ip addr command - confirm that the drift address is valid

Step 5: start the master and slave servers
(1) start the main server:

memcached -m 512k -u root -d -l -p 11211

(2) start the slave server:

memcached -m 512k -u root -d -l -p 11211
netstat -anptu | grep 11211

Step 6: client test
Install Telnet tool first:

yum install telnet -y

1. Use drift address to log in and connect:

2. We operate on the client, write a data, and observe whether the master and slave servers will generate synchronously:

(1) main server:

(2) from server:

3. Dual hot standby:
(1) stop the main server:

systemctl stop keepalived.service

(2) the client can still connect:

Tags: vim yum shell Makefile

Posted on Sat, 14 Dec 2019 13:19:07 -0500 by adnanhb