memcached high availability cluster building

Posted by jdeer0618 on Mon, 23 Mar 2020 15:30:20 +0100

memcached high availability cluster building
We need three servers:

  1. Main server 192.168.247.206
  2. From server 192.168.247.160
  3. Client 192.168.247.161
    master server
    Configure memcached primary and secondary cache nodes
    [root@lamp ~]# hostnamectl set-hostname master
    [root@lamp ~]# su
    [root@master ~]# 
    [root@master ~]# mkdir /abc
    mkdir: cannot create directory '/abc': File exists
    [root@master ~]# mount.cifs //192.168.254.10/linuxs /abc
    Password for root@//192.168.254.10/linuxs:  
    [root@master ~]# cd /abc
    [root@master abc]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt
    [root@master abc]# tar zxvf memcached-1.5.6.tar.gz -C /opt
    [root@master abc]# mkdir /opt/magent
    [root@master abc]# tar zxvf magent-0.5.tar.gz -C /opt/magent/
    [root@master abc]# yum install gcc gcc-c++ make -y

    from server
    magent does not need to be installed from the server

    [root@nginx ~]# hostnamectl set-hostname slave
    [root@nginx ~]# su
    [root@slave ~]# 
    [root@slave ~]# mkdir /abc
    mkdir: cannot create directory '/abc': File exists
    [root@slave ~]# mount.cifs //192.168.254.10/linuxs /abc
    Password for root@//192.168.254.10/linuxs:  
    [root@slave ~]# cd /abc
    [root@slave abc]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt
    [root@slave abc]# tar zxvf memcached-1.5.6.tar.gz -C /opt
    [root@slave abc]# yum install gcc gcc-c++ make -y

    Back to main server

    [root@master abc]# cd /opt
    [root@master opt]# ls
    libevent-2.1.8-stable  magent  memcached-1.5.6  mysql-5.7.17  rh
    [root@master opt]# cd libevent-2.1.8-stable/
    [root@master libevent-2.1.8-stable]# ./configure --prefix=/usr
    [root@master libevent-2.1.8-stable]# make && make install
    [root@master libevent-2.1.8-stable]# cd /opt/memcached-1.5.6/
    [root@master memcached-1.5.6]# ./configure --with-libevent=usr
    [root@master memcached-1.5.6]# make && make install

    Switch to from server

    [root@slave abc]# cd /opt
    [root@slave opt]# ls
    data  libevent-2.1.8-stable  memcached-1.5.6  nginx-1.12.2  rh
    [root@slave opt]# cd libevent-2.1.8-stable/
    [root@slave libevent-2.1.8-stable]# ./configure --prefix=/usr
    [root@slave libevent-2.1.8-stable]# make && make install
    [root@slave libevent-2.1.8-stable]# cd /opt/memcached-1.5.6/
    [root@slave memcached-1.5.6]# ./configure -with-libevent=/usr
    [root@slave memcached-1.5.6]# make && make install

    Configure magent on the primary server

    [root@master memcached-1.5.6]# cd /opt/magent/
    [root@master magent]# ls
    ketama.c  ketama.h  magent.c  Makefile
    [root@master magent]# vim ketama.h 
    //Modify the first three lines
    #ifndef SSIZE_MAX
    #define SSIZE_MAX 32767
    #endif      
    //The last line has an endif, delete it
    [root@master magent]# vim Makefile 
    //First line modification
    LIBS = -levent -lm
    [root@master magent]# make
    [root@master magent]# ls
    ketama.c  ketama.h  ketama.o  magent  magent.c  magent.o  Makefile
    //Add the magent executable, install ssh, and copy the file to the slave server
    [root@master magent]# yum install openssh-clients -y
    [root@master magent]# cp magent /usr/bin/
    [root@master magent]# scp magent root@192.168.247.160:/usr/bin
    [root@master magent]# systemctl stop firewalld
    [root@master magent]# setenforce 0
    #Turn off firewall and enhanced security

    The master and slave servers start to install keepalived
    [root@master magent]# yum install keepalived -y
    [root@slave memcached-1.5.6]# yum install keepalived -y
    Modify master profile

    [root@master magent]# vim /etc/keepalived/keepalived.conf 
    //Delete the original, re-enter, delete the previous copy
    ! Configuration File for keepalived
    vrrp_script magent {
        script "/opt/shell/magent.sh"
        interval 2
    }
    global_defs {
    notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
    }   
    notification_email_from Alexandre.Cassen@firewall.loc
    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
    }   
    track_script {
        magent
    }
    virtual_ipaddress {
        192.168.247.188
    }   
    }
    [root@slave memcached-1.5.6]# cd /etc/keepalived/
    [root@slave keepalived]# ls
    keepalived.conf
    [root@slave keepalived]# mv keepalived.conf keepalived.conf.bk
    [root@slave keepalived]# yum install openssh-clients -y
    [root@slave keepalived]# scp root@192.168.247.206:/etc/keepalived/keepalived.conf /etc/keepalived/
    [root@slave keepalived]# vim keepalived.conf
    //modify
    17    router_id MAGENT_HB
    21     state BACKUP
    23     virtual_router_id 52
    24     priority 95
    [root@master magent]# mkdir /opt/shell
    [root@master magent]# cd /opt/shell/
    [root@master shell]# ls
    [root@master shell]# vim magent.sh
    #!/bin/bash
    K=`ps -ef | grep keepalived | grep -v grep | wc -l`
    if [ $K -gt 0 ]; then
        magent -u root -n 51200 -l 192.168.247.188 -p 12000 -s 192.168.247.206:11211 -b 192.168.247.160:11211
    else
    pkill -9 magent
    fi
    [root@master shell]# chmod +x magent.sh 
    [root@master shell]# ls
    magent.sh
    [root@master shell]# ln -s /lib/libevent-2.1.so.6* /usr/lib64/
    [root@master shell]# systemctl start keepalived.service 
    [root@master lib64]# netstat -natp | grep 12000
    tcp        0      0 192.168.247.188:12000   0.0.0.0:*               LISTEN      16635/magent        

    127 status code error appears, magent shows no file or directory, create soft connection
    that will do

    [root@master shell]# magent -u root -n 51200 -l 192.168.247.188 -p 12000 -s 192.168.247.206:11211 -b 192.168.247.160:11211
    magent: error while loading shared libraries: libevent-2.1.so.6: cannot open shared object file: No such file or directory
    ln -s /lib/libevent-2.1.so.6* /usr/lib64/

    Verify it.

    [root@master lib64]# cat /var/log/messages | grep "Transition to MASTER STATE"
    Feb  1 23:10:46 lamp Keepalived_vrrp[101923]: VRRP_Instance(VI_1): Transition to MASTER STATE

    Verify that the drift address is valid

    [root@master lib64]# ip addr
    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:d6:c0:8a brd ff:ff:ff:ff:ff:ff
    inet 192.168.247.206/24 brd 192.168.247.255 scope global dynamic ens33
       valid_lft 5438031sec preferred_lft 5438031sec
    inet 192.168.247.188/32 scope global ens33
    [root@slave bin]# mkdir /opt/shell
    [root@slave bin]# cd /opt/shell/
    [root@slave shell]# vim magent.sh
    #!/bin/bash
    K=`ps -ef | grep keepalived | grep -v grep | wc -l`
    if [ $K -gt 0 ]; then
        magent -u root -n 51200 -l 192.168.247.188 -p 12000 -s 192.168.247.206:11211 -b 192.168.247.160:11211
    else
    pkill -9 magent
    fi
    [root@slave shell]# chmod +x magent.sh 
    [root@slave shell]# ln -s /lib/libevent-2.1.so.6* /usr/lib64/
    [root@slave shell]# systemctl start keepalived.service 
    [root@slave shell]# netstat -natp | grep 12000
    tcp        0      0 192.168.247.188:12000   0.0.0.0:*               LISTEN      16132/magent  
    [root@slave shell]# ip addr
    2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc 
    inet 192.168.247.188/32 scope global ens33

    Start memcached service

    [root@master lib64]# memcached -m 512k -u root -d -l 192.168.247.206 -p 11211
    [root@master lib64]# netstat -natp | grep 11211
    tcp        0      0 192.168.247.206:11211   0.0.0.0:*               LISTEN      36267/memcached  
    [root@slave shell]# memcached -m 512k -u root -d -l 192.168.247.160 -p 11211
    [root@slave shell]# netstat -natp | grep 11211
    tcp        0      0 192.168.247.160:11211   0.0.0.0:*               LISTEN      20928/memcached   

    Local connection

    [root@master lib64]# yum install telnet -y
    [root@master lib64]# telnet 192.168.247.206 11211
    Trying 192.168.247.206...
    Connected to 192.168.247.206.
    Escape character is '^]'.
    [root@slave shell]# yum install telnet -y
    [root@slave shell]# telnet 192.168.247.160 11211
    Trying 192.168.247.160...
    Connected to 192.168.247.160.
    Escape character is '^]'.

    Verify client

    [root@client ~]# yum install telnet -y
    [root@client ~]# telnet 192.168.247.188 12000
    Trying 192.168.247.188...
    Connected to 192.168.247.188.
    Escape character is '^]'.
    add username 0 0 7
    1234567
    STORED

    At this time, the client exits memcached first, and the primary server is disconnected

    quit
    Connection closed by foreign host.
    [root@master lib64]# systemctl stop keepalived.service 
    [root@master lib64]# 

    Still available on the client

    [root@client ~]# telnet 192.168.247.188 12000
    Trying 192.168.247.188...
    Connected to 192.168.247.188.
    Escape character is '^]'.
    get username
    VALUE username 0 7
    1234567
    END

Topics: shell yum vim firewall