实验报告
第一、实验环境:
第二、实验步骤:
配置memcached主缓存节点和从缓存节点-----两台配置相同
解包:
tar xf memcached-1.5.6.tar.gz -C /opt/
tar xf libevent-2.1.8-stable.tar.gz -C /opt/
cd /opt/libevent-2.1.8-stable --- 安装lib插件
./configure --prefix=/usr/
make && make install ---编译安装
cd ../memcached-1.5.6 ---安装memcached
./configure \
--with-libevent=/usr
make && make install ---编译安装
下面在主上安装magent:
mkdir /opt/magent
tar xf magent-0.5.tar.gz -C /opt/magent
cd /opt/magent/
vi ketama.h
vi Makefile
LIBS = -levent -lm //第一行末尾加-lm (不是数字1)
ls一下可看到magent可执行程序
cp magent /usr/bin/ //将编译好的magent程序复制到PATH路径中
scp magent [email protected]:/usr/bin/ //把产生的magent文件直接复制到从服务器。
下面安装keepalived:
yum install keepalived –y
vi /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_HB
}
vrrp_script magent {
script "/opt/shell/magent.sh"
interval 2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
magent
}
virtual_ipaddress {
192.168.80.188
}
}
从服务器上安装keepalived,拷贝上面的配置文件进行修改:
yum install -y keepalived
cd /etc/keepalived/
mv keepalived.conf keepalived.conf.bak
scp [email protected]:/etc/keepalived/keepalived.conf ./ //把主服务器的配置文件复制过来
vi keepalived.conf
从服务器的名称要修改;router_id MAGENT_HB
从服务器的热备状态要修改成BACKUP;state BACKUP
从调度器的优先级要小于主的;priority 90
下面设置magent管理脚本
--在主服务器上设置:
mkdir /opt/shell 创建目录
cd /opt/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.80.188 -p 12000 -s 192.168.80.181:11211 -b 192.168.80.182:11211
else
pkill -9 magent
fi
参数注解:
-n 51200 //定义用户最大连接数
-l 192.168.80.188 //指定虚拟IP
-p 12000 //指定端口号
-s //指定主缓存服务器
-b //指定从缓存服务器
chmod +x magent.sh 增加执行权限
--在从服务器上操作:
mkdir /opt/shell
cd /opt/shell
vi magent.sh
脚本内容如下,与主服务器脚本有区别!
#!/bin/bash
K=`ip addr | grep 192.168.80.188 | grep -v grep | wc -l`
if [ $K -gt 0 ]; then
magent -u root -n 51200 -l 192.168.80.188 -p 12000 -s 192.168.80.181:11211 -b 192.168.80.182:11211
else
pkill -9 magent
fi
chmod +x magent.sh 加执行权限
第三步:开始验证工作
---启动主服务器
service keepalived start
netstat -anpt | grep 12000 //确认magent运行
![](https://s1.51cto.com/images/blog/201803/25/20c59e92c452d26c559ba15bbc70ec27.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
cat /var/log/messages 看日志,验证主从
![](https://s1.51cto.com/images/blog/201803/25/d0554e0924fec37435290893d042078e.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
ip addr //确认漂移地址生效
![](https://s1.51cto.com/images/blog/201803/25/69dda3e28ae6ea8e098da85dbf87ac7e.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
验证了漂移地址在主服务器上
从服务器开启keepalived
service keepalived start
cat /var/log/messages 看日志,验证主从
![](https://s1.51cto.com/images/blog/201803/25/c71e3acc5d79a5b0744fcd4a9101864b.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
下面启动memcached
memcached -m 512k -u root -d -l 192.168.80.181 -p 11211 //启动主
memcached -m 512k -u root -d -l 192.168.80.182 -p 11211 //启动从
![](https://s1.51cto.com/images/blog/201803/25/d3006a19746f44b8d7c1dc2ac7bf706d.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
启动成功
下面在客户端测试:192.168.80.138
-----关闭干扰------------
service firewalld stop
setenforce 0
telnet 192.168.80.188 12000 用漂移地址登陆服务:
以上界面表示登陆成功
从181与182服务器本地登陆查看内容:
telnet 192.168.80.181 11211
telnet 192.168.80.182 11211
下面进行倒换测试:
service keepalived stop //把主服务器停了业务不影响
可以看到漂移地址已经转移到从服务器上了,说明从已接受工作!
再次通过漂移地址访问时,业务不受影响,内容依然可以查看!
证明倒换测试成功,主备工作正常
service keepalived start 将主服务器再次启动后
漂移地址再次转移到主服务器上,接手地址,服务依然不受影响。
至此,实验成功!