Magent + Keepalived实现Memcached高可用群集
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Magent + Keepalived实现Memcached高可用群集相关的知识,希望对你有一定的参考价值。
Memcached的特点:
- 在Memcached中可以保存的item数据量是没有限制的,只要内存足够;
- Memcached单进程最大使用内存为2GB,要使用更多内存,可以分别在不同端口启动多个Memcached进程;
- Memcached是一种无阻塞的socket通信方式的服务,基于libevent库,由于无阻塞通信,对内存读写速度非常之快;
- Memcached分为服务器和客户端,可以配置多个服务器和客户端,应用于分布式的服务非常广泛;
- Memcached作为小规模的数据分布式平台是非常高效的;
Memcached存在的问题:
本身没有内置分布式功能,无法实现使用多台Memcachd服务器来存储不同的数据,最大程度的使用相同的资源;无法同步数据,容易造成单点故障,这里可以通过Memcached代理实现集群功能;
集群
由于Mamcached服务器与服务器之间没有任何通信,并且不进行任何数据复制备份,所以当任何服务器节点出现故障时,会出现单点故障,如果需要实现高可用,就需要通过其他方式。这里通过Magent缓存代理,防止单点现象,缓存代理也可以做备份功能,通过客户端连接到缓存代理了服务器,缓存代理服务器连接缓存连接复苏期,缓存代理服务器可以连接多台Memcached机器可以将每台Memcached机器进行数据同步,如果其中有一台缓存服务器down了,系统依然可以继续工作,如果其中一台Memcached机器down了,数据不会丢失并且可以保证数据的完整性。
架构图
系统环境
主机名 | 操作系统 | IP地址 | 软件包 |
---|---|---|---|
Memcached1 | Centos 7.3 x86_64 | 192.168.96.16 | libevent-2.1.8-stable.tar.gz、memcached-1.5.9.tar.gz、magent-0.5.tar.gz、keepalived |
Memcached2 | Centos 7.3 x86_64 | 192.168.96.17 | libevent-2.1.8-stable.tar.gz、memcached-1.5.9.tar.gz、keepalived |
客户端 | Centos 7.3 x86_64 | 192.168.96.22 | telnet |
相关软件包:百度云盘 密码:b7za
开始部署
配置memcached主缓存节点和从缓存节点-----两台配置相同
1.关闭防火墙机及Selinux[重要]
setenforce 0
systemctl stop firewalld.service
2.安装环境包
yum -y install gcc gcc-c++ make
3.编译安装libevent(必须先安装)
#解压
tar zxvf libevent-2.1.8-stable.tar.gz -C /opt
#切换到libevent目录
cd /opt/libevent-2.1.8-stable/
#配置
./configure --prefix=/usr/
#编译及安装
make && make install
3.编译安装memcached
#解压
tar zxvf memcached-1.5.9.tar.gz -C /opt/
#切换到memcached
cd /opt/memcached-1.5.9/
#配置(指定libevent路径)
./configure --with-libevent=/usr/
#编译及安装
make && make install
4.优化启动(支持复制功能的Mencached需要安装后的libevent-2.1.so.6模块,不然启动服务会报错)
ln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6
5.分别在两台服务器上启动memcached
#主服务器
memcached -m 512k -u root -d -l 192.168.96.16 -p 11211
#从服务器
memcached -m 512k -u root -d -l 192.168.96.17 -p 11211
6.检查memcached是否启动
netstat -ntap | grep 11211
magent在主服务器安装,从服务器不用安装
编译安装magent
1.创建magent目录
mkdir /opt/magent
2.解压
tar zxvf magent-0.5.tar.gz -C /opt/magent
3.切换到目录
cd /opt/magent
4.让动态链接库为系统所共享
/sbin/ldconfig
5.编辑ketama.h,添加文件头部信息
vim ketama.h
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif
6.修改Makefile
sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile
7.编译(完成后会生成一个manage的可执行文件)
make
8.复制mgent命令到系统管理中,便于使用
cp /opt/magent/magent /usr/bin/
9.发送至从服务器
scp /opt/magent/magent [email protected]:/usr/bin
安装keepalived
yum -y install keepalived
1.编辑keepalived配置文件
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.200.1
smtp_connect_timeout 30
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.96.100
}
}
2.建立shell目录
mkdir /root/shell
3.创建magent.sh脚本
vim /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.96.100 -p 12000 -s 192.168.96.16:11211 -b 192.168.96.17: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.service
systemctl start keepalived.service
6.检查keepalived服务状态
systemctl status keepalived.service
7.检查vip地址
ip addr
在从服务器上操作
1.安装keepalived
yum -y install keepalived
2.将主服务器上keepalived配置文件复制过来
scp [email protected]:/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
3.编辑keepalived配置文件
vim keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
fail[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id magent_H2 #指定router_id
}
vrrp_script magent { #定义函数
script "/root/shell/magent.sh"
interval 2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33 #本地网卡名称
virtual_router_id 51 #id主从一致
priority 90 #主优先级大于从优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #vip地址
192.168.96.100
}
track_script { #调用函数
magent
}
}
4.新建shell目录
mkdir /root/shell
5.创建magent.sh脚本
vim /root/shell/magent.sh
#!/bin/bash
Keepalived=`ip addr | grep 192.168.96.100 | grep -v grep | wc -l`
if [ $Keepalived -gt 0 ]; then
magent -u root -n 51200 -l 192.168.96.100 -p 12000 -s 192.168.96.12:11211 -b 192.168.96.17:11211
else
pkill -9 magent
fi
6.赋予脚本执行权限
chmod +x /root/shell/magent.sh
7.启动keepalived服务
systemctl enable keepalived.service
systemctl start keepalived.service
8.检查keepalived服务状态
systemctl status keepalived.service
9.检查vip地址
ip addr
在客户端测试
1.安装telnet客户端
yum -y install telnet
2.连接memcached
telnet 192.168.175.188 12000
测试复制功能
//连接vip进行测试,插入user键值
[[email protected] ~]# telnet 192.168.96.100 12000
Trying 192.168.96.100...
Connected to 192.168.96.100.
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键值,可以获取,成功!
[[email protected] ~]# telnet 192.168.96.16 11211
Trying 192.168.96.16...
Connected to 192.168.96.16.
Escape character is ‘^]‘.
get user
VALUE user 0 5
test1
END
quit
Connection closed by foreign host.
//连接Memcached2,查询user键值,也可以获取,成功!
[[email protected] ~]# telnet 192.168.96.17 11211
Trying 192.168.96.17...
Connected to 192.168.96.17.
Escape character is ‘^]‘.
get user
VALUE user 0 5
test1
END
quit
Connection closed by foreign host.
测试单点故障
以上是关于Magent + Keepalived实现Memcached高可用群集的主要内容,如果未能解决你的问题,请参考以下文章
Memcached+keepalived+magent实现主从复制和高可用
memcached+magent+keepalived实现高可用群集
Magent + Keepalived实现Memcached高可用群集
Memcached + Magent + keepalived高可用集群