nginx+keepalived 雙主熱備負載均衡
前端
負載均衡技術對於一個網站尤爲是大型網站的web服務器集羣來講是相當重要的!作好負載均衡架構,能夠實現故障轉移和高可用環境,避免單點故障,保證網站健康持續運行。不少企業的網絡架構都須要用到nginx+keepalived的負載均衡技術,此時能夠用到兩種模式,分別是主從模式和雙主模式,這兩種模式的主要區別是:主從模式其中的一臺備用機是長時間處於空閒狀態的,而雙主模式的兩臺調度器都是一塊兒工做,這樣能合理利用資源以及分擔在一臺前端nginx服務器上的壓力。nginx
實現的過程主要仍是分爲兩個過程:web
1、前端兩臺nginx作反向代理到後面兩臺web服務器vim
2、對前端兩臺nginx作keepalived(心跳檢測) 後端
基本配置緩存
前端nginx fjw133:192.168.10.133 vip1:192.168.10.200(主) 192.168.10.200(備)bash
前端nginx fjw134:192.168.10.134 vip1:192.168.10.200(主) 192.168.10.220(備)服務器
後端web服務器 fjw132:192.168.10.132 網絡
後端web服務器 fjw135:192.168.10.135架構
1、先配置前端nginx反向代理
在fjw133上安裝nginx
一、wget nginx安裝包
cd /usr/local/nginx
wget http://nginx.org/download/nginx-1.13.6.tar.gz
二、建立nginx用戶
useradd nginx -s /sbin/nologin -M
三、解壓
tar -zxvf nginx-1.13.6.tar.gz
cd nginx-1.13.6
四、編譯
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
make && make install
五、檢測配置文件
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s start
/usr/local/nginx/sbin/nginx -s reload
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx -s reload
六、修改配置文件
vim /usr/local/nginx/conf/nginx.conf
#運行用戶 #user nobody; #啓動進程,一般設置成和cpu的數量相等 worker_processes 1; #全局錯誤日誌及PID文件 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; error_log /usr/local/nginx/logs/error.log notice; pid /usr/local/nginx/logs/nginx.pid; events { #單個後臺worker process進程的最大併發連接數 worker_connections 1024; } http { ##設定mime類型,類型由mime.type文件定義 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 65; #打開gzip壓縮 gzip on; upstream backend { server 192.168.10.132:80 max_fails=3 fail_timeout=30s;#代理的web服務 server 192.168.10.135:80 max_fails=3 fail_timeout=30s;#最大失敗3次直接踢掉,超過30秒踢掉 } server { listen 80; #監聽端口 server_name localhost #charset koi8-r; ##access_log logs/host.access.log main; location / { proxy_pass http://backend; ##對應上面upstream定義的backend proxy_store off; ##啓用本地緩存功能 proxy_redirect off; ##指定修改被代理服務器返回的響應頭中的location頭域跟refresh頭域數值 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; ##定義或添加字段傳遞給代理服務器的請求頭。 proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; } } }
七、檢測配置文件並從新加載服務
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload
一樣的步驟去配置fjw134,配置文件也相同,就再也不演示
八、而後配置後端兩臺web服務器
我用的也是nginx作web服務器,安裝過程和上面同樣,只須要發佈網站就能夠,也不詳細演示
九、接着能夠作keepalived的部分,安裝keepalived
yum install -y keepalived
十、修改配置文件
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived global_defs { notification_email { #故障發生時給誰發郵件 root@localhost } notification_email_from root@fjw133 #通知郵件從哪一個地址發出 smtp_server localhost smtp_connect_timeout 30 #鏈接smtp服務器的超時時間 router_id hostname #上面都是發郵件的部分,這裏就不演示 } vrrp_script chk_http_port { script "/test/check_nginx.sh" interval 2 weight -5 fall 2 rise 1 } vrrp_instance VI_1 { state MASTER #另外一臺的狀態是BACKUP interface ens33 virtual_router_id 51 #另外一臺的id要保持同樣 priority 100 ##權重必須比BACKUP高 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.10.200 } track_script { chk_nginx } } vrrp_instance VI_2 { state BACKUP #另外一臺的狀態是MASTER interface ens33 virtual_router_id 52 #與另外一臺的id要保持同樣 priority 99 #權重必須比master低 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.10.220 } track_script { chk_http_port } }
十一、fjw134也一樣要裝keepalived,其配置文件是
! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from root@localhost smtp_server localhost smtp_connect_timeout 30 router_id NodeA } vrrp_script chk_nginx { ##這個部分定義nginx反向代理服務的檢測腳本 script "/test/nginx_check.sh" ##腳本路徑 interval 2 ##檢測週期 weight -2 ##若是腳本被執行,那麼優先級將會下降2 fall 2 ##指監控幾回判斷爲失敗 rise 1 ##指監控幾回判斷爲成功 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.10.200 } track_script { chk_nginx } } vrrp_instance VI_2 { state MASTER interface ens33 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.10.220 } track_script { chk_nginx } }
十二、/test/check_nginx.sh是檢測nginx的腳本,若是nginx服務忽然down,腳本會自動重啓nginx,若是起不了就關掉keepalived,這樣服務就轉到另外一臺前端nginx
vim /test/check_nginx.sh
#!/bin/sh A=`ps -C nginx --no-header | wc -l` if [ $A -eq 0 ] ; then /usr/local/nginx/sbin/nginx sleep 2 A=`ps -C nginx --no-header | wc -l` if [ $A -eq 0 ] ; then systemctl stop keepalived fi fi
1三、此時能夠查看兩個vip,已經分別分配到兩臺前端nginx上
[root@fjw133 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:0a:17:8a brd ff:ff:ff:ff:ff:ff
inet 192.168.10.133/24 brd 192.168.10.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.10.200/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::9c2e:cd7b:fea:3bf9/64 scope link
[root@fjw134 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:95:72:dc brd ff:ff:ff:ff:ff:ff
inet 192.168.10.134/24 brd 192.168.10.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.10.220/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::46e:7225:ebf3:f9ca/64 scope link
valid_lft forever preferred_lft forever
inet6 fe80::538f:f55f:2ea8:6dd9/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
inet6 fe80::9c2e:cd7b:fea:3bf9/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
1四、測試關閉fjw133的nginx服務,但服務仍是會自動起來 ,fjw134也是同樣
[root@fjw133 ~]# /usr/local/nginx/sbin/nginx -s stop
[root@fjw133 ~]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 13847/nginx: master
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 878/sshd
1五、關閉fjw133的keepalived,VIP1會跳到fjw134上
[root@fjw133 ~]# systemctl stop keepalived
[root@fjw134 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:95:72:dc brd ff:ff:ff:ff:ff:ff
inet 192.168.10.134/24 brd 192.168.10.255 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.10.220/32 scope global ens33
valid_lft forever preferred_lft forever
inet 192.168.10.200/32 scope global ens33
valid_lft forever preferred_lft forever
1六、即便一臺前端nginx關掉了,訪問兩個VIP仍是能夠輪詢訪問兩個web服務器發佈的網頁