Magent + keepalived implement memcached highly available clusters

Source: Internet
Author: User

Features of memcached:
  1. There is no limit to the amount of item data that can be saved in memcached, as long as the memory is sufficient;
  2. memcached single process maximum use memory is 2GB, to use more memory, you can start multiple memcached processes on different ports respectively;
  3. Memcached is a non-blocking socket communication service, based on the Libevent library, because of non-blocking communication, memory read and write speed is very fast;
  4. Memcached is divided into servers and clients, can be configured with multiple servers and clients, which is widely used in distributed services;
  5. Memcached as a small-scale data distributed platform is very efficient;
memcached Existing problems:

In itself, there is no built-in distributed function, can not realize the use of multiple MEMCACHD servers to store different data, maximize the use of the same resources, unable to synchronize data, easy to cause a single point of failure, where the memcached agent to achieve cluster function;

Cluster

Because there is no communication between the mamcached server and the server, and no data copy backups are made, there is a single point of failure when any server node fails, and if high availability is required, it needs to be done in other ways. Here through the Magent cache proxy, to prevent a single point of the phenomenon, the cache proxy can also do backup functions, through the client connection to the cache proxy Server, cache proxy Connection Cache connection recovery period, The cache proxy server can connect multiple memcached machines to synchronize data for each memcached machine, and if one of the cache servers is down, the system can still continue to work if one of the memcached machines is down, Data is not lost and data integrity can be guaranteed.

Architecture diagram

System environment
Host name Operating System IP Address Package
Memcached1 Centos 7.3 x86_64 192.168.96.16 Libevent-2.1.8-stable.tar.gz, memcached-1.5.9.tar.gz, magent-0.5.tar.gz, keepalived
Memcached2 Centos 7.3 x86_64 192.168.96.17 Libevent-2.1.8-stable.tar.gz, memcached-1.5.9.tar.gz, keepalived
Client Centos 7.3 x86_64 192.168.96.22 Telnet

Related software package: Baidu Cloud Disk Password: B7za

Start deployment configuration Memcached Primary cache node and-----Two configurations from cache Node 1. Shutting down the firewall and selinux[important]
setenforce 0systemctl stop firewalld.service
2. Install the Environment pack
yum -y install gcc gcc-c++ make
3. Compile and install libevent (must be installed first)
#解压tar zxvf libevent-2.1.8-stable.tar.gz -C /opt#切换到libevent目录cd /opt/libevent-2.1.8-stable/#配置./configure --prefix=/usr/#编译及安装make && make install
3. Compile and install memcached
#解压tar zxvf memcached-1.5.9.tar.gz -C /opt/#切换到memcachedcd /opt/memcached-1.5.9/#配置(指定libevent路径)./configure --with-libevent=/usr/#编译及安装make && make install
4. Optimized startup (mencached that support the copy function need to install the Libevent-2.1.so.6 module, otherwise the boot service will be error)
ln -s /usr/lib/libevent-2.1.so.6  /usr/lib64/libevent-2.1.so.6
5. Start memcached on two servers, respectively
#主服务器memcached -m 512k -u root -d -l 192.168.96.16 -p 11211#从服务器memcached -m 512k -u root -d -l 192.168.96.17 -p 11211
6. Check if memcached is started
netstat -ntap | grep 11211

Magent installed on the primary server, do not install the installation magent1 from the server. Create a magent directory

Mkdir/opt/magent

2. Unzip

Tar zxvf magent-0.5.tar.gz-c/opt/magent

3. Switch to the directory

Cd/opt/magent

4. Let the dynamic link library be shared with the system

/sbin/ldconfig

5. Edit Ketama.h, add File header information

Vim Ketama.h

    #ifndef SSIZE_MAX    #define SSIZE_MAX 32767    #endif

6. Modify Makefile
sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile

7. Compile (a manage executable will be generated when it is completed)
make

8. Copy mgent command to System management, easy to use
cp /opt/magent/magent /usr/bin/
9. Send to Slave server
scp /opt/magent/magent [email protected]:/usr/bin
Installing keepalived
yum -y install keepalived
1. Edit the keepalived configuration file

