Deploying memcached Primary Master replication +keepalived highly available architecture on CentOS7

Source: Internet
Author: User

Principle:

Memcached primary master replication refers to any one memcached server modified data will be synchronized to another, but Memcached API client is unable to determine which memcached server to connect to, so you need to set up a VIP address, Provided to the Memcached API client for connection. You can use the VIP address generated by keepalived to connect to the primary memcached server and provide a highly available architecture.

Using two memcached server, a client to complete, the experimental environment table is as follows:

1. Configure the Memcached Primary cache node and-----Two configurations from the cache node

 [[email protected] ~]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt/   //解包// [[email protected] ~]# tar zxvf memcached-1.5.6.tar.gz -C /opt/ [[email protected] ~]# mkdir /opt/magent [[email protected] ~]# tar zxvf magent-0.5.tar.gz -C /opt/magent/  [[email protected] opt]#cd libevent-2.1.8-stable/ [[email protected] libevent-2.1.8-stable]# yum install gcc gcc-c++ make -y [[email protected] libevent-2.1.8-stable]# ./configure --prefix=/usr [[email protected] libevent-2.1.8-stable]# make && make install [[email protected] libevent-2.1.8-stable]# cd ../memcached-1.5.6/ [[email protected] memcached-1.5.6]# ./configure --with-libevent=/usr [[email protected] memcached-1.5.6]# ln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6    //软链接//

2. Turn off the firewall and turn on the memcached service

[[email protected] memcached-1.5.6]# systemctl stop firewalld.service [[email protected] memcached-1.5.6]# setenforce 0[[email protected] memcached-1.5.6]# memcached -d -m 32m -p 11211 -u root[[email protected] memcached-1.5.6]# netstat -ntap | grep 11211tcp        0      0 0.0.0.0:11211           0.0.0.0:*               LISTEN      11224/memcached     tcp6       0      0 :::11211                :::*                    LISTEN      11224/memcached

3. Install magent on the primary server

[[email protected] memcached-1.5.6]# cd /opt/magent/[[email protected] magent]# lsketama.c  ketama.h  magent.c  Makefile[[email protected] magent]# vim ketama.h#ifndef SSIZE_MAX#define SSIZE_MAX 32767#endif[[email protected] magent]# vim Makefile LIBS = -levent -lm //第一行末尾加-lm (不是数字1LIBS = -levent -lmCFLAGS = -Wall -O2 -g[[email protected] magent]# makegcc -Wall -O2 -g  -c -o magent.o magent.cgcc -Wall -O2 -g  -c -o ketama.o ketama.cgcc -Wall -O2 -g -o magent magent.o ketama.o -levent -lm

4. Make the system aware of the generated Mgent program

ls一下可看到magent可执行程序[[email protected] magent]# lsketama.c  ketama.h  ketama.o  magent  magent.c  magent.o  Makefile[[email protected] magent]# cp magent /usr/bin/

5. Copy the resulting magent file directly to the slave server.

6. Install keepalived and modify the default configuration file.

[[email protected] bin]# yum install keepalived -y[[email protected] bin]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalivedvrrp_script magent {        script "/opt/shell/magent.sh"        interval 2}global_defs {   notification_email {     [email protected]     [email protected]     [email protected]   }   notification_email_from [email protected]   smtp_server 192.168.200.1   smtp_connect_timeout 30   router_id MAGENT_HA    //主服务器名称//}vrrp_instance VI_1 {    state MASTER    interface ens33     //网卡名称//    virtual_router_id 51    priority 100    //优先级//    advert_int 1    authentication {        auth_type PASS        auth_pass 1111       }    virtual_ipaddress {        192.168.126.188     //虚拟IP//    }track_script {        magent     //函数//}}

7. Install keepalived from the server and modify the configuration file.

