兩台相同配置的web
用途
IP
MASTER
192.168.1.100
BACKUP
192.1681.101
2 安裝 兩台接入服務器分別安裝NginX和keepalived: 准備依賴包: yum -y install gcc pcre-devel zlib-devel openssl-devel yum -y install popt-devel 下載 wget http://nginx.org/download/nginx-1.2.4.tar.gz wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz 安裝NginX 安裝keepalivetar zxvf keepalived-1.2.7.tar.gz cd keepalived-1.2.7 ./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/ mkdir /etc/keepalived cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/sbin/keepalived /usr/sbin/
加入啟動服務 echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local echo "/etc/init.d/keepalived start" >> /etc/rc.local 3 配置 3.1 配置NginX 兩台接入服務器的NginX的配置完全一樣,主要是配置/usr/local/nginx/conf/nginx.conf的http。其中多域名指向是通過虛擬主機(配置http下面的server)實現;同一域名的不同虛擬目錄通過每個server下面的不同location實現;到後端的服務器在http下面配置upstream,然後在server或location中通過proxypass引用。要實現前面規劃的接入方式,http的配置如下:
http { include mime.types; default_type application/octet-stream; sendfile on; upstream dev.hysec.com { server 50.1.1.21:80; } upstream opslinux.com { ip_hash; server 192.168.1.102:80 server 192.168.1.103:80 server 192.168.1.104:80 } server { listen 80; server_name opslinux.com; location / { proxy_pass http://opslinux.com; } }
驗證方法: 首先用IP訪問前表中各個應用服務器的url 再用域名和路徑訪問前表中各個應用系統的域名/虛擬路徑 3.2 配置keepalived 按照上面的安裝方法,keepalived的配置文件在/etc/keepalived/keepalived.conf。主、從服務器的配置相關聯但有所不同。如下:
Master: ! Configuration File for keepalived global_defs { router_id NGINX_DEVEL } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 101 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 } } Backup: ! Configuration File for keepalived global_defs { router_id NGINX_DEVEL } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 } }
驗證: 先後在主、從服務器上啟動keepalived: /etc/init.d/keepalived start 在主服務器上查看是否已經綁定了虛擬IP: ip addr 停止主服務器上的keepalived: /etc/init.d/keepalived stop 然後在從服務器上查看是否已經綁定了虛擬IP: 啟動主服務器上的keepalived,看看主服務器能否重新接管虛擬IP 3.3 讓keepalived監控NginX的狀態 經過前面的配置,如果主服務器的keepalived停止服務,從服務器會自動接管VIP對外服務;一旦主服務器的keepalived恢復,會重新接管VIP。 但這並不是我們需要的,我們需要的是當NginX停止服務的時候能夠自動切換。 keepalived支持配置監控腳本,我們可以通過腳本監控NginX的狀態,如果狀態不正常則進行一系列的操作,最終仍不能恢復NginX則殺掉keepalived,使得從服務器能夠接管服務。 如何監控NginX的狀態 最簡單的做法是監控NginX進程,更靠譜的做法是檢查NginX端口,最靠譜的做法是檢查多個url能否獲取到頁面。 如何嘗試恢復服務 如果發現NginX不正常,重啟之。等待3秒再次校驗,仍然失敗則不再嘗試。 根據上述策略很容易寫出監控腳本。這裡使用nmap檢查nginx端口來判斷nginx的狀態,記得要首先安裝nmap。監控腳本如下:
#!/bin/bash # check nginx server status NGINX=/usr/local/nginx/sbin/nginx PORT=8080 nmap localhost -p $PORT | grep "$PORT/tcp open" #echo $? if [ $? -ne 0 ];then $NGINX -s stop $NGINX sleep 3 nmap localhost -p $PORT | grep "$PORT/tcp open" [ $? -ne 0 ] && /etc/init.d/keepalived stop fi
不要忘了設置腳本的執行權限,否則不起作用。 假設上述腳本放在/opt/chk_nginx.sh,則keepalived.conf中增加如下配置:
主keepalived vrrp_script chk_http_port { script "/opt/chk_nginx.sh" interval 1 weight -2 } track_script { chk_http_port } 例子: ! Configuration File for keepalived global_defs { router_id NGINX_UPSTEAM } vrrp_script chk_http_port { script "/opt/chk_nginx.sh" interval 1 weight -2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.100 } track_script { chk_http_port } }
更進一步,為了避免啟動keepalived之前沒有啟動nginx , 可以在/etc/init.d/keepalived的start中首先啟動nginx:
start() { /usr/local/nginx/sbin/nginx sleep 3 echo -n $"Starting $prog: " daemon keepalived ${KEEPALIVED_OPTIONS} RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog }