
正文
【 Keepalived 】Nginx or Http 主-主模式
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
上一篇:【 Keepalived 】Nginx or Http 主-备模式
在此基础上进行修改并实现 Keepalived主-主模式
首先,需要理解的是:主-备模式是一个VIP在工作,主-主模式则需要两个VIP来工作。一旦其中一台主机出现问题,则两个VIP都会绑定到同一台主机上,待到故障解除,重新启动httpd服务,恢复正常。
ka1 配置文件:
! Configuration File for keepalivedglobal_defs {
notification_email {
root@localhost
}
notification_email_from root@localhost
smtp_server localhost
smtp_connect_timeout
router_id NodeA
}vrrp_script check_nginx {
script "/etc/keepalived/bash/check_nginx.sh"
interval
weight -
}vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id
priority
advert_int
authentication {
auth_type PASS
auth_pass
}
track_script {
check_nginx
}
virtual_ipaddress {
192.168.2.200/
}
}
vrrp_instance VI_2 {
state BUCKUP
interface eth0
virtual_router_id
priority
advert_int
authentication {
auth_type PASS
auth_pass
}
track_script {
check_nginx
}
virtual_ipaddress {
192.168.2.201/
}
}
ka2 配置文件:
! Configuration File for keepalivedglobal_defs {
notification_email {
root@localhost
}
notification_email_from root@localhost
smtp_server localhost
smtp_connect_timeout
router_id NodeB
}vrrp_script check_nginx {
script "/etc/keepalived/bash/check_nginx.sh"
interval
weight -
}vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id
priority
advert_int
authentication {
auth_type PASS
auth_pass
}
track_script {
check_nginx
}
virtual_ipaddress {
192.168.2.200/
}
}
vrrp_instance VI_2 {
state MASTER
interface eth1
virtual_router_id
priority
advert_int
authentication {
auth_type PASS
auth_pass
}
track_script {
check_nginx
}
virtual_ipaddress {
192.168.2.201/
}
}
测试:
正常情况下:
ka1:
[ka1 root@192.168.2.10 ~]#ip a
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c::::4f brd ff:ff:ff:ff:ff:ff
inet 192.168.2.10/ brd 192.168.2.255 scope global eth0
inet 192.168.2.200/ scope global secondary eth0
inet6 fe80::20c:29ff:fe39:924f/ scope link
valid_lft forever preferred_lft forever
ka2:
[ka2 root@192.168.2.11 ~]#ip a
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c::7b:9f:8c brd ff:ff:ff:ff:ff:ff
inet 192.168.2.11/ brd 192.168.2.255 scope global eth1
inet 192.168.2.201/ scope global secondary eth1
inet6 fe80::20c:29ff:fe7b:9f8c/ scope link
valid_lft forever preferred_lft forever
当ka1 httpd出现错误时:
ka1 VIP被转移[ka1 root@192.168.2.10 ~]#ip a
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c::::4f brd ff:ff:ff:ff:ff:ff
inet 192.168.2.10/ brd 192.168.2.255 scope global eth0
inet6 fe80::20c:29ff:fe39:924f/ scope link
valid_lft forever preferred_lft foreverka2 被绑定了两个VIP
[ka2 root@192.168.2.11 ~]#ip a
: lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue state UNKNOWN
link/loopback ::::: brd :::::
inet 127.0.0.1/ scope host lo
inet6 ::/ scope host
valid_lft forever preferred_lft forever
: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu qdisc pfifo_fast state UP qlen
link/ether :0c::7b:9f:8c brd ff:ff:ff:ff:ff:ff
inet 192.168.2.11/ brd 192.168.2.255 scope global eth1
inet 192.168.2.201/ scope global secondary eth1
inet 192.168.2.200/ scope global secondary eth1
inet6 fe80::20c:29ff:fe7b:9f8c/ scope link
valid_lft forever preferred_lft forever
再三强调:在keepalived使用中,脚本很重要很重要。要经过严格的测试调试,才能使用,我这里在贴一遍脚本,上个主-备中也有脚本,需要灵活修改的地方我红色标识出来:
#!/bin/bashpidfile=/var/lock/subsys/`basename $`.pid
if [ -f $pidfile ] && [ -e /proc/`cat $pidfile` ] ; then
exit
fi
trap "rm -rf $pidfile ; exit 0"
echo $$ > $pidfile
maxfails=
fails=
success=while [ ]
do /usr/bin/wget --timeout= --tries= http://192.168.2.11/ -q -O /dev/null && ping -c1 192.168.2.1 &> /dev/null
if [ $? -ne ] ; then
let fails=$[$fails+]
success=
else
fails=
let success=$[$success+]
fi if [ $fails -ge $maxfails ] ; then
fails=
success=
#check keepalived is running ? try to stop it
/etc/init.d/keepalived status | grep 正在运行
if [ $? -eq ] ; then
/usr/bin/logger -is "local service fails $maxfails times ... try to stop keepalived."
/etc/init.d/keepalived stop >& | /usr/bin/logger
fi fi if [ $success -gt $maxfails ] ; then
#check keepalived is stopped ? try to start it
/etc/init.d/keepalived status | grep 已停
if [ $? -eq ] ; then
/usr/bin/logger -is "service changes normal, try to start keepalived ."
/etc/init.d/keepalived start
fi
success=
fi
sleep done
注意:此脚本必须有访问默认页面支持。






