1.配置前准备
一台yum源服务器
两个虚拟机server4,server5
实验系统是redhat6.5
server4:172.25.254.4
server5:172.25.254.5
2.软件安装
1.配置yum源
server4和server5都要这样配置
[Server]
name=Server
baseurl=http://172.25.254.65/rhel6.5/Server
gpgcheck=0
[ScalableFileSystem]
name=ScalableFileSystem
baseurl=http://172.25.254.65/rhel6.5/ScalableFileSystem
gpgcheck=0
[HighAvailability]
name=HighAvailability
baseurl=http://172.25.254.65/rhel6.5/HighAvailability
gpgcheck=0
[LoadBalancer]
name=LoadBalancer
baseurl=http://172.25.254.65/rhel6.5/LoadBalancer
gpgcheck=0
[ResilientStorage]
name=ResilientStorage
baseurl=http://172.25.254.65/rhel6.5/ResilientStorage
gpgcheck=0
2.安装keepalived和apache,并且修改httpd的主页
yum install keepalived httpd -y
echo `hostname`>/var/www/html/index.html
3.修改keepalived的配置文件
server4: cat /etc/keepalived/keepalived.conf
global_defs {
notification_email { #接受邮件提醒
root@localhost
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1 #邮件服务器
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER #主服务
interface eth0 #网卡端口
virtual_router_id 51
priority 100 #优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.88/24 dev eth0 label eth0:1 #VIP
}
}
server5: cat /etc/keepalived/keepalived.conf
global_defs {
notification_email {
root@172.25.254.4
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP #备用服务器
interface eth0 #网卡端口
virtual_router_id 51 #虚拟路由值
priority 80 #优先级
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.254.88/24 dev eth0 label eth0:1 #VIP
}
}
!并没有启动服务!
3.启动服务配置
至此,keepalived的配置已经完成,当主服务器(server4)的keepalived被关掉时,VIP会转至另一个结点上(server5),当主服务器被修好后,VIP有会回到主服务器上。
但是并没有实现检测httpd服务的功能,所以编写一个脚本来实现我们想要的效果。
server4: vim /etc/keepalived/bind_httpd.sh
#!/bin/bash
while true
do
curl localhost >/dev/null #测试本机的httpd
if [ "$?" != "0" ]; then #如果未正常运行
/etc/init.d/httpd restart>>/var/log/httpd.log #尝试重启
if [ "$?" != "0" ]; then #若重启失败,则关掉keepalived
/etc/init.d/keepalived stop>/dev/null
echo 'Error:httpd stop!'>>/var/log/httpd.log
else ##重启成功,则刷新keepalived
/etc/init.d/keepalived restart >/dev/null
echo 'Warning:httpd restart success!'>>/var/log/httpd.log
fi
else #如果httpd没有问题,则检测keepalived
/etc/init.d/keepalived status>>/dev/null
if [ "$?" != "0" ]; then #如果keepalived被关闭,尝试重启
/etc/init.d/keepalived restart>>/dev/null
if [ "$?" != "0" ]; then #重启失败,日至报警
echo 'Error:keepalived stop'>>/var/log/httpd.log
else #重启成功,继续服务
echo 'Warning:keepalived restart success!'>>/var/log/httpd.log
fi
fi
fi
sleep 5 #每5s检测一次
done
给写好的脚本添加权限并执行
chmod +x /etc/keepalived/bind_httpd.sh
echo '/etc/keepalived/bind_httpd.sh' >>/etc/rc.d/rc.local #开机便运行
reboot #重启便执行(启动服务)
在这部分server5的操作和萨尔ver4相同。
4.测试结果
curl 172.25.254.88
>>server4
无论关调keepalived或是httpd,服务都会自起并恢复,想要看到服务器切换的效果,就把server4上的httpd卸载掉:
yum remove httpd -y
然后就发现server4上的keepalived也关掉了,VIP已经转移到server5上