memcached高可用群集搭建 我们需要三台服务器:

  1. 主服务器 192.168.247.206
  2. 从服务器 192.168.247.160
  3. 客户端 192.168.247.161 主服务器 配置memcached主缓存节点和从缓存节点
[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

从服务器 从服务器不需要安装magent

[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

回到主服务器

[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

切换到从服务器

[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

在主服务器上配置magent

[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 
//开头三行修改
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif		
//最后一行有#endif,把它删除
[root@master magent]# vim Makefile 
//首行修改
LIBS = -levent -lm
[root@master magent]# make
[root@master magent]# ls
ketama.c  ketama.h  ketama.o  magent  magent.c  magent.o  Makefile
//增加magent 可执行文件,安装ssh,把这个文件复制到从服务器上
[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
#关闭防火墙和增强型安全功能

主从服务器开始安装keepalived [root@master magent]# yum install keepalived -y [root@slave memcached-1.5.6]# yum install keepalived -y 修改主配置文件

[root@master magent]# vim /etc/keepalived/keepalived.conf 
//删除原有,重新输入,删除之前备个份
! 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
//修改
 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状态码报错,magent 显示没有文件或目录,创建软连接 即可

[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/

验证一下

[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

验证漂移地址是否生效

[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

开启memcached服务

[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   

本机连接本地

[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 '^]'.

验证客户端

[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

此时,客户端先退出memcached,断掉主服务器

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

客户机上依旧可以使用

[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