nginx+keepalived完善

nginx+keepalived 雙機互備的文章,寫那篇文章的時候沒有想過如果apache或者nginx 掛了,而 keepalived 或者 機器沒有死,那麼主輔是不會切換的,今天就研究了一下該如何監控 nginx進程呢,看官方站看到了。vrrp_script 功能,但是用他的方法實在形不通,可能是我的方法不對,或者是個BUG。所以後來我自己寫了個小腳本來完成工作。<?xml:namespace prefix="o" ns="urn:schemas-microsoft-com:office:office">

?xml:namespace>

很明顯的看出 server 2 已經接管了,已經變爲 MASTER 

可以看出keepalived 的進程已經停到

這時候看server 2的日誌,看是否已經接管

Apr 20 18:41:23 varnish Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Apr 20 18:41:24 varnish Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Apr 20 18:41:24 varnish Keepalived_vrrp: Netlink: skipping nl_cmd msg...

這時候看server 1的日誌

Apr 20 18:41:26 nginx Keepalived_healthcheckers: Terminating Healthchecker child process on signal
Apr 20 18:41:26 nginx Keepalived_vrrp: Terminating VRRP child process on signal

看日誌可以看出,兩臺服務器的 MASTRE 和 BACUKUP 已經都正常了

現在我們在 server 1 把 nginx 服務器停到

Server 1 $> killall nginx

監控 server 2的日誌 

Apr2018:38:23 varnish Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'.
Apr 20 18:38:23 varnish Keepalived_healthcheckers: Configuration is using : 3405 Bytes
Apr 20 18:38:23 varnish Keepalived_vrrp: Using MII-BMSR NIC polling thread...
Apr 20 18:38:23 varnish Keepalived_vrrp: Registering Kernel netlink reflector
Apr 20 18:38:23 varnish Keepalived_vrrp: Registering Kernel netlink command channel
Apr 20 18:38:23 varnish Keepalived_vrrp: Registering gratutious ARP shared channel
Apr 20 18:38:23 varnish Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.
Apr 20 18:38:23 varnish Keepalived_vrrp: Configuration is using : 35486 Bytes
Apr 20 18:38:23 varnish Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Apr 20 18:38:25 varnish Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded

監控 server 1 的日誌

Apr 20 18:37:39 nginx Keepalived_vrrp: Registering Kernel netlink command channel
Apr 20 18:37:39 nginx Keepalived_vrrp: Registering gratutious ARP shared channel
Apr 20 18:37:39 nginx Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.
Apr 20 18:37:39 nginx Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'.
Apr 20 18:37:39 nginx Keepalived_healthcheckers: Configuration is using : 3401 Bytes
Apr 20 18:37:39 nginx Keepalived_vrrp: Configuration is using : 35476 Bytes
Apr 20 18:37:40 nginx Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Apr 20 18:37:41 nginx Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Apr 20 18:37:41 nginx Keepalived_vrrp: Netlink: skipping nl_cmd msg...
Apr 20 18:37:41 nginx Keepalived_vrrp: VRRP_Script(chk_http_port) succeeded

8、測試,分別在兩個服務器 啓動 nginx 和 keepalived

/usr/local/nginx/sbin/nginx
/etc/init.d/keepalived start

7.編寫監控nginx監控腳本

vim /opt/nginx_pid.sh
#!/bin/bash

# varsion 0.0.2

# 根據一網友說這樣做不科學,如果nginx服務起來了,但是我把keepalived 殺掉了,我的理由是,如果nginx死掉了,我覺得就很難在起來,再有就是nagios 當然要給你報警了啊。不過這位同學說的有道理,所以就稍加改了一下腳本
A=`ps -C nginx --no-header |wc -l`                ## 查看是否有 nginx進程 把值賦給變量A
if [ $A -eq 0 ];then                                         ## 如果沒有進程值得爲 零

                /usr/local/nginx/sbin/nginx

                sleep 3

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

                       killall keepalived                        ## 則結束 keepalived 進程

                fi
fi

6.在 server 2 服務器 keepalived 配置

vrrp_script chk_http_port {
                script "/opt/nginx_pid.sh"
                interval 2
                weight 2
}
vrrp_instance VI_1 {
        state BACKUP                                ### 設置爲 輔機
        interface eth0
        virtual_router_id 51                        ### 與 MASTRE 設置 值一樣
        priority 100                                     ### 比 MASTRE權重值 低
        authentication {
                     auth_type PASS
                     auth_pass eric                    ### 密碼 與 MASTRE 一樣
        }
        track_script {
                chk_http_port
        }
        virtual_ipaddress {
                 192.168.6.7
        }
}

5.server 1服務器編寫配置文件

vrrp_script chk_http_port {
                script "/opt/nginx_pid.sh"         ###監控腳本
                interval 2                             ###監控時間
                weight 2                                ###目前搞不清楚
}
vrrp_instance VI_1 {
        state MASTER                            ### 設置爲 主
        interface eth0                             ### 監控網卡    
        virtual_router_id 51                    ### 這個兩臺服務器必須一樣
        priority 101                                 ### 權重值 MASTRE 一定要高於 BAUCKUP
        authentication {
                     auth_type PASS             ### 加密
                     auth_pass eric                ### 加密的密碼,兩臺服務器一定要一樣,不然會出錯
        }
        track_script {
                chk_http_port                     ### 執行監控的服務
        }
        virtual_ipaddress {
             192.168.6.7                            ###    VIP 地址
        }
}

4.安裝 keepalived 

InBlock.gifapt-get install keepalived

3.分別在兩臺機器創建測試文件

echo "192.168.6.162" > /var/www/index.html
echo "192.168.6.188" > /var/www/index.html

2.分別在兩臺服務器編寫配置文件

vim /usr/local/nginx/conf/nginx.conf
user    www www;
worker_processes    1;
error_log    logs/error.log    notice;
pid                logs/nginx.pid;
events {
        worker_connections    1024;
}
http {
        include             mime.types;
        default_type    application/octet-stream;
        sendfile                on;
        tcp_nopush         on;
        keepalive_timeout    65;
        gzip    on;
        server {
                listen             80;
                server_name    localhost;
                index     index.html index.htm;
                root        /var/www;
                error_page     500 502 503 504    /50x.html;
                location = /50x.html {
                        root     html;
                }
        }
}

環境

Server 1  :  ubuntu-server 8.04.4          192.168.6.162

Server 2  :  userver-server 8.04.4          192.168.6.188

軟件

Keepalived 1.1.15

nginx-0.8.35

pcre-8.02

1.分別在兩臺服務器上安裝nginx

tar jxvf pcre-8.02.tar.bz2
cd pcre-8.02
./configure --prefix=/usr --enable-utf8 --enable-pcregrep-libbz2 --enable-pcregrep-libz
make
make install
tar zxvf nginx-0.8.35.tar.gz
cd nginx-0.8.35
--prefix=/usr/local/nginx --with-pcre --user=www --group=www --with-file-aio --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-cc-opt=' -O3'
make
make install

.LVS 基礎知識彙總
LVS的算法介紹               http://www.linuxtone.org/viewthread.php?tid=69
學習LVS的三種轉發模式       http://www.linuxtone.org/viewthread.php?tid=77
LVS中的IP負載均衡技術       http://www.linuxtone.org/viewthread.php?tid=68

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