First, Introduction
Whether it is keepalived or heartbeat do high availability, its high availability, are standing in the server script to say the high availability, rather than the angle of the service.
In other words, if the server down or network failure, high availability can be achieved automatically switch. If the service is running, such as nginx hanging off
These high-availability software is not aware of, need to write their own scripts to implement the service switch.
Second, installation configuration keepalived
copy content to clipboardCode:
# ./configure
# make
# make install
# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
# cp /usr/local/sbin/keepalived /usr/bin/
# chkconfig --add keepalived
# mkdir /etc/keepalived/
# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
Edit Master's main configuration file (master:1.1.1.1)
copy content to clipboardCode:
# vim/etc/keepalived/keepalived.conf
! Configuration File for keepalived
Global_defs {
router_id lvs_devel
}
Vrrp_script Chk_ Nginx {
script "/opt/monnginx.sh"
Interval 2
weight 2
}
vrrp_instance vi_1 {
state MASTER
interface eth0
virtual_router_id
Priority
& nbsp Advert_int 1
Authentication {
auth_type PASS
auth_pass Mdnginx
}
virtual_ipaddress {
1.1.1.100
}
Track_script {
chk_nginx
}
}
Edit BACKUP's Master profile (backup:1.1.1.2)
copy content to clipboardCode:
# vim/etc/keepalived/keepalived.conf
! Configuration File for keepalived
Global_defs {
router_id lvs_devel
}
Vrrp_script Chk_ Nginx {
script "/opt/monnginx.sh"
Interval 2
weight 2
}
vrrp_instance vi_1 {
state BACKUP
interface eth0
virtual_router_id
Priority
& nbsp Advert_int 1
Authentication {
auth_type PASS
auth_pass Mdnginx
}
virtual_ipaddress {
1.1.1.100
}
Track_script {
chk_nginx
}
}
Iii. Start-Up service and scripting (both host and Standby are performed)
copy content to clipboardCode:
# service nginx start
# service keepalived start
Writing a script to monitor Nginx
copy content to clipboardCode:
# vim /opt/monnginx.sh
#!/bin/bash
# author: honway.liu
# date: 2013-03-15
if [ $(ps -C nginx --no-header | wc -l) -eq 0 ]; then
service nginx start
fi
sleep 3
if [ $(ps -C nginx --no-header | wc -l) -eq 0 ]; then
service keepalived stop
fi
Improved scripting, plus alarm function.
copy content to clipboardCode:
# vim /opt/monnginx.sh
#!/bin/bash
# author: honway.liu
# date: 2013-03-15
IPADDR=$(ip addr show eth0|awk ‘{print $2}‘ | sed -n 3p)
contact=([email protected])
num=${#contact[@]}
function email() {
for ((i=0;i<num;i++));do
echo "$IPADDR service problem" | mail -s "WARNING" ${contact[i]} -- -f [email protected]
done
}
if [ $(ps -C nginx --no-header | wc -l) -eq 0 ]; then
service nginx start
fi
email
sleep 3
if [ $(ps -C nginx --no-header | wc -l) -eq 0 ]; then
service keepalived stop
email
fi
Here the alarm use is 139 mailbox, in 139 mailbox inside set up to have mail, notice to mobile phone, and long letter way.
Need to account for the SendMail service of this machine.
keeplived Configuration Nginx Dual Machine High Availability