Keepalived+ nginx的安装部署
主机:IP->10.252.3.160 nginx已安装OK(省略)
备机:IP->10.252.3.161 nginx已安装OK(省略)
VIP:10.252.3.162
第一步:主备一起安装keepalived
yum installkeepalived –y
第二步:配置/etc/keepalived/keepalived.conf
主机10.252.3.160配置如下:
!Configuration File for keepalived
global_defs{
notification_email {
acassen@firewall.loc #发生邮件目标地址
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from root@nginx2 #发生邮件源地址
smtp_server 127.0.0.1 #smtp服务器
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_scriptchk_http_port {
script "/data/script/nginx.sh" #监控服务脚步
interval 2 #检测时间间隔(执行脚步间隔)
weight 2
}
vrrp_instanceVI_1 {
state MASTER #主备的区别点,标识
interface eth1 #对应的网卡
virtual_router_id 60
priority 101 #优先级,谁做主(值越大,优先级越高)
advert_int 2
authentication { #验证方式,以及密码必须主备一样
auth_type PASS
auth_pass 1234
}
track_interface { #监控的网卡。如果只监控服务,这里可以不填
eth1
}
track_script { #以脚本为监控chk_http_port是前面填写的
chk_http_port
}
virtual_ipaddress { #切换方式。切换虚拟IP
10.252.3.165/24 dev eth1 label eth1:1
}
}
===================================================
备机配置: 10.252.3.161
!Configuration File for keepalived
global_defs{
notification_email {
acassen@firewall.loc #发生邮件目标地址
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from root@nginx2 #发生邮件源地址
smtp_server 127.0.0.1 #smtp服务器
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_scriptchk_http_port {
script "/data/script/nginx.sh" #监控服务脚步
interval 2 #检测时间间隔(执行脚步间隔)
weight 2
}
vrrp_instanceVI_1 {
state BACKUP #主备的区别点,标识
interface eth1 #对应的网卡
virtual_router_id 60
priority 100 #优先级,谁做主(值越大,优先级越高)
advert_int 2
authentication { #验证方式,以及密码必须主备一样
auth_type PASS
auth_pass 1234
}
track_interface { #监控的网卡。如果只监控服务,这里可以不填
eth1
}
track_script { #以脚本为监控chk_http_port是前面填写的
chk_http_port
}
virtual_ipaddress { #切换方式。切换虚拟IP
10.252.3.165/24 dev eth1 label eth1:1
}
}
第三步:编写/data/script/nginx.sh脚本内容
#!/bin/sh
nginxPidNum=`ps-C nginx --no-header |wc -l`
keepalivedPidNum=`ps-C keepalived --no-header |wc -l`
if [$nginxPidNum -eq 0 ];then
killall keepalived
elif [$keepalivedPidNum -eq 0 ];then
service keepalived start
fi
给脚本权限 chmod +x /data/script/nginx.sh
第四步:启动主备keepalived
Servicekeepalived start
Ifconfig查看eth1:1是否起来了。如果起来了,则可以测试。没有自行调试。
本文出自 “东神要一打五” 博客,谢绝转载!
原文地址:http://gdutccd.blog.51cto.com/9271664/1741240