nginx+keepalived(帶nginx監控腳本)

      Keepalived+ nginx的安裝部署

 

主機:IP->10.252.3.160  nginx已安裝OK(省略)

備機:IP->10.252.3.161   nginx已安裝OK(省略)

VIP10.252.3.162

 

第一步:主備一起安裝keepalived    

yum installkeepalived –y

第二步:配置/etc/keepalived/keepalived.conf

主機10.252.3.160配置如下:

!Configuration File for keepalived

global_defs{

   notification_email {

       [email protected] #發生郵件目標地址

       [email protected]

       [email protected]

   }

   notification_email_from root@nginx2      #發生郵件源地址

   smtp_server 127.0.0.1               #smtp服務器

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

 

vrrp_scriptchk_http_port {

    script "/data/script/nginx.sh"                   #監控服務腳步

    interval 2                    #檢測時間間隔(執行腳步間隔)

    weight 2

}

 

vrrp_instanceVI_1 {

    state MASTER   #主備的區別點,標識

    interface eth1           #對應的網卡

    virtual_router_id 60  

    priority 101     #優先級,誰做主(值越大,優先級越高)

    advert_int 2

    authentication {    #驗證方式,以及密碼必須主備一樣

        auth_type PASS

        auth_pass 1234

    }

 

    track_interface {               #監控的網卡。如果只監控服務,這裏可以不填

      eth1

    } 

    track_script {                      #以腳本爲監控chk_http_port是前面填寫的

        chk_http_port

    }

    virtual_ipaddress {   #切換方式。切換虛擬IP

        10.252.3.165/24 dev eth1 label eth1:1

    }

}

===================================================

備機配置: 10.252.3.161

!Configuration File for keepalived

global_defs{

   notification_email {

       [email protected] #發生郵件目標地址

       [email protected]

       [email protected]

   }

   notification_email_from root@nginx2      #發生郵件源地址

   smtp_server 127.0.0.1               #smtp服務器

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

 

vrrp_scriptchk_http_port {

    script "/data/script/nginx.sh"                   #監控服務腳步

    interval 2                    #檢測時間間隔(執行腳步間隔)

    weight 2

}

 

vrrp_instanceVI_1 {

    state BACKUP   #主備的區別點,標識

    interface eth1           #對應的網卡

    virtual_router_id 60  

    priority 100     #優先級,誰做主(值越大,優先級越高)

    advert_int 2

    authentication {    #驗證方式,以及密碼必須主備一樣

        auth_type PASS

        auth_pass 1234

    }

 

    track_interface {               #監控的網卡。如果只監控服務,這裏可以不填

      eth1

    } 

    track_script {                      #以腳本爲監控chk_http_port是前面填寫的

        chk_http_port

    }

    virtual_ipaddress {   #切換方式。切換虛擬IP

        10.252.3.165/24 dev eth1 label eth1:1

    }

}

第三步:編寫/data/script/nginx.sh腳本內容

#!/bin/sh

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

keepalivedPidNum=`ps-C keepalived --no-header |wc -l`

if [$nginxPidNum -eq 0 ];then

    killall keepalived

elif [$keepalivedPidNum -eq 0 ];then

    service keepalived start

fi

 

給腳本權限 chmod +x /data/script/nginx.sh

第四步:啓動主備keepalived

Servicekeepalived start

Ifconfig查看eth1:1是否起來了。如果起來了,則可以測試。沒有自行調試。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章