天天看点

keepalived+nginx双机热备

软件下载:

http://www.keepalived.org/software/keepalived-1.1.15.tar.gz

http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz

主从:

                      / 172.16.0.200(master)+nginx\

VIP:172.16.0.199<===>                                 <==>mysql集群

                      \ 172.16.0.201  (slave) +nginx/

主从安装软件:

这里可以省略安装ipvsadm

#ln -s /usr/src/kernels/2.6.18-164.el5-x86_64/ /usr/src/linux

#tar zxvf ipvsadm-1.24.tar.gz

#cd ipvsadm-1.24

#make && make install

#tar zxvf keepalived-1.1.15.tar.gz

#cd keepalived-1.1.15

#./configure && make && make install

#find / -name keepalived # 查看keepalived位置

#cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

#cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

#mkdir /etc/keepalived

#cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

#cp /usr/local/sbin/keepalived /usr/sbin/

#service keepalived start|stop #做成系统启动服务方便管理.

主keepalived配置文件:

! Configuration File for keepalived

global_defs {

   notification_email {

        [email protected]

   }

   notification_email_from [email protected]

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

vrrp_script chk_http_port {

        script "/etc/keepalived/nginx_pid.sh"

        interval 2

         weight 2

! VIP1

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    mcast_src_ip 172.16.0.200

    virtual_router_id 51

    lvs_sync_daemon_inteface eth0

    priority 100

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    track_script {

        chk_http_port

    virtual_ipaddress {

        172.16.0.199

从keepalived配置文件:

    state BACKUP

    mcast_src_ip 172.16.0.201

    priority 80

                chk_http_port

nginx_pid.sh脚本检查nginx检查:

#/bin/bash

Q=`ps -C nginx --no-header |wc -l`

if [ $Q -eq 0 ];then

        /usr/local/nginx/sbin/nginx

        sleep 3

        if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then

                killall keepalived

        fi

fi

继续阅读