https://pan.baidu.com/s/1vdoBffFBfUCtrN3DXFPM6w //软件链接百度网盘
配置memcached主缓存节点和从缓存节点-----两台配置相同
1.关闭防火墙机及Selinux[重要]
systemctl stop firewalld
setenforce 0
2.安装环境包
yum install lrz* -y
yum -y install gcc gcc-c++ make
3.编译安装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
3.编译安装memcached
#切换到memcached
cd /opt/memcached-1.5.6/
#配置(指定libevent路径)
./configure \
--prefix=/usr/local/memcached \
--with-libevent=/usr/local/libevent/
#编译及安装
make && make install
4.优化启动(支持复制功能的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
5.分别在两台服务器上启动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
6.检查memcached是否启动
netstat -ntap | grep 11211
magent在主服务器安装,从服务器不用安装
编译安装magent
1.创建magent目录
mkdir /opt/magent
2.解压
tar xf magent-0.5.tar.gz -C /opt/magent
3.切换到目录
cd /opt/magent
4.让动态链接库为系统所共享
/sbin/ldconfig
5.编辑ketama.h,添加文件头部信息
vi ketama.h
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
6.修改Makefile
vi Makefile //libevent安装路径在/usr/local/libevent中使用以下内容:
LIBS = -levent -lm -L /usr/local/libevent/lib
INCLUDE=-I /usr/local/libevent/include
Magent + Keepalived实现Memcached高可用群集
7.编译(完成后会生成一个manage的可执行文件)
make
8.复制mgent命令到系统管理中,便于使用
cp /opt/magent/magent /usr/bin/
9.发送至从服务器
scp /opt/magent/magent root@192.168.80.101:/usr/bin
安装keepalived
yum -y install keepalived
1.编辑keepalived配置文件
vi /etc/keepalived/keepalived.conf
! 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 ens32 #本地网卡名称
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
}
}
2.建立shell目录
mkdir /root/shell
3.创建magent.sh脚本
vi /root/shell/magent.sh
#!/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
参数说明:
-n 51200 #定义用户最大连接数
-l 192.168.175.188 #指定虚拟IP
-p 12000 #指定端口号
-s #指定主缓存服务器
-b #指定从缓存服务器
4.赋予脚本执行权限
chmod +x /root/shell/magent.sh
5.启动keepalived服务
systemctl enable keepalived
systemctl start keepalived
6.检查keepalived服务状态
systemctl status keepalived
7.检查vip地址
ip addr show ens32
在从服务器上操作
1.安装keepalived
yum -y install keepalived
2.将主服务器上keepalived配置文件复制过来
scp root@192.168.80.100:/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
3.编辑keepalived配置文件
vi keepalived.conf
! Configuration File for keepalived
global_defs {
router_id magent_H2 #指定router_id
}
vrrp_script magent { #定义函数
script "/root/shell/magent.sh"
interval 2
}
vrrp_instance VI_1 {
state BACKUP
interface ens32 #本地网卡名称
virtual_router_id 51 #id主从一致
priority 90 #主优先级大于从优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #vip地址
192.168.80.188
}
track_script { #调用函数
magent
}
}
4.新建shell目录
mkdir /root/shell
5.创建magent.sh脚本
vi /root/shell/magent.sh
#!/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
6.赋予脚本执行权限
chmod +x /root/shell/magent.sh
7.启动keepalived服务
systemctl enable keepalived
systemctl start keepalived
8.检查keepalived服务状态
systemctl status keepalived
9.检查vip地址
ip addr show ens32
在客户端测试
1.安装telnet客户端
yum -y install telnet
2.连接memcached
测试复制功能
//连接vip进行测试,插入user键值
[root@centos7-1 ~]# telnet 192.168.80.188 12000
Trying 192.168.80.188...
Connected to 192.168.80.188.
Escape character is '^]'.
set user 0 0 5
test1
STORED
get user
VALUE user 0 5
test1
END
quit
Connection closed by foreign host.
//连接Memcached1,查询user键值,可以获取,成功!
[root@centos7-1 ~]# telnet 192.168.80.100 11211
Trying 192.168.80.100...
Connected to 192.168.80.100.
Escape character is '^]'.
get user
VALUE user 0 5
test1
END
quit
Connection closed by foreign host.
//连接Memcached2,查询user键值,也可以获取,成功!
[root@centos7-1 ~]# telnet 192.168.80.101 11211
Trying 192.168.80.101...
Connected to 192.168.80.101.
Escape character is '^]'.
get user
VALUE user 0 5
test1
END
quit
Connection closed by foreign host.
测试单点故障
停止主服务器,去连接漂移地址,查看有木有出现内容
转载于:https://blog.51cto.com/14158288/2352531