原理:
Memcached主主复制是指在任意一台Memcached服务器修改数据都会被同步到另外一台,但是Memcached API客户端是无法判断连接到哪一台Memcached服务器的,所以需要设置VIP地址,提供给Memcached API客户端进行连接。可以使用Keepalived产生的VIP地址连接主Memcached服务器,并且提供高可用架构。
使用两台Memcached服务器,一台客户机来完成,实验环境表如下:
1.配置memcached主缓存节点和从缓存节点-----两台配置相同
[root@localhost ~]# tar zxvf libevent-2.1.8-stable.tar.gz -C /opt/ //解包//
[root@localhost ~]# tar zxvf memcached-1.5.6.tar.gz -C /opt/
[root@localhost ~]# mkdir /opt/magent
[root@localhost ~]# tar zxvf magent-0.5.tar.gz -C /opt/magent/
[root@localhost opt]#cd libevent-2.1.8-stable/
[root@localhost libevent-2.1.8-stable]# yum install gcc gcc-c++ make -y
[root@localhost libevent-2.1.8-stable]# ./configure --prefix=/usr
[root@localhost libevent-2.1.8-stable]# make && make install
[root@localhost libevent-2.1.8-stable]# cd ../memcached-1.5.6/
[root@localhost memcached-1.5.6]# ./configure --with-libevent=/usr
[root@localhost memcached-1.5.6]# ln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6 //软链接//
2.关闭防火墙并开启memcached服务
[root@localhost memcached-1.5.6]# systemctl stop firewalld.service
[root@localhost memcached-1.5.6]# setenforce 0
[root@localhost memcached-1.5.6]# memcached -d -m 32m -p 11211 -u root
[root@localhost memcached-1.5.6]# 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
[root@localhost memcached-1.5.6]# cd /opt/magent/
[root@localhost magent]# ls
ketama.c ketama.h magent.c Makefile
[root@localhost magent]# vim ketama.h
#ifndef SSIZE_MAX
#define SSIZE_MAX 32767
#endif
[root@localhost magent]# vim Makefile
LIBS = -levent -lm //第一行末尾加-lm (不是数字1
LIBS = -levent -lm
CFLAGS = -Wall -O2 -g
[root@localhost magent]# 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可执行程序
[root@localhost magent]# ls
ketama.c ketama.h ketama.o magent magent.c magent.o Makefile
[root@localhost magent]# cp magent /usr/bin/
5.把产生的magent文件直接复制到从服务器。
[root@localhost bin]# yum install openssh-clients -y
[root@localhost bin]# scp magent [email protected]:/usr/bin/
6.安装keepalived,修改默认配置文件。
[root@localhost bin]# yum install keepalived -y
[root@localhost bin]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script magent {
script "/opt/shell/magent.sh"
interval 2
}
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_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,配置文件进行修改。
[root@localhost bin]# 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 {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
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管理脚本
[root@localhost bin]# mkdir /opt/shell
[root@localhost bin]# 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 //指定从缓存服务器
[root@localhost shell]# chmod +x magent.sh // 增加执行权限//
9.在从服务器上操作
扫描二维码关注公众号,回复:
2385880 查看本文章
![](/qrcode.jpg)
[root@localhost bin]# mkdir /opt/shell
[root@localhost bin]# cd /opt/shell/
[root@localhost shell]# vim magent.sh
[root@localhost shell]# 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
[root@localhost shell]# chmod +x magent.sh
10.开始验证
1)启动主服务器
[root@localhost shell]# systemctl start keepalived.service
[root@localhost shell]# netstat -ntap | grep 12000 //确认magent运行//
tcp 0 0 192.168.126.188:12000 0.0.0.0:* LISTEN 12422/magent
2)启动从服务器
[root@localhost shell]# systemctl start keepalived.service
[root@localhost shell]# netstat -ntap | grep 12000
tcp 0 0 192.168.126.188:12000 0.0.0.0:* LISTEN 11716/magent
3)在主服务器上使用telnet进行简单验证复制功能
[root@localhost shell]# 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
在从服务器上查看
[root@localhost shell]# 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.在客户端用漂移地址登陆服务
[root@localhost ~]# yum install telnet -y
[root@localhost ~]# 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)把主服务器停了业务不影响
[root@localhost shell]# systemctl stop keepalived.service
[root@localhost shell]# ip addr
inet 192.168.126.138/24 brd 192.168.126.255 scope global dynamic ens33
3)在从服务器上查看
[root@localhost shell]# 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)再把主服务器开启
[root@localhost shell]# systemctl start keepalived.service
[root@localhost shell]# 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
漂移地址再次转移到主服务器上,接手地址,服务依然不受影响。
实验成功