基於Nginx負載均衡方案

項目背景


公司一直使用商用負載均衡(LB),基於以下幾點原因考慮用開源產品來替代:


  • 價格昂貴,HTTPS支持併發數太低

  • 技術門檻比較高,學習成本大

  • 技術Bug修復方面都太慢

  • 商用產品在新功能技術支持方面(如H2,protocol_proxy支持)滯後


技術選型


主要調研了lvs/haproxy/nginx這三種開源產品在四層負載方面功能特性,新LB方案水平擴展相對容易,選型階段主要考察功能支持情況,情況如下:



綜上,同時負載均衡主要業務對象是web類型,現有運維人員對nginx比較熟悉,最終先把Nginx做四層負載。


方案設計


四層負載在最前端,後端七層負載。四層負載主要負責對外暴露公網IP,七層負載主要業務規則重寫。同時考慮多機房的容災,架構設計如下:


上圖主要做到以下組件冗餘:


  • 機房A和機房B即可主備也可雙主,避免單點

  • 四層接入方面,當OSPF發生故障,可以用Nginx做備用,直接指向L7服務器組

  • 雙機房L7服務器組也可以同時提供服務,避免單點

  • 雙機房L7服務器組配置保持同步,安裝方式採用靜態編譯安裝,複製考貝啓動即可


相關配置


  • 網絡相關配置

    • 服務器部分配置如下:


#OS基於Centos7,測試環境,生產環境根據實際情況修改
#安裝路由軟件
yum install quagga
#配置zebra
#cat /etc/quagga/zebra.conf
!
! Zebra configuration saved from vty
!   2017/09/28 15:57:12
!
hostname test-ssl-10-231.test.org #這個每臺名字要不同
password 8 WuN0UOEsh./0U
enable password 8 g9UPXyneQv2n.
log file /var/log/quagga/zebra.log
service password-encryption
#配置ospfd
# cat /etc/quagga/ospfd.conf
hostname test-ssl-10-231.test.org #每臺要不同
password 8 cQGHF4e9QbcA 
enable password 8 RBUKMtvgMhU3M
log file /var/log/quagga/ospfd.log
service password-encryption
!
!
!
interface eth2
 ip ospf authentication message-digest
 ip ospf message-digest-key 1 md5 pIW87ypU3d4v3pG7 #此處密碼告知網絡工程師
 ip ospf hello-interval 1
 ip ospf dead-interval 4
 ip ospf priority 0

router ospf
 ospf router-id 10.10.41.130 #每臺router-id要不一樣
 log-adjacency-changes
 network 10.10.41.0/24 area 0.0.0.0
 network 10.10.100.100/32 area 0.0.0.0 #宣告自己的ospf互邊地址和VIP地址,新增地址都在此處添加
 area 0.0.0.0 authentication message-digest
!
line vty
!

#啓動服務
systemctl enable zebra.service
systemctl enable ospfd.service
systemctl start zebra.service
systemctl start ospfd.service
#添加ospf和zebra保活,打開配置文件打開如下行行
vim /etc/sysconfig/quagga
WATCH_DAEMONS="zebra ospfd"
######策略路由配置,eth0指向默認路由,在eth1模擬公網進行配置######
#cat /etc/iproute2/rt_tables增加
100 wan41
#增加路由表相關配置
ip route add 10.10.41.0/24 dev eth1 src 10.10.41.130 table wan41
ip route add default via 10.10.41.250 table wan41
ip rule add from 10.10.41.130 table wan41
持久化到配置文件
cat route-eth1
10.10.41.0/24 dev eth2 src 10.10.41.130 table wan41
default via 10.10.41.250 table 100
cat rule-eth1
from 10.10.41.130 table wan41
######策略路由配置結束######


    • 交換機配置部分(略)


  • 增加zebra ospfd保活

    • 打開/etc/sysconfig/quagga註釋以下行:WATCH_DAEMONS="zebra ospfd"

  • nginx 七層配置,關鍵是日誌配置獲取ClientIP如下:


server context listen增加如下:
listen 80 proxy_protocol;
listen 443 http2 proxy_protocol;
#log_format ,要配置$proxy_protocol_addr $proxy_protocol_port,
log_format  xff  '$proxy_protocol_addr:$proxy_protocol_port $http_x_forwarded_for - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" "$http_user_agent" "$host" '
                      '$request_time "$upstream_addr" "$upstream_response_time" "$server_protocol"';


  • nginx tcp 四層代理配置


stream {
log_format proxy '$remote_addr:$remote_port [$time_local] '
                 '$protocol $status $bytes_sent $bytes_received '
                 '$session_time "$upstream_addr" '
                 '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
upstream backend-test {
    server 10.x.x.233:80;
}
upstream backend-test_ssl {
    server 10.x.x.233:443;
}
        server {
                listen 80;
                proxy_protocol on;
                proxy_pass backend-test;
                access_log  /opt/test/logs/nginx/m.test.com.log proxy ;
        }
        server {
                listen 443;
                proxy_protocol on;
                proxy_pass backend-test_ssl;
                access_log  /opt/test/logs/nginx/m.test.com.log proxy buffer=1k flush=1s;
        }
}


  • nginx 加入sysctemctl管理,並加入開機啓動


[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/opt/test/nginx/sbin/nginx
ExecReload=/opt/test/nginx/sbin/nginx -s reload
ExecStop=/opt/test/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
#開機啓動
systemctl enable nginx.service


運維管理


  • 新增IP


cat addip.sh
#!/bin/bash
ip=$1
pswd="test123"
expect -c " set timeout 30
eval spawn -noecho telnet 127.0.0.1 2604
expect \"Password:\"
send \"$pswd\r\"
expect \" *>\"
send \"enable\r\"
expect \"Password:\"
send \"$pswd\r\"
expect \" *#\"
send \"configure t\r\"
expect \" *(config)#\"
send \"router ospf\r\"
expect \" *(config-router)#\"
send \"network $ip/32 area 0.0.0.0\r\"
expect \" *(config-router)#\"
send \"w\r\"
send \"exit\r\"
send \"exit\r\"
send \"exit\r\"
interact" >/dev/null
###增加策略路由
ip addr add 10.10.100.103/32 dev lo:1
ip rule add from 10.10.100.103 table wan41
###持久化到配置文件
#rule-lo:1
from 10.10.100.103 table wan41


  • 保活


#故障,OSPF下線,恢復自動上線,監控
set mailserver mail.test.com port 25
set mail-format {
  from:[email protected]
  subject:Nginx-L4 $SERVICE $EVENT at $DATE
  message:Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION.
}
set alert [email protected]
check process nginx with pidfile /opt/test/nginx/logs/nginx.pid
  if does not exist for 3 cycles then exec "/bin/systemctl stop zebra"  else if succeeded for 3 cycles then exec "/bin/sh /opt/test/sysadmin/o
spf_start.sh"
check host  Nginx-L4 with address 10.x.x.250
  if failed ping count 5 with timeout 1 seconds then exec "/bin/systemctl stop zebra"  else if succeeded then exec "/bin/sh /opt/test/sysadmin/o
spf_start.sh"


  • 性能測試數據


主要測試七層SSL RSA 2048位加解密能力,2620CPU加裝了加速卡之後,併發TPS能達到26000。


數據分析


基於ES API獲取 帶寬,流量,PV數據 彙總後再次存入ES,最終使用grafana進行展示。


出處:https://zhuanlan.zhihu.com/p/32122459


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章