实验环境: 实验目的:client端通过虚拟IP地址登陆memcached主和从缓存服务器插入数据,主缓存和从缓存是否有数据 步骤:主服务器 [root@localhost ~]# yum install -y gcc gcc-c++ make //搭建安装环境 [root@localhost ~]# tar xvf libevent-2.1.8-stable.tar.gz
[root@localhost ~]# tar xvf memcached-1.5.6.tar.gz [root@localhost ~]# mkdir /opt/magent [root@localhost ~]# tar xvf magent-0.5.tar.gz -C /opt/magent/ //解压软件包 [root@localhost ~]# cd libevent-2.1.8-stable [root@localhost libevent-2.1.8-stable]# ./configure --prefix=/usr/ [root@localhost libevent-2.1.8-stable]# make && make install [root@localhost memcached-1.5.6]# cd ../memcached-1.5.6 [root@localhost memcached-1.5.6]# ./configure --with-libevent=/usr [root@localhost memcached-1.5.6]# make && make install //编译安装 [root@localhost libevent-2.1.8-stable]# ln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6 [root@localhost ~]# cd /opt/magent/ [root@localhost magent]# vi ketama.h #ifndef SSIZE_MAX #define SSIZE_MAX 32767 [root@localhost magent]# vi Makefile LIBS = -levent -lm //第一行末尾加-lm (不是数字1) [root@localhost magent]# make
[root@localhost magent]# ls //会产生magent可执行程序 [root@localhost magent]# cp magent /usr/bin/ //将编译好的magent程序复制到PATH路径中 [root@localhost magent]# scp magent root@192.168.100.20:/usr/bin/ //把产生的magent文件直接复制到从服务器 [root@localhost magent]# yum install keepalived -y //下载健康检查工具 [root@localhost shell]# vi /etc/keepalived/keepalived.conf //配置keepalived文件 ! 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 } track_script { magent } virtual_ipaddress { 192.168.100.188 } } //定义漂移地址,×××字体要改或添加其余全删除 [root@localhost shell]# mkdir /opt/shell //创建magent脚本目录 [root@localhost shell]# cd /opt/shell/ [root@localhost shell]# vi 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.188 -p 12000 -s 192.168.100.10:11211 -b 192.168.100.20:11211 else pkill -9 magent Fi [root@localhost shell]# chmod +x magent.sh //给执行权限 [root@localhost shell]# systemctl start keepalived.service //启动健康检查工具 [root@localhost shell]# netstat -anpt | grep 12000 tcp 0 0 192.168.100.188:12000 0.0.0.0:* LISTEN 9582/magent //说明magent 启动工作了 [root@localhost shell]# cat /var/log/messages //查看日志 …… Transition to MASTER STATE …… [root@localhost shell]# ip addr //确定定义的漂移地址存在 ---从服务器配置-- [root@localhost shell]# yum install -y gcc gcc-c++ make //搭建安装环境 [root@localhost shell]# tar xvf libevent-2.1.8-stable.tar.gz [root@localhost shell]# tar xvf memcached-1.5.6.tar.gz [root@localhost ~]# cd libevent-2.1.8-stable [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]# make && make install //编译安装 [root@localhost memcached-1.5.6]# ln -s /usr/lib/libevent-2.1.so.6 /usr/lib64/libevent-2.1.so.6 [root@localhost memcached-1.5.6]# yum install keepalived -y //下载健康检查工具 [root@localhost memcached-1.5.6]# cd /etc/keepalived/ [root@localhost keepalived]# mv keepalived.conf keepalived.conf.bak [root@localhost keepalived]# scp root@192.168.100.10:/etc/keepalived/keepalived.conf ./ 把主服务器的配置文件复制过来 [root@localhost keepalived]# vi 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_HB }

vrrp_instance VI_1 { state BACKUO interface ens33 virtual_router_id 52 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { magent } virtual_ipaddress { 192.168.100.188 } } //×××字体需要修改

[root@localhost keepalived]# mkdir /opt/shell [root@localhost keepalived]# cd /opt/shell/ [root@localhost shell]# vi magent.sh #!/bin/bash K=ip addr | grep 192.168.100.188 | grep -v grep | wc -l if [ $K -gt 0 ]; then magent -u root -n 51200 -l 192.168.100.188 -p 12000 -s 192.168.100.10:11211 -b 192.168.100.20:11211 else pkill -9 magent Fi [root@localhost shell]# chmod +x magent.sh [root@localhost shell]# systemctl start keepalived.service [root@localhost shell]# cat /var/log/messages //验证主从 …… Entering BACKUP STATE …… [root@localhost shell]# memcached -m 512k -u root -d -l 192.168.100.10 -p 11211 //启动主 [root@localhost shell]# memcached -m 512k -u root -d -l 192.168.100.20 -p 11211 //启动从 ---在客户端测试--- [root@localhost ~]# yum install -y telnet [root@localhost ~]# telnet 192.168.100.188 12000 Trying 192.168.100.188... Connected to 192.168.100.188. Escape character is '^]'. add username 0 0 7 1234567 主服务器 [root@localhost shell]# telnet 192.168.100.10 11211 Trying 192.168.100.10... Connected to 192.168.100.10. Escape character is '^]'. get username VALUE username 0 7 1234567 从服务器 [root@localhost shell]# telnet 192.168.100.20 11211 Trying 192.168.100.20... Connected to 192.168.100.20. Escape character is '^]'. get username VALUE username 0 7 1234567 客户端再进 [root@localhost ~]# telnet 192.168.100.188 12000 Trying 192.168.100.188... Connected to 192.168.100.188. Escape character is '^]'. add ok 0 0 6 123456 STORED get ok VALUE ok 0 6 123456 //新建ok 从服务器跟主服务器都检证一下是否都有数据插入 主--- [root@localhost shell]# telnet 192.168.100.10 11211 Trying 192.168.100.10... Connected to 192.168.100.10. Escape character is '^]'. get ok VALUE ok 0 6 123456 从--- [root@localhost shell]# telnet 192.168.100.20 11211 Trying 192.168.100.20... Connected to 192.168.100.20. Escape character is '^]'. get username VALUE username 0 7 1234567 END get ok VALUE ok 0 6 123456

//把主停了业务不影响