[[email protected] bin]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalivedvi keepalived.confvrrp_script magent {        script "/opt/shell/magent.sh"        interval 2}global_defs {   notification_email {     [email protected]     [email protected]     [email protected]   }   notification_email_from [email protected]   smtp_server 192.168.200.1   smtp_connect_timeout 30   router_id MAGENT_HB      //从服务器的名称//}vrrp_instance VI_1 {    state BACKUP            //从服务器的热备状态要修改成BACKUP//    interface ens33  //网卡名称//    virtual_router_id 52    //不能与主服务器相同//    priority 90       //从调度器的优先级要小于主的//    advert_int 1    authentication {        auth_type PASS        auth_pass 1111    }    virtual_ipaddress {        192.168.126.188     //虚拟IP//    }track_script {        //函数//        magent  }}

8. Set the Magent management script on the primary server

[[email protected] bin]# mkdir /opt/shell[[email protected] bin]# vim /opt/shell/magent.sh#!/bin/bashK=`ps -ef | grep keepalived | grep -v grep | wc -l`if [ $K -gt 0 ]; then        magent -u root -n 51200 -l 192.168.126.188 -p 12000 -s 192.168.126.138:11211 -b 192.168.126.166:11211elsepkill -9 magentfi参数注解:-n 51200 //定义用户最大连接数-l 192.168.126.188 //指定虚拟IP-p 12000  //指定端口号-s //指定主缓存服务器-b //指定从缓存服务器[[email protected] shell]# chmod +x magent.sh   // 增加执行权限//

9. Working from the server

[[email protected] bin]# mkdir /opt/shell[[email protected] bin]# cd /opt/shell/[[email protected] shell]# vim magent.sh[[email protected] shell]# vim magent.sh脚本内容如下,与主服务器脚本有区别!#!/bin/bashK=`ip addr | grep 192.168.126.188 | grep -v grep | wc -l`if [ $K -gt 0 ]; then        magent -u root -n 51200 -l 192.168.126.188 -p 12000 -s 192.168.126.138:11211 -b 192.168.126.166:11211elsepkill -9 magentfi  

10. Start validation

1) Start the primary server

[[email protected] shell]# systemctl start keepalived.service [[email protected] shell]# netstat -ntap | grep 12000  //确认magent运行//tcp        0      0 192.168.126.188:12000   0.0.0.0:*               LISTEN      

2) Start from server

[[email protected] shell]# systemctl start keepalived.service [[email protected] shell]# netstat -ntap | grep 12000tcp        0      0 192.168.126.188:12000   0.0.0.0:*               LISTEN      11716/magent  

3) Simple authentication replication with Telnet on the primary server

[[email protected] shell]# telnet 192.168.126.188 12000  //用漂移地址登陆服务//Trying 192.168.126.188...Connected to 192.168.126.188.Escape character is ‘^]‘.add username 0 0 7      //添加一条键值数据//1234567STORED在从服务器上查看[[email protected] shell]# telnet 192.168.126.188 12000 Trying 192.168.126.188...Connected to 192.168.126.188.Escape character is ‘^]‘.get username    //查看键值数据VALUE username 0 71234567         //内容存在,写入成功//END

11. Login Service with drift address on client

[[email protected] ~]# yum install telnet -y[[email protected] ~]# telnet 192.168.126.188 12000 Trying 192.168.126.188...Connected to 192.168.126.188.Escape character is ‘^]‘.add username 0 0 8    //添加一条键值数据//12345678STORED

1) See if the write succeeds on the primary server and from the server.

主服务器get usernameVALUE username 0 812345678END从服务器get usernameVALUE username 0 812345678END

2) Stop the primary server business does not affect

[[email protected] shell]# systemctl stop keepalived.service[[email protected] shell]# ip addrinet 192.168.126.138/24 brd 192.168.126.255 scope global dynamic ens33

3) Viewing from the server

[[email protected] shell]# ip addrinet 192.168.126.166/24 brd 192.168.126.255 scope global dynamic ens33       valid_lft 1146sec preferred_lft 1146sec    inet 192.168.126.188/32 scope global ens33可以看到漂移地址已经转移到从服务器上了,说明从已接受工作。

3) Open the main server again

[[email protected] shell]# systemctl start keepalived.service [[email protected] shell]# ip addrinet 192.168.126.138/24 brd 192.168.126.255 scope global dynamic ens33       valid_lft 1145sec preferred_lft 1145sec    inet 192.168.126.188/32 scope global ens33       valid_lft forever preferred_lft forever漂移地址再次转移到主服务器上,接手地址,服务依然不受影响。

Experimental success

Deploying memcached Primary Master replication +keepalived highly available architecture on CentOS7

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.