概述html
目前關於負載均衡和高可用的架構方案能找到至關多且詳盡的資料,此篇是本身學習相關內容的一個總結,防止未來遺忘再次從新查找資料,也避免踩相同的坑。node
這次配置的負載均衡與高可用架構:Nginx + Keepalived(主備模式),Nginx 使用反向代理實現七層負載均衡。linux
衆所周知,Nginx 是一款自由的、開源的、高性能HTTP服務器和反向代理服務器,也是一個IMAP、POP三、SMTP代理服務器。nginx
也就是說Nginx自己就能夠託管網站(相似於Tomcat同樣),進行HTTP服務處理,也能夠做爲反向代理服務器使用。c++
Keepalived 是一個基於VRRP協議來實現的服務高可用方案,能夠利用其來避免IP單點故障,相似的工具還有heartbeat、corosync、pacemaker。web
可是它通常不會單獨出現,而是與其它負載均衡技術(如lvs、haproxy、nginx)一塊兒工做來達到集羣的高可用。正則表達式
相關原理對於理解整個架構的工做方式以及以後的troubleshooting都很是重要。shell
關於負載均衡,Nginx + Keepalived(主備模式)實現負載均衡高可用的架構方式,可參考另外一篇至關不錯的博客:vim
http://www.cnblogs.com/kevingrace/p/6138185.htmlsegmentfault
關於虛擬路由冗餘協議(VRRP),可參考:
https://www.cnblogs.com/yechuan/archive/2012/04/17/2453707.html
http://network.51cto.com/art/201309/412163.htm
http://zhaoyuqiang.blog.51cto.com/6328846/1166840/
一.環境說明
系統環境:
4臺 Red Hat Enterprise Linux Server release 7.0 (Maipo)
master節點:192.168.0.151/24
backup節點:192.168.0.152/24
虛擬IP(VIP):192.168.0.16
nginx web服務器:192.168.0.153/24
nginx web服務器:192.168.0.154/24
架構示意圖:
二.軟件版本
Nginx stable version:nginx-1.12.2
Keepalived version:keepalived-1.3.9
三.環境安裝部署
4個節點均進行如下操做:
1. 關閉firewalld防火牆,此處將使用iptables防火牆管理軟件。
[root@rhel7-vm1 ~]# systemctl stop firewalld # 中止firewalld服務,關閉firewalld防火牆。 [root@rhel7-vm1 ~]# systemctl disable firewalld # 取消firewalld開機自啓動
2. 關閉selinux
[root@rhel7-vm1 ~]# vim /etc/selinux/config # 配置selinux爲permissive模式 ... SELINUX=permissive # 第7行 ...
[root@rhel7-vm1 ~]# setenforce 0 # 使selinux即時生效並使開機依然爲permissive模式 [root@rhel7-vm1 ~]# getenforce # 查看當前selinux模式 Permissive
3. 節點間時間同步並添加入週期性任務中(此處使用的是阿里的ntp時間同步服務器)
[root@rhel7-vm1 ~]# crontab -u root -e */1 * * * * root /usr/sbin/ntpdate 120.25.108.11
4. 開啓自定義iptables防火牆規則(此處爲虛擬機上實驗,只配置了一張網卡ens3;實際生產環境還要更加複雜,能夠指定多個網卡接口)
[root@rhel7-vm1 ~]# vim iptables_cfg.sh #!/bin/bash # # Edited : 2017.11.12 08:05 by hualf. # Usage : Used to configure firewall by 'iptables'. # iptables -F iptables -P INPUT DROP iptables -P OUTPUT ACCEPT iptables -P FORWARD ACCEPT iptables -I INPUT -i ens3 -m state --state RELATED,ESTABLISHED -j ACCEPT iptables -A INPUT -i lo -j ACCEPT iptables -A INPUT -i ens3 -s 192.168.0.0/24 -p icmp -j ACCEPT # 添加icmp協議,可以使用ping命令 iptables -A INPUT -i ens3 -s 192.168.0.0/24 -p tcp --dport 22 -j ACCEPT # 添加ssh服務端口,可以遠程登錄 # web service # 添加web服務端口 iptables -A INPUT -i ens3 -p tcp --dport 80 -j ACCEPT # 80端口:master節點與backup節點用於監聽nginx負載均衡服務 iptables -A INPUT -i ens3 -p tcp --dport 8080 -j ACCEPT # 8080端口:2個nginx web服務器的監聽端口 # dns service and keepalived(vrrp) iptables -A INPUT -i ens3 -p udp --sport 53 -j ACCEPT # 注意:容許來源爲53端口(DNS服務)的數據包進入主機,避免yum安裝rpm軟件包時沒法解析yum源地址! iptables -A INPUT -i ens3 -p vrrp -j ACCEPT # 注意:添加vrrp協議,確保能使用keepalived主備間的正常通訊,不然會發生腦裂! nginx web服務器節點可取消該規則。 service iptables save # 保存防火牆規則使開機自啓動
在運行 iptables_cfg.sh 時,出現報錯以下:
The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status).
For other actions, please try to use systemctl.
解決方法:
若是以前沒有中止firewalld服務的話,將其中止服務並取消開機自啓動;安裝iptables-services軟件包及相關依賴;重啓iptables服務,並實現開機自啓動。
[root@rhel7-vm1 ~]# systemctl stop firewalld [root@rhel7-vm1 ~]# systemctl disable firewalld [root@rhel7-vm1 ~]# yum install -y iptables-services [root@rhel7-vm1 ~]# systemctl restart iptables
[root@rhel7-vm1 ~]# systemctl enable iptables
再次運行 iptables_cfg.sh 時,防火牆規則被從新定義。
**************************************************************************
master節點:
1. 安裝及配置nginx
1)安裝 nginx 相關的依賴包
[root@rhel7-vm1 ~]# yum install -y gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel acpid
依賴包相關說明:
gcc / gcc-c++:gcc編譯器,編譯nginx須要。
pcre / pcre-devel:Perl 語言兼容正則表達式(Perl Compatible Regular Expressions,用C語言編寫的正則表達式函數庫),nginx的rewrite模塊正則表達式使用。
openssl / openssl-devel:nginx的ssl模塊使用。
zlib / zlib-devel:nginx的gzip模塊使用。
acpid:電源管理軟件包
注意:以上軟件包在編譯安裝nginx時必須安裝,不然報錯。
2)下載nginx-1.12.2,解壓及源碼安裝
[root@rhel7-vm1 ~]# tar zxvf nginx-1.12.2.tar.gz -C /usr/local # 解壓nginx源碼包至/usr/local目錄中 [root@rhel7-vm1 ~]# cd /usr/local/nginx-1.12.2 [root@rhel7-vm1 nginx-1.12.2]# ./configure --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module # nginx安裝檢查配置 ... skipping ... Configuration summary # 列出nginx所需的系統庫及配置的相關信息 + using system PCRE library + using system OpenSSL library + using system zlib library
nginx path prefix: "/usr/local/nginx" nginx binary file: "/usr/local/nginx/sbin/nginx" nginx modules path: "/usr/local/nginx/modules" nginx configuration prefix: "/usr/local/nginx/conf" nginx configuration file: "/usr/local/nginx/conf/nginx.conf" nginx pid file: "/usr/local/nginx/logs/nginx.pid" nginx error log file: "/usr/local/nginx/logs/error.log" nginx http access log file: "/usr/local/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
[root@rhel7-vm1 nginx-1.12.2]# make && make install # 編譯安裝
檢查配置完畢,便可編譯安裝。安裝過中通常不會出現報錯。
3)配置nginx實現開機自啓動
方法一:直接編輯自定義開機啓動腳本 /etc/rc.d/rc.local
[root@rhel7-vm1 ~]# cp /usr/local/nginx/sbin/nginx /usr/sbin/ [root@rhel7-vm1 ~]# vim /etc/rc.d/rc.local #!/bin/bash # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # # It is highly advisable to create own systemd services or udev rules # to run scripts during boot instead of using this file. # # In contrast to previous versions due to parallel execution during boot # this script will NOT be run after all other services. # # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure # that this script will be executed during boot. touch /var/lock/subsys/local /usr/local/nginx/sbin/nginx # 將nginx的可執行文件寫入開機自定義啓動腳本中
[root@rhel7-vm1 ~]# chmod 755 /etc/rc.d/rc.local # 添加可執行權限,實現開機自啓動
[root@rhel7-vm1 ~]# ls -lh /etc/rc.d/rc.local -rwxr-xr-x. 1 root root 473 Nov 17 23:51 /etc/rc.d/rc.local
方法二:編輯 /etc/init.d/nginx 腳本(該腳本由nginx官方提供,根據配置進行相應更改),使用chkconfig命令來實現開機自啓動。
該方法可使用 /etc/init.d/nginx {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest} 或 service 命令集中式管理。
[root@rhel7-vm1 ~]# vim /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
[root@rhel7-vm1 ~]# chmod 755 /etc/init.d/nginx
[root@rhel7-vm1 ~]# chkconfig --level 35 nginx on # chkconfig命令更改nginx運行級別,設置開機自啓動
[root@rhel7-vm1 ~]# service nginx restart # 重啓nginx服務 Restarting nginx (via systemctl): [ OK ] [root@rhel7-vm1 ~]# /etc/init.d/nginx status # 查看nginx服務狀態
4)添加nginx系統用戶與系統用戶組
從安全角度考慮,若是nginx遭受攻擊並被獲取權限,使用nginx系統用戶將下降主機被入侵的風險。也可使用除root外的其餘系統用戶與系統用戶組。
[root@rhel7-vm1 ~]# groupadd -r nginx # 添加系統用戶組nginx [root@rhel7-vm1 ~]# useradd -r -g nginx -M nginx -s /sbin/nologin # -r:添加系統用戶nginx;-g:添加到系統用戶組nginx;-M:不建立用戶家目錄;-s:指定登錄的shell爲/sbin/nologin(不容許登錄)
5)配置nginx反向代理與負載均衡
這次的配置文件使用基本的反向代理與負載均衡,較爲詳細的配置文件說明可參考:
http://www.javashuo.com/article/p-yheskdoy-ha.html
[root@rhel7-vm1 ~]# vim /usr/local/nginx/conf/nginx.conf user nginx nginx; # 使用nginx系統用戶與nginx系統用戶組 worker_processes 4; # nginx對外提供web服務時的worker進程數,一般設置成與cpu的核心數相等 error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid logs/nginx.pid; events { use epoll; # 使用epoll事件模型;epoll是多路複用IO(I/O Multiplexing)的一種方式,僅用於linux2.6以上內核,能夠大大提升nginx的性能。 worker_connections 1024; # 每個worker進程能併發處理(發起)的最大鏈接數(包含與客戶端或後端被代理服務器間等全部鏈接數)。 } http { # http全局塊 include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream nginx-static.com { # 加載負載均衡模塊;域名指向後端web服務器集羣 # ip_hash; # 默認狀況下使用輪詢(round-robin)模式,也可配置爲ip_hash模式 server 192.168.0.153:8080 max_fails=3 fail_timeout=30s; # max_fails: 容許失敗的次數,默認值爲1 server 192.168.0.154:8080 max_fails=3 fail_timeout=30s; # fail_timeout: 當max_fails次失敗後,暫停將請求分發到後端web服務器的時間 } server { listen 80; # 監聽master負載均衡節點80端口 server_name lb-ngx.com; # master負載均衡節點的域名 charset utf-8; # 使用utf-8字符串編碼格式 root /var/www; # 定義nginx服務的根目錄: /var/www
#access_log logs/host.access.log main; location / { # index index.html index.htm; # 定義首頁索引文件的名稱,即/var/www下的索引文件。 proxy_pass http://nginx-static.com; # 加載反向代理模塊: 將訪問http://lb-ngx.com根目錄文件的請求,所有代理分發到後端服務器。
proxy_redirect off; proxy_set_header Host $host; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for; proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 600; proxy_buffer_size 256k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; proxy_temp_file_write_size 256k; proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; proxy_max_temp_file_size 128m; } location /test { proxy_pass http://nginx-static.com/test; # 加載反向代理模塊: 將訪問http://lb-ngx.com/test的請求,所有代理分發到後端服務器。
proxy_redirect off; proxy_set_header Host $host; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; # 錯誤頁面 location = /50x.html { root html; } }
6)重啓nginx服務使配置生效,查看nginx運行狀態及端口使用。
[root@rhel7-vm1 ~]# /etc/init.d/nginx restart Restarting nginx (via systemctl): [ OK ] [root@rhel7-vm1 ~]# /etc/init.d/nginx status
[root@rhel7-vm1 ~]# netstat -tunlp | grep 80
2. 安裝及配置keepalived
1)下載並解壓keepalived-1.3.9
[root@rhel7-vm1 ~]# tar zxvf keepalived-1.3.9.tar.gz
2)編譯安裝keepalived及安裝排錯
[root@rhel7-vm1 ~]# cd keepalived-1.3.9 [root@rhel7-vm1 keepalived-1.3.9]# ./configure
檢查配置過程當中報錯以下:
*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.
解決方法:
[root@rhel7-vm1 ~]# yum install -y libnl libnl-devel # 安裝libnl及libnl-deve依賴包
再次檢查配置./configure,報錯以下:
configure: error: libnfnetlink headers missing
解決方法:
[root@rhel7-vm1 ~]# yum install -y libnfnetlink-devel
編譯安裝:
[root@rhel7-vm1 keepalived-1.3.9]# ./configure [root@rhel7-vm1 keepalived-1.3.9]# make && make install
[root@rhel7-vm1 ~]# cp /usr/local/keepalived-1.3.9/keepalived/keepalived /usr/sbin # 拷貝keepalived可執行文件 [root@rhel7-vm1 ~]# cp /usr/local/keepalived-1.3.9/keepalived/etc/sysconfig/keepalived /etc/sysconfig # 拷貝keepalived的systemctl配置文件,可由systemctl命令控制 [root@rhel7-vm1 ~]# cp -r /usr/local/keepalived-1.3.9/keepalived/etc/keepalived /etc # 拷貝keepalived的所有配置文件,不然配置完keepalived並啓動將報錯
3)keepalived高可用基本配置
keepalived的高可用經過vrrp的虛擬IP(VIP)來實現。
keepalived能夠經過自定義腳本來跟蹤nginx負載均衡服務的狀態。當master節點的nginx負載均衡服務down掉後,可經過腳本結束keepalived進程。
此時backup節點的keepalived偵測到原master節點的keepalived進程已中止,master節點的VIP漂移到backup節點上,即backup節點的keepalived由BACKUP狀態轉換爲MASTER狀態。
[root@rhel7-vm1 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { # acassen@firewall.loc # failover@firewall.loc # sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc # smtp_server 192.168.200.1 # smtp_connect_timeout 30 router_id LVS_DEVEL vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script chk_http_port { # vrrp_script定義腳本檢測nginx服務是否在運行 script "/opt/chk_ngx.sh" # 自定義腳本所在的路徑,並將腳本添加可執行權限。 interval 2 # 腳本執行的時間間隔;此處爲2s檢查一次 weight -5 # 腳本結果致使的優先級變動 fall 2 # 檢測2次失敗纔算是真的失敗 rise 1 # 檢測1次成功就算是真的成功 } vrrp_instance VI_1 { # vrrp實例;keepalived的virtual_router_id中priority(0-255)最大的成爲MASTER,也就是接管虛擬IP(VIP) state MASTER # 指定keepalived的狀態爲MASTER,但決定MASTER狀態的爲priority參數,該參數的權限值須要比BACKUP節點的設的要高,才能成爲真正的MASTER,不然會被BACKUP搶佔。 interface ens3 # 偵聽HA的網卡接口,防火牆規則需對該網卡指定vrrp協議。 virtual_router_id 51 # 虛擬路由標誌,該標誌是一個數字;在同一個vrrp實例中使用同一個標誌,即同一個vrrp實例中的MASTER和BACKUP使用相同的virtual_router_id。 priority 100 # 配置優先級;在同一個vrrp實例中,MASTER的優先級必須大於BACKUP的優先級,不然即便state定義爲MASTER,也會被優先級定義更高的BACKUP所搶佔。 advert_int 1 # 配置MASTER與BACKUP節點間互相檢查的時間間隔,單位是秒。 authentication { # 配置MASTER和BACKUP的驗證類型和密碼,二者必須同樣。 auth_type PASS # 配置vrrp驗證類型,主要有PASS和AH兩種。 auth_pass 1111 # 配置vrrp驗證密碼,在同一個vrrp_instance下,MASTER和BACKUP必須使用相同的密碼才能正常通訊。 } virtual_ipaddress { # vrrp虛擬IP(VIP),若是有多個VIP的話,能夠寫成多行。 192.168.0.16/24 } track_script { chk_http_port # 引用vrrp_script中定義的腳本,定時運行,可實現MASTER和BACKUP間的切換。 } }
[root@rhel7-vm1 ~]# vim /opt/chk_ngx.sh # 監測nginx負載均衡服務的腳本,可根據nginx進程狀態來切換keepalived的狀態。
#!/bin/bash
#
# Edited : 2017.11.12 16:16 by hualf.
# Usage : checking status of nginx. If nginx has been down,
# master node will restart nginx again. When nginx has started
# failedly, keepalived will be killed, and backup node will
# replace the master node.
#
status=$(ps -C nginx --no-headers | wc -l)
if [ $status -eq 0 ]; then # nginx服務中止後,再次啓動nginx。
/usr/local/nginx/sbin/nginx
sleep 2
counter=$(ps -C nginx --no-headers | wc -l)
if [ "${counter}" -eq 0 ]; then # nginx再次啓動失敗後,中止master節點的keepalived,切換並啓用backup節點。
systemctl stop keepalived
fi
fi
4)查看nginx及keepalived進程運行狀態
在master節點上均已開啓nginx及keepalived服務,keepalived此時爲MASTER狀態並與backup節點保持通訊。
master節點的虛擬IP(VIP)只能經過ip addr命令查看,沒法使用ifconfig命令查看。
********************************************************************************
backup節點:
1. 安裝及配置nginx
與master節點方法相似,參照master節點配置。
2. keepalived高可用基本配置
配置文件中只列出與master節點的差別項
! Configuration File for keepalived
global_defs {
notification_email {
# acassen@firewall.loc
# failover@firewall.loc
# sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
# smtp_server 192.168.200.1
# smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_http_port {
script "/opt/chk_ngx.sh"
interval 2
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP # backup節點的keepalived狀態必須配置爲BACKUP
interface ens3
virtual_router_id 51 # 在同一個vrrp實例中,master節點與backup節點的virtual_router_id必須相同。
priority 50 # backup節點的優先級必須小於master節點的優先級
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.0.16/24
}
track_script {
chk_http_port
}
}
3. 重啓nginx及keepalived服務
********************************************************************************
2個節點nginx web服務器:
1. 安裝及配置nginx(以192.168.0.153/24爲例)
安裝與master節點方法類似,具體配置以下:
[root@rhel7-vm3 ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;
worker_processes 2;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8080; # nginx web服務器監聽8080端口
server_name 192.168.0.153;
charset utf-8;
root /var/www; # 配置虛擬主機的根目錄: /var/www
#access_log logs/host.access.log main;
location / { # 根目錄中首頁的索引文件
index index.html index.htm;
}
location /test { # 根目錄中test子目錄首頁的索引文件
index index.html;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@rhel7-vm3 ~]# vim /var/www/index.html <html> <head> <title>Welcome to nginx!</title> </head> <body bgcolor="white" text="black"> <center> <h1>Welcome to nginx! 192.168.0.153</h1> </center> </body> </html>
[root@rhel7-vm3 ~]# vim /var/www/test/index.html <html> <head> <title>Welcome to nginx!</title> </head> <body bgcolor="white" text="black"> <center> <h1>Welcome to <A style="color:red;">test</A> nginx! 192.168.0.153</h1> </center> </body> </html>
2. 重啓nginx服務
2個nginx web服務器配置完成並重啓後,進行負載均衡與高可用測試。
********************************************************************************
負載均衡和高可用測試:
1. 當master節點的nginx及keepalived均正常提供服務時,在這次配置中nginx負載均衡使用輪詢模式,即192.168.0.153/24與192.168.0.154/24可以以類似的概率被訪問。
master節點的keepalived爲MASTER狀態,虛擬IP(VIP)在master節點上。
經過瀏覽器訪問http://lb-ngx.com與http://lb-ngx.com/test,分別可以訪問位於153站點及154站點上的主頁,而且經過刷新可以輪詢訪問兩個站點。
2. 當master節點的nginx負載均衡服務中止,而且再次嘗試重啓失敗後,keepalived進程也被中止,此時由backup節點來接管master節點的nginx負載均衡服務,backup節點keepalived
狀態轉變爲MASTER,虛擬IP(VIP)漂移到了backup節點上。經過瀏覽器訪問上述網站依然可以訪問,在這裏再也不展現。能夠經過查看日誌/var/log/messages來肯定master節點與backup節點
keepalived的狀態。
至此,Nginx + Keepalived(主備模式)的負載均衡高可用架構的基本配置已完成,該架構屬於輕量級負載均衡高可用架構。以後還會學習haproxy/pacemaker的原理及配置方法。