在CentOS7上部署Memcached主主复制+Keepalived高可用架构
原理:Memcached主主复制是指在任意一台Memcached服务器修改数据都会被同步到另外一台,但是Memcached API客户端是无法判断连接到哪一台Memcached服务器的,所以需要设置VIP地址,提供给Memcached API客户端进行连接。可以使用Keepalived产生的VIP地址连接主Memcached服务器,并且提供高可用架构。
使用两台Memcached服务器,一台客户机来完成,实验环境表如下:
http://i2.运维网.com/images/blog/201807/26/fe4579da7bc2aa16243c0adba3df8343.jpg
1.配置memcached主缓存节点和从缓存节点-----两台配置相同
# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt/ //解包//
# tar zxvf memcached-1.5.6.tar.gz -C /opt/
# mkdir /opt/magent
# tar zxvf magent-0.5.tar.gz -C /opt/magent/
#cd libevent-2.1.8-stable/
# yum install gcc gcc-c++ make -y
# ./configure --prefix=/usr
# make && make install
# cd ../memcached-1.5.6/
# ./configure --with-libevent=/usr
# ln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6 //软链接//
2.关闭防火墙并开启memcached服务
# systemctl stop firewalld.service
# setenforce 0
# memcached -d -m 32m -p 11211 -u root
# netstat -ntap | grep 11211
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN 11224/memcached
tcp6 0 0 :::11211 :::* LISTEN 11224/memcached
3.在主服务器上安装magent
# cd /opt/magent/
# ls
ketama.cketama.hmagent.cMakefile
# vim ketama.h
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif
# vim Makefile
LIBS = -levent -lm //第一行末尾加-lm (不是数字1
LIBS = -levent -lm
CFLAGS = -Wall -O2 -g
# make
gcc -Wall -O2 -g-c -o magent.o magent.c
gcc -Wall -O2 -g-c -o ketama.o ketama.c
gcc -Wall -O2 -g -o magent magent.o ketama.o -levent -lm
4.把生成的mgent程序让系统识别
ls一下可看到magent可执行程序
# ls
ketama.cketama.hketama.omagentmagent.cmagent.oMakefile
# cp magent /usr/bin/
5.把产生的magent文件直接复制到从服务器。
# yum install openssh-clients -y
# scp magent root@192.168.126.166:/usr/bin/
6.安装keepalived,修改默认配置文件。
# yum install keepalived -y
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script magent {
script "/opt/shell/magent.sh"
interval 2
}
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_instance VI_1 {
state MASTER
interface ens33 //网卡名称//
virtual_router_id 51
priority 100 //优先级//
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.126.188 //虚拟IP//
}
track_script {
magent //函数//
}
}
7.从服务器上安装keepalived,配置文件进行修改。
# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vi keepalived.conf
vrrp_script magent {
script "/opt/shell/magent.sh"
interval 2
}
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_HB //从服务器的名称//
}
vrrp_instance VI_1 {
state BACKUP //从服务器的热备状态要修改成BACKUP//
interface ens33//网卡名称//
virtual_router_id 52 //不能与主服务器相同//
priority 90 //从调度器的优先级要小于主的//
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.126.188 //虚拟IP//
}
track_script { //函数//
magent
}
}
8.在主服务器上设置magent管理脚本
# 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.126.188 -p 12000 -s 192.168.126.138:11211 -b 192.168.126.166:11211
else
pkill -9 magent
fi
参数注解:
-n 51200 //定义用户最大连接数
-l 192.168.126.188 //指定虚拟IP
-p 12000//指定端口号
-s //指定主缓存服务器
-b //指定从缓存服务器
# chmod +x magent.sh // 增加执行权限//
9.在从服务器上操作
# mkdir /opt/shell
# cd /opt/shell/
# vim magent.sh
# vim magent.sh
脚本内容如下,与主服务器脚本有区别!
#!/bin/bash
K=`ip addr | grep 192.168.126.188 | grep -v grep | wc -l`
if [ $K -gt 0 ]; then
magent -u root -n 51200 -l 192.168.126.188 -p 12000 -s 192.168.126.138:11211 -b 192.168.126.166:11211
else
pkill -9 magent
fi
# chmod +x magent.sh
10.开始验证
1)启动主服务器
# systemctl start keepalived.service
# netstat -ntap | grep 12000//确认magent运行//
tcp 0 0 192.168.126.188:12000 0.0.0.0:* LISTEN 12422/magent
2)启动从服务器
# systemctl start keepalived.service
# netstat -ntap | grep 12000
tcp 0 0 192.168.126.188:12000 0.0.0.0:* LISTEN 11716/magent
3)在主服务器上使用telnet进行简单验证复制功能
# telnet 192.168.126.188 12000//用漂移地址登陆服务//
Trying 192.168.126.188...
Connected to 192.168.126.188.
Escape character is '^]'.
add username 0 0 7 //添加一条键值数据//
1234567
STORED
在从服务器上查看
# telnet 192.168.126.188 12000
Trying 192.168.126.188...
Connected to 192.168.126.188.
Escape character is '^]'.
get username //查看键值数据
VALUE username 0 7
1234567 //内容存在,写入成功//
END
11.在客户端用漂移地址登陆服务
# yum install telnet -y
# telnet 192.168.126.188 12000
Trying 192.168.126.188...
Connected to 192.168.126.188.
Escape character is '^]'.
add username 0 0 8 //添加一条键值数据//
12345678
STORED
1)在主服务器和从服务器上查看是否写入成功。
主服务器
get username
VALUE username 0 8
12345678
END
从服务器
get username
VALUE username 0 8
12345678
END
2)把主服务器停了业务不影响
# systemctl stop keepalived.service
# ip addr
inet 192.168.126.138/24 brd 192.168.126.255 scope global dynamic ens33
3)在从服务器上查看
# ip addr
inet 192.168.126.166/24 brd 192.168.126.255 scope global dynamic ens33
valid_lft 1146sec preferred_lft 1146sec
inet 192.168.126.188/32 scope global ens33
可以看到漂移地址已经转移到从服务器上了,说明从已接受工作。
3)再把主服务器开启
# systemctl start keepalived.service
# ip addr
inet 192.168.126.138/24 brd 192.168.126.255 scope global dynamic ens33
valid_lft 1145sec preferred_lft 1145sec
inet 192.168.126.188/32 scope global ens33
valid_lft forever preferred_lft forever
漂移地址再次转移到主服务器上,接手地址,服务依然不受影响。
实验成功
页:
[1]