keeplived How to configure Nginx dual-machine high availability

Source: Internet
Author: User
Tags mail sleep backup

First, Introduction

Whether keepalived or heartbeat is highly available, its high availability is all about the high availability of server scripting, not the service perspective.

In other words, if the server down machine or network failure, high availability can be achieved automatically switch. If you run a service like Nginx hang off

These highly available software are not aware of, and need to write their own scripts to implement service switching.

Second, install the configuration keepalived

#./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 configuration file (master:1.1.1.1)

# 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
    Advert_int 1
    authentication {
        Auth_type Pass
        auth_pass Mdnginx
    }
    virtual_ipaddress {
        1.1.1.100
    }
    track_script {
        chk_ Nginx
  }
}

Edit BACKUP's primary configuration file (backup:1.1.1.2)

# 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
    Advert_int 1
    authentication {
        Auth_type Pass
        auth_pass Mdnginx
    }
    virtual_ipaddress {
        1.1.1.100
    }
    track_script {
        chk_ Nginx
  }
}

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Servers/web/

Third, start the service and scripting (both host and standby to perform)

# service Nginx start
# service keepalived start

Writing a script to monitor Nginx

# 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 keep alived Stop
Fi

Improve the script, plus the alarm function.

# vim/opt/monnginx.sh
    
#!/bin/bash
# author:honway.liu #
date:2013-03-15
ipaddr=$ (IP addr Show Eth0|awk ' {print $} ' | Sed-n 3p)
contact= (137XXXXXXXX@139.com)
num=${#contact [@]}
function email () {for (
i=0;i< num;i++);d o
        echo "$IPADDR Service Problem" | mail-s "WARNING" ${contact[i]}---F gm100861@gmail.com
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

The alarm used here is 139 mailbox, in 139 of the mailbox set up with mail, notice to the mobile phone, and the way of long letter.

Need to account for the SendMail service of the machine.

This article comes from "Acridine a Pooh" blog, please be sure to keep this source http://gm100861.blog.51cto.com/1930562/1155549

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.