Vim/etc/keepalived/keepalived.conf

! Configuration File for keepalivedglobal_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_H1              #指定router_id}vrrp_script magent {                #定义函数    script "/root/shell/magent.sh"    interval 2}vrrp_instance VI_1 {    state MASTER    interface ens33             #本地网卡名称    virtual_router_id 51        #id主从一致    priority 100                #主优先级大于从优先级    advert_int 1    authentication {        auth_type PASS        auth_pass 1111    }track_script {              #调用函数        magent    }virtual_ipaddress {         #vip地址        192.168.96.100    }}
2. Create a Shell directory
mkdir /root/shell
3. Create a magent.sh script

vim/root/shell/magent.sh

#!/bin/bashKeepalived=`ps -ef | grep keepalived | grep -v grep | wc -l`if [ $Keepalived -gt 0 ]; then        magent -u root -n 51200 -l 192.168.96.100 -p 12000 -s 192.168.96.16:11211 -b 192.168.96.17:11211else        pkill -9 magentfi

Parameter description:

        -n 51200                #定义用户最大连接数        -l 192.168.175.188                #指定虚拟IP        -p 12000                   #指定端口号        -s                              #指定主缓存服务器        -b                              #指定从缓存服务器
4. Assigning Script Execution permissions
chmod +x /root/shell/magent.sh
5. Start the keepalived service
systemctl enable keepalived.servicesystemctl start keepalived.service
6. Check the keepalived service status
systemctl status keepalived.service

7. Check VIP Address
ip addr

Operating from the server 1. Install the keepalived
yum -y install keepalived
2. Copy the keepalived configuration file from the primary server
3. Edit the keepalived configuration file

Vim keepalived.conf

! Configuration File for keepalivedglobal_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_H2              #指定router_id}vrrp_script magent {                #定义函数    script "/root/shell/magent.sh"    interval 2}vrrp_instance VI_1 {    state BACKUP    interface ens33             #本地网卡名称    virtual_router_id 51        #id主从一致    priority 90             #主优先级大于从优先级    advert_int 1    authentication {        auth_type PASS        auth_pass 1111    }    virtual_ipaddress {         #vip地址        192.168.96.100    }    track_script {              #调用函数        magent    }}
4. Create a new Shell directory
mkdir /root/shell
5. Create a magent.sh script

vim/root/shell/magent.sh

#!/bin/bashKeepalived=`ip addr | grep 192.168.96.100 | grep -v grep | wc -l`if [ $Keepalived -gt 0 ]; then        magent -u root -n 51200 -l 192.168.96.100 -p 12000 -s 192.168.96.12:11211 -b 192.168.96.17:11211else        pkill -9 magentfi
6. Assigning Script Execution permissions
chmod +x /root/shell/magent.sh
7. Start the keepalived service
systemctl enable keepalived.servicesystemctl start keepalived.service
8. Check the keepalived service status
systemctl status keepalived.service

9. Check VIP Address
ip addr

In the client Test 1. Install the Telnet client
yum -y install telnet
2. Connecting memcached
telnet 192.168.175.188 12000
Test replication functionality
//连接vip进行测试,插入user键值[[email protected] ~]# telnet 192.168.96.100 12000Trying 192.168.96.100...Connected to 192.168.96.100.Escape character is ‘^]‘.set user 0 0 5test1STOREDget userVALUE user 0 5test1ENDquitConnection closed by foreign host.//连接Memcached1,查询user键值,可以获取,成功![[email protected] ~]# telnet 192.168.96.16 11211Trying 192.168.96.16...Connected to 192.168.96.16.Escape character is ‘^]‘.get userVALUE user 0 5test1ENDquitConnection closed by foreign host.//连接Memcached2,查询user键值,也可以获取,成功![[email protected] ~]# telnet 192.168.96.17 11211Trying 192.168.96.17...Connected to 192.168.96.17.Escape character is ‘^]‘.get userVALUE user 0 5test1ENDquitConnection closed by foreign host.
Test single point of failure

Magent + keepalived implement memcached highly available clusters

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.