keeplived Configuration Nginx Dual Machine High Availability

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.