Magent搭建Memcached高可用群集
Magent缓存代理介绍
因为Memcached服务器与服务器之间没有任何通讯,所以当任何服务器节点出现故障时,会出现单点故障。通过Magent缓存代理,防止单点现象。通过客户端连接到缓存代理服务器,缓存代理服务器可以连接多台Memcached机器并同时将每台Memcached机器进行数据同步。如果其中一台缓存服务器down机,系统依然可以继续工作,如果其中一台Memcached机器down掉,数据不会丢失并且可以保证数据的完整性。
原理演示图:
实验环境介绍:
三台服务器:
master/slave/client
实验步骤
在master和slave上同时安装libevent、memcached
master&&slave:
systemctl stop firewalld.service
setenforce 0
tar zxvf memcached-1.5.6.tar.gz -C /opt
tar zxvf libevent-2.1.8-stable.tar.gz -C /opt
yum install gcc gcc-c++ -y
cd /opt/libevent-2.1.8-stable/
./configure --prefix=/usr/local
make && make install
cp /usr/local/lib/libevent-2.1.so.6 /usr/lib64/
cd /opt/memcached-1.5.6/
./configure --with-libevent=/usr/local
make && make install
在master上安装配置magent
master:
mkdir /opt/magent
tar zxvf magent-0.5.tar.gz -C /opt
vim /opt/magent/ketama.h
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif
vim /opt/magent/Makefile
LIBS = -levent -lm 此处为英文单词'l',不是1
make
将magent命令复制到slave服务器上
注:标记地方为需要更改的位置
yum install openssl-clients -y
cp magent /usr/bin
scp magent root@192.168.218.157:/usr/bin
yum install keepalived -y
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
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_script magent {
script "/opt/shell/magent.sh" 脚本位置
interval 2 检测脚本为2秒
}
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 { 调用vrrp_script magent
magent
}
virtual_ipaddress {
192.168.218.16
}
}
从服务器上安装配置keepalived
slave:
yum install openssl-clients -y
yum install keepalived -y
cd /etc/keepalived/
mv keepalived.conf keepalived.conf.bk
master:
cd /etc/keepalived/
scp keepalived.conf root@192.168.218.157:/etc/keepalived/keepalived.conf
slave:
vim /etc/keepalived/keepalived.conf
global_defs {
router_id MAGENT_HB
vrrp_instance VI_1 {
state BACKUP
virtual_router_id 52
priority 90
master:
mkdir /opt/shell
vim /opt/shell/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.218.16 -p 12000 -s 192.168.218.130:11211 -b 192.168.218.157:11211 else pkill -9 magent fichmod +x /opt/shell/magent.sh
systemctl start keepalived.servic
netstat -ntap | grep 12000 #此时检查应有12000端口号
slave:
mkdir /opt/shell
vim /opt/shell/magent.sh
#!/bin/bash K=`ip addr | grep 192.168.218.16 | grep -v grep | wc -l` if [ $K -gt 0 ];then magent -u root -n 51200 -l 192.168.218.16 -p 12000 -s 192.168.218.130:11211 -b 192.168.218.157:11211 else pkill -9 magent fi
#magent -u指定用户、-n指定连接数量、-l指定漂移地址、-p指定端口、-s指定主服务器地址、-b指定从服务器地址
chmod +x /opt/shell/magent.sh
systemctl start keepalived.servic
netstat -ntap | grep 12000 #此时检查应有12000端口号
master:
memcached -m 512k -u root -d -l 192.168.218.130 -p 11211
netstat -ntap | grep 11211
slave:
memcached -m 512k -u root -d -l 192.168.218.157 -p 11211
netstat -ntap | grep 11211
测试:
master:
yum install telnet -y
telnet 192.168.218.130 11211
slave:
yum install telnet -y
telnet 192.168.218.157 11211
client:
yum install telnet -y
telnet 192.168.218.16 12000
Trying 192.168.218.16...
Connected to 192.168.218.16.
Escape character is '^]'.
add username 0 0 7 #写入一段数据
1231231
STORED
slave:
telnet 192.168.218.157 11211
Trying 192.168.218.157...
Connected to 192.168.218.157.
Escape character is '^]'.
get username
VALUE username 0 7
1231231 #在主服务器和从服务器上可以看到同步过来的数据
END