简介
Memcached本身是基于内存的缓存,它的设计本身没有冗余机制。如果一个Memcached节点失去了所有的数据,应用程序可以从数据源中再次获取到数据,可以使用开源软件Magent实现Memcached集群,通过Magen缓存代理,防止单点现象。缓存代理也可以做备份,可以用keepalived软件实现高可用
实验环境
主机名 | 操作系统 | IP地址 | 相关软件 |
---|---|---|---|
master | CentOS_7.4_x86_64 | 192.168.100.71 | libevent-2.1.8-stable.tar.gz;memcached-1.5.6.tar.gz;magent-0.5.tar.gz |
backuper | CentOS_7.4_x86_64 | 192.168.100.72 | libevent-2.1.8-stable.tar.gz;memcached-1.5.6.tar.gz; |
client | CentOS_7.4_x86_64 | 192.168.100.73 | telnet软件 |
搭建步骤
一、配置主机
1、安装libevent
[[email protected] ~]# tar xvfz libevent-2.1.8-stable.tar.gz
[[email protected] ~]# cd libevent-2.1.8-stable/
[[email protected] libevent-2.1.8-stable]# ./configure --prefix=/usr
[[email protected] libevent-2.1.8-stable]# make && make install
2、安装memcached
[[email protected] ~]# tar xvfz memcached-1.5.6.tar.gz
[[email protected] ~]# cd memcached-1.5.6/
[[email protected] memcached-1.5.6]# ./configure --with-libevent=/usr
[[email protected] memcached-1.5.6]# make && make install
3、优化路径
[[email protected] ~]# ln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6
4、安装magent
1).解压magent
[[email protected] ~]# mkdir magent
[[email protected] ~]# tar xvfz magent-0.5.tar.gz -C magent
2).编辑ketama文件
[[email protected] ~]# cd magent/
[[email protected] magent]# vim ketama.h #编辑hash算法配制文件
#第一行添加如下参数:
#ifndef SSIZE_MAX #第1行,编辑声明SSIZE_MAX,否则编译会报错
#define SSIZE_MAX 32767 #第2行,编辑参数
#endif #第3行,关闭
3).编辑Makefile文件
[[email protected] magent]# vim Makefile
LIBS = -levent -lm #第1行,添加"-lm"
4).编译安装magent
[[email protected] magent]# make
gcc -Wall -O2 -g -I/usr/local/libevent/include -c -o magent.o magent.c
gcc -Wall -O2 -g -I/usr/local/libevent/include -c -o ketama.o ketama.c
gcc -Wall -O2 -g -o magent magent.o ketama.o -levent -lm -L /usr/local/libevent/lib
[[email protected] magent]# ls
[[email protected] magent]# cp magent /usr/bin/ #将执行脚本放到bin目录下,可直接执行命令
5).上传magent执行脚本给从服务器
[[email protected] magent]# scp magent [email protected]:/usr/bin/
4、配置keepalived
1).安装keepalived
[[email protected] ~]# yum -y install keepalived #安装keepalived软件
2).修改主配置文件
[[email protected] ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak #修改之前先进行备份
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.100.1
smtp_connect_timeout 30
router_id MAGENT_HA1
}
vrrp_script magent {
script "/opt/shell/magent.sh"
interval 2 //每2秒执行一次
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 200
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script { #触发脚本
magent
}
virtual_ipaddress {
192.168.100.200
}
}
}
5、编辑脚本
[[email protected] ~]# mkdir -p /opt/shell
[[email protected] ~]# cd /opt/shell/
[[email protected] 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.100.200 -p 12000 -s 192.168.100.71:11211 -b 192.168.100.72:11211
else
pkill -9 magent
fi
解析:
-u:指定用户
-n:用户最大连接数
-l:指定虚拟IP
-p:指定虚拟IP端口号
-s:主缓存节点
-b:从缓存节点
总结:主要是为memcache做了一个集群,最终虚拟出的IP是192.168.100.200
[[email protected] shell]# chmod +x magent.sh #赋予执行权限
二、配置备机
1、安装libevent、memcached
与主机配置操作相同,略…… 注意备机不需要安装magent``
2、配置keepalived
[[email protected] ~]# yum -y install keepalived
[[email protected] ~]# cd /etc/keepalived/
[[email protected] keepalived]# cp keepalived.conf keepalived.conf.bak
[[email protected] keepalived]# scp 192.168.100.71:/etc/keepalived/keepalived.conf .
[[email protected] keepalived]# vim keepalived.conf
global_defs {
……
router_id MAGENT_HA2 #服务器名称,区别于主机服务器名称
}
vrrp_instance VI_1 {
state BACKUP #注意作为备机,参数为BACKUP
priority 150 #注意作为备机,活跃值要小于主机活跃值
……
3、对应的创建脚本
[[email protected] ~]# mkdir -p /opt/shell
[[email protected] ~]# cd /opt/shell/
[[email protected] shell]# vim magent.sh
#!/bin/bash
K=`ip addr | grep 192.168.100.200 | grep -v grep | wc -l`
if [ $K -gt 0 ]; then
magent -u root -n 51200 -l 192.168.100.200 -p 12000 -s 192.168.100.71:11211 -b 192.168.100.72:11211
else
pkill -9 magent
fi
[[email protected] shell]# chmod +x magent.sh
三、启动Keepalived服务
1、主机启动
[[email protected] ~]# systemctl start keepalived.service
[[email protected] ~]# netstat -anpt | grep ‘12000‘
[[email protected] ~]# ip addr show ens33
2、备机启动
[[email protected] ~]# systemctl start keepalived.service
[[email protected] ~]# ip addr show ens33
四、启动Memcached服务
1、主机启动Memcached
[[email protected] ~]# memcached -m 512k -u root -d -l 192.168.100.71 -p 11211
[[email protected] ~]# netstat -anpt | grep ‘11211‘
2、备机启动Memcached
[[email protected] ~]# memcached -m 512k -u root -d -l 192.168.100.72 -p 11211
[[email protected] ~]# netstat -anpt | grep ‘11211‘
五、测试Memcached群集
1、客户端写入数据
[[email protected] ~]# yum -y install telnet
[[email protected] ~]# telnet 192.168.100.200 12000 #连接的是虚拟IP
2、查看主、备服务器数据
1).查看主机
[[email protected] ~]# telnet 192.168.100.71 11211
2).查看备机
3、模拟主机故障
[[email protected] ~]# systemctl stop keepalived.service
4、查看备机
[[email protected] ~]# ip addr
5、客户端再次访问
[[email protected] ~]# telnet 192.168.100.200 12000
原文地址:http://blog.51cto.com/11905606/2286374