环境两台memcached集群服务器:

192.168.80.100
192.168.80.101

关闭防火墙和selinux

systemctl stop firewalld
setenforce 0

编译环境

yum -y install gcc gcc-c++ make -y

安装依赖包libevent

tar xf libevent-2.1.8-stable.tar.gz -C /opt //依赖包
tar xf memcached-1.5.6.tar.gz -C /opt/ //安装包

编译libevent

cd /opt/libevent-2.1.8-stable/
配置
./configure --prefix=/usr/local/libevent
编译及安装
make && make install

编译安装memcached

切换到memcached目录
cd /opt/memcached-1.5.6/
配置(指定安装目录和libevent路径)
./configure \
--prefix=/usr/local/memcached \
--with-libevent=/usr/local/libevent/
make && make install

优化启动

优化启动(支持复制功能的Mencached需要安装后的libevent-2.1.so.6模块,不然启动服务会报错)

ln -s /usr/local/libevent/lib/libevent-2.1.so.6.0.2 /usr/lib64/libevent-2.1.so.6

分别在两台服务器上启动memcached

主服务器
./memcached -m 512k -u root -d -l 192.168.80.100 -p 11211
从服务器
./memcached -m 512k -u root -d -l 192.168.80.101 -p 11211

检查是否开启成功

linux memcached集群
linux memcached集群

magent在主服务器安装,从服务器不用安装
编译安装magent

创建magent目录
mkdir /opt/magent
解压
tar xf magent-0.5.tar.gz -C /opt/magent
切换到目录
cd /opt/magent
让动态链接库为系统所共享
/sbin/ldconfig

编辑ketama.h,添加文件头部信息

vi ketama.h
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
linux memcached集群

修改Makefile

vi Makefile //libevent安装路径在/usr/local/libevent中使用以下内容:
LIBS = -levent -lm -L /usr/local/libevent/lib
INCLUDE=-I /usr/local/libevent/include
linux memcached集群

复制mgent命令到系统管理中,便于使用

cp /opt/magent/magent /usr/bin/

发送至从服务器

scp /opt/magent/magent [email protected]:/usr/bin

安装keepalived

yum -y install keepalived

编辑keepalived配置文件

vi /etc/keepalived/keepalived.conf
linux memcached集群_第1张图片
! Configuration File for keepalived

global_defs {
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.80.188
}

}

建立shell目录

mkdir /root/shell

创建magent.sh脚本

vi /root/shell/magent.sh
linux memcached集群_第2张图片

#!/bin/bash

Keepalived=ps -ef | grep keepalived | grep -v grep | wc -l
if [ $Keepalived -gt 0 ]; then
magent -u root -n 51200 -l 192.168.80.188 -p 12000 -s 192.168.80.100:11211 -b 192.168.80.101:11211
else
pkill -9 magent
fi

magent参数

-n 51200 #定义用户最大连接数
-l 192.168.175.188 #指定虚拟IP
-p 12000 #指定端口号
-s #指定主缓存服务器
-b #指定从缓存服务器

赋予脚本执行权限

chmod +x /root/shell/magent.sh

启动keepalived服务

systemctl enable keepalived.service
systemctl start keepalived.service

检查keepalived服务状态

systemctl status keepalived.service

检查ip地址

ip addr show ens32
linux memcached集群_第3张图片

从服务器安装keepalived:

yum install keepalived -y

主服务:

发送至从服务器
scp /etc/keepalived/keepalived.conf root$192.168.80.101:/etc/keepalived/keepalived.conf

修改从服务器keepalive 配置文件

vi /etc/keepalived/keepalived.conf
linux memcached集群_第4张图片

创建magent.sh脚本

vi /opt/magent.sh
linux memcached集群_第5张图片
#!/bin/bash
Keepalived=ip addr | grep 192.168.80.188 | grep -v grep | wc -l
if [ $Keepalived -gt 0 ]; then
magent -u root -n 51200 -l 192.168.80.188 -p 12000 -s 192.168.80.100:11211 -b 192.168.80.101:11211
else
pkill -9 magent
fi

赋予脚本执行权限

chmod +x /opt/magent

启动keepalived

systemctl start keepalived

测试:

当80.100服务器keepalived关掉时
飘逸IP 回到从服务器80.101
如图
linux memcached集群_第6张图片