負載均衡技術對於一個網站尤爲是大型網站的web服務器集羣來講是相當重要的!作好負載均衡架構,能夠實現故障轉移和高可用環境,避免單點故障,保證網站健康持續運行。
因爲業務擴展,網站的訪問量不斷加大,負載愈來愈高。現須要在web前端放置nginx負載均衡,同時結合keepalived對前端nginx實現HA高可用。
介紹下Nginx和keepalive
1.Nginxhtml
Nginx 是一個很強大的高性能Web和反向代理服務器,它具備不少很是優越的特性:
Nginx做爲負載均衡服務器:Nginx 既能夠在內部直接支持 Rails 和 PHP 程序對外進行服務,也能夠支持做爲 HTTP代理服務器對外進行服務。Nginx採用C進行編寫,不管是系統資源開銷仍是CPU使用效率都比 Perlbal 要好不少。
2.keepalive前端
Keepalived是Linux下面實現VRRP備份路由的高可靠性運行件。基於Keepalived設計的服務模式可以真正作到主服務器和備份服務器故障時IP瞬間無縫交接。兩者結合,能夠構架出比較穩定的軟件LB方案。
Nginx+keepalive高可用方式有兩種:
1.Nginx+keepalived 主從配置node
這種方案,使用一個vip地址,前端使用2臺機器,一臺作主,一臺作備,但同時只有一臺機器工做,另外一臺備份機器在主機器不出現故障的時候,永遠處於浪費狀態,對於服務器很少的網站,該方案不經濟實惠。
2.Nginx+keepalived 雙主配置linux
這種方案,使用兩個vip地址,前端使用2臺機器,互爲主備,同時有兩臺機器工做,當其中一臺機器出現故障,兩臺機器的請求轉移到一臺機器負擔,很是適合於當前架構環境。因此在這裏就詳細介紹下雙主模型配置nginx
系統centos7.4 64位
centos6.9 64位
前端node1服務器:DIP:192.168.92.136
VIP1:192.168.92.23
VIP2:192.168.92.24
前端node2服務器:DIP:192.168.92.133
VIP1:192.168.92.24
VIP2:192.168.92.23
後端服務器:web node3:192.168.92.123
web node4:192.168.92.124
web node5:192.168.92.125
咱們開始以前先把防火牆和selinux關掉,不少時候咱們服務器之間不通都是這些緣由形成的。 web
Nginx和keepalive的安裝很是簡單,咱們能夠直接使用yun來安裝。
yum install keepalived nginx -y
後端服務器咱們一樣用yum來裝上Nginx
後端node3vim
[root@node3 ~]# yum -y install nginx [root@node3 ~]# echo "this is 192.168.92.123" > /usr/share/nginx/html/index.html [root@node3 ~]# service nginx start [root@node3 ~]# curl 192.168.92.123 this is 192.168.92.123
後端node4後端
[root@node4 ~]# yum -y install nginx [root@node4 ~]# echo "this is 192.168.92.124" > /usr/share/nginx/html/index.html [root@node4 ~]# service nginx start [root@node4 ~]# curl 192.168.92.124 this is 192.168.92.124
後端node5centos
[root@node5 ~]# yum -y install nginx [root@node5 ~]# echo "this is 192.168.92.125" > /usr/share/nginx/html/index.html [root@node5 ~]# service nginx start [root@node5 ~]# curl 192.168.92.125 this is 192.168.92.125
[root@node2 ~]# vim /etc/nginx/conf.d/node2.conf #在擴展配置目錄中配置須要註釋掉主配置文件中的server部分 upstream web1 { #ip_hash; #hash綁定ip server 192.168.92.123:80; server 192.168.92.124:80; server 192.168.92.125:80; } server { listen 80; server_name www.node.com; index index.html index.htm; location / { proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://web1; } }
[root@node1 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id node1 vrrp_mcast_gruop4 224.0.100.23 } vrrp_script chk_haproxy { script "/etc/keepalived/chk_nginx.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER interface ens37 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 111123 } track_script { chk_nginx } virtual_ipaddress { 192.168.92.23 } } vrrp_instance VI_2 { state BACKUP interface ens37 virtual_router_id 151 priority 98 advert_int 1 authentication { auth_type PASS auth_pass 123123 } track_script { chk_nginx } virtual_ipaddress { 192.168.92.24 } }
[root@node2 ~]# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id node1 vrrp_mcast_gruop4 224.0.100.23 } vrrp_script chk_haproxy { script "/etc/keepalived/chk_nginx.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP interface ens34 virtual_router_id 51 priority 98 advert_int 1 authentication { auth_type PASS auth_pass 111123 } track_script { chk_nginx } virtual_ipaddress { 192.168.92.23 } } vrrp_instance VI_2 { state MASTER interface ens34 virtual_router_id 151 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 123123 } track_script { chk_nginx } virtual_ipaddress { 192.168.92.24 } }
此腳本做用是檢測Nginx是否運行,若是沒有運行就啓動Nginx
若是啓動失敗則中止keepalive,保證備用服務器正常運行。bash
[root@node2 ~]# cat /etc/keepalived/chk_nginx.sh #!/bin/bash status=$(ps -C nginx --no-heading|wc -l) if [ "${status}" = "0" ]; then systemctl start nginx status2=$(ps -C nginx --no-heading|wc -l) if [ "${status2}" = "0" ]; then systemctl stop keepalived fi fi
[root@node2 ~]# service nginx start [root@node2 ~]# service keepalived start [root@node3 ~]# service nginx start [root@node3 ~]# service keepalived start
[root@node2 ~]# ip a .......... ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:ca:0b:2b brd ff:ff:ff:ff:ff:ff inet 192.168.92.133/24 brd 192.168.92.255 scope global dynamic ens34 valid_lft 1293sec preferred_lft 1293sec inet 192.168.92.24/32 scope global ens34 valid_lft forever preferred_lft forever inet6 fe80::9bff:2e2b:aebb:e35/64 scope link valid_lft forever preferred_lft forever ......... [root@node1 ~]# ip a .......... ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:04:b6:17 brd ff:ff:ff:ff:ff:ff inet 192.168.92.136/24 brd 192.168.92.255 scope global dynamic ens37 valid_lft 1567sec preferred_lft 1567sec inet 192.168.92.23/32 scope global ens37 valid_lft forever preferred_lft forever inet6 fe80::7ff4:9608:5903:1a4b/64 scope link valid_lft forever preferred_lft forever ..........
[root@node1 ~]# curl http://192.168.92.23 this is 192.168.92.123 [root@node1 ~]# curl http://192.168.92.23 this is 192.168.92.124 [root@node1 ~]# curl http://192.168.92.23 this is 192.168.92.125 [root@node1 ~]# curl http://192.168.92.24 this is 192.168.92.124
手動中止Nginx後自動恢復啓動
[root@node1 ~]# systemctl stop nginx [root@node1 ~]# ss -tnlp State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:80 *:* users:(("nginx",pid=20257,fd=6),("nginx",pid=20256,fd=6)) LISTEN 0 128 *:22 *:* users:(("sshd",pid=913,fd=3)) LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=991,fd=13)) LISTEN 0 128 :::22 :::* users:(("sshd",pid=913,fd=4)) LISTEN 0 100 ::1:25 :::* users:(("master",pid=991,fd=14))