1. 程式人生 > 其它 >nginx高可用方案

nginx高可用方案

3.3.6 Nginx 高可用

3.3.6.1 安裝Keepalived

yum install -y keepalived

3.3.6.2 編輯keepalived配置檔案

vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id LVS_DEVEL					#主備一樣
}
vrrp_script chk_nginx {					#檢測nginx指令碼
    script "/home/eqics/script/chk_nginx.sh"
    interval 2
    weight -20
}

vrrp_instance VI_1 {
    state BACKUP						#主MASTER  備BACKUP
    interface eth0						#網絡卡
    nopreempt							#備機BACKUP才用,不搶佔的意思
    unicast_src_ip 192.168.0.96			#本機內網ip
    unicast_peer {
		192.168.0.102					#對機內網ip
	}
    virtual_router_id 51				#主備一樣
    priority 101						#主比備高
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111					#密碼可以修改主備相同
    }
    track_script {						#指令碼呼叫
	chk_nginx
    }

    virtual_ipaddress {
        192.168.0.100					#虛擬ip,自己設定,要跟內網一個網段
    }
}

3.3.6.3 keepalived命令

systemctl start keepalived.service

雲伺服器上操作:

虛擬ip繫結兩臺機器,再用公網ip繫結虛擬ip。

可以實現一臺機器掛了,ip會跳到另外的機器上繼續執行。

3.3.6.4 chk_nginx 指令碼

在/home/eqics/script/下建立chk_nginx.sh

#!/bin/bash

n=`netstat -tlnp | grep 0.0.0.0:80`

if [ $? -eq "1" ]
then
        /home/eqics/nginx-https/sbin/nginx
	sleep 2 
        n2=`netstat -tlnp | grep 0.0.0.0:80`
        if [ $? -eq "1" ]
        then
                killall keepalived
        fi
fi

3.3.6.5 指令碼賦權

chmod 755 chk_nginx

3.3.6.6 重啟Keepalived

systemctl restart keepalived.service
辛辛苦苦,忙忙碌碌。加油