1. 程式人生 > >Nginx實現七層的負載均衡(LB Nginx)

Nginx實現七層的負載均衡(LB Nginx)

lb nginx



Nginx實現七層的負載均衡

調度到不同組後端服務器
1. 動靜分離
2. 網站進行分區
=================================================================================

拓撲結構

[vip: 20.20.20.20]

[LB1 Nginx] [LB2 Nginx]
192.168.1.2 192.168.1.3

[index] [milis] [videos] [images] [news]


1.11 1.21 1.31 1.41 1.51
1.12 1.22 1.32 1.42 1.52
1.13 1.23 1.33 1.43 1.53
... ... ... ... ...
/web /web/milis /web/videos /web/images /web
/news
index.html index.html index.html index.html


一、實施過程
方案一 根據站點分區進行調度
http {
upstream index {
server 192.168.1.11:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.12:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.13:80 weight=2 max_fails=2 fail_timeout=2;
}

upstream milis {
server 192.168.1.21:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.22:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.23:80 weight=2 max_fails=2 fail_timeout=2;
}

upstream videos {
server 192.168.1.31:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.32:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.33:80 weight=2 max_fails=2 fail_timeout=2;
}

upstream images {
server 192.168.1.41:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.42:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.43:80 weight=2 max_fails=2 fail_timeout=2;
}

upstream news {
server 192.168.1.51:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.52:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.53:80 weight=2 max_fails=2 fail_timeout=2;
}

server {
location / {
proxy_pass http://index;
}

location /news {
proxy_pass http://news;
}

location /milis {
proxy_pass http://milis;
}

location ~* \.(wmv|mp4|rmvb)$ {
proxy_pass http://videos;
}

location ~* \.(png|gif|jpg)$ {
proxy_pass http://images;
}
}


方案二 根據動靜分離進行調度
http {
upstream htmlservers {
server 192.168.1.1:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.2:80 weight=2 max_fails=2 fail_timeout=2;
}

upstream phpservers {
server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.4:80 weight=2 max_fails=2 fail_timeout=2;
}

server {
location ~* \.html$ {
proxy_pass http://htmlservers;
}

location ~* \.php$ {
proxy_pass http://phpservers;
}
}
}


二、Keepalived實現調度器HA
註:主/備調度器均能夠實現正常調度
1. 主/備調度器安裝軟件
[[email protected] ~]# yum -y install keepalived
[[email protected] ~]# yum -y install keepalived

2. Keepalived
BACKUP1
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
router_id director1 //輔助改為director2
}

vrrp_instance VI_1 {
state BACKUP
nopreempt
interface eth0 //心跳接口,盡量單獨連接心跳
virtual_router_id 80 //整個集群的調度器一致
priority 100 //輔助改為50
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
20.20.20.20
}
}

BACKUP2


3. 啟動KeepAlived(主備均啟動)
[[email protected] ~]# chkconfig keepalived on
[[email protected] ~]# service keepalived start
[[email protected] ~]# ip addr

到此:
可以解決心跳故障keepalived
不能解決Nginx服務故障



4. 擴展對調度器Nginx健康檢查(可選)
思路:
讓Keepalived以一定時間間隔執行一個外部腳本,腳本的功能是當Nginx失敗,則關閉本機的Keepalived

a. script
[[email protected] ~]# cat /etc/keepalived/check_nginx_status.sh
#!/bin/bash
/usr/bin/curl -I http://localhost &>/dev/null
if [ $? -ne 0 ];then
/etc/init.d/keepalived stop
fi

[[email protected] ~]# chmod a+x /etc/keepalived/check_nginx_status.sh

b. keepalived使用script
! Configuration File for keepalived

global_defs {
router_id director1
}

vrrp_script check_nginx {
script "/etc/keepalived/check_nginx_status.sh"
interval 5
}


vrrp_instance VI_1 {
state BACKUP
interface eth0
nopreempt
virtual_router_id 90
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass uplook
}

virtual_ipaddress {
192.168.1.80
}

track_script {
check_nginx
}

}

註:必須先啟動nginx,再啟動keepalived




調度到同一組後端服務器
網站沒按業務/版塊拆分,所有後端服務器提供整站代碼。
=================================================================================

拓撲結構

[LB Nginx]
20.20.20.20
192.168.1.2

[httpd] [httpd] [httpd]
192.168.1.3 192.168.1.4 192.168.1.5


實施過程
1. nginx
http {
upstream httpservers {
server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.4:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.5:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.100:80 backup; 等3、4、5 掛掉100上線
}

server {
location / {
proxy_pass http://httpservers;
proxy_set_header X-Real-IP $remote_addr;
}
}
}

2. Apache LogFormat 可選
LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

3. Nginx LogFormat

=================================================================================


本文出自 “八月長安” 博客,轉載請與作者聯系!

Nginx實現七層的負載均衡(LB Nginx)