於Nginx負載均衡方案前端
1、技術選型
主要調研了lvs/haproxy/nginx這三種開源產品在四層負載方面功能特性,新LB方案水平擴展相對容易,選型階段主要考察功能支持狀況,狀況以下:nginx
綜上,同時負載均衡主要業務對象是web類型,現有運維人員對nginx比較熟悉,最終先把Nginx作四層負載。
2、方案設計
四層負載在最前端,後端七層負載。四層負載主要負責對外暴露公網IP,七層負載主要業務規則重寫。同時考慮多機房的容災,架構設計以下:web
上圖主要作到如下組件冗餘:
· 機房A和機房B便可主備也可雙主,避免單點
· 四層接入方面,當OSPF發生故障,能夠用Nginx作備用,直接指向L7服務器組
· 雙機房L7服務器組也能夠同時提供服務,避免單點
· 雙機房L7服務器組配置保持同步,安裝方式採用靜態編譯安裝,複製考貝啓動便可
3、相關配置vim
網絡相關配置
服務器部分配置以下:
[Java] 純文本查看 複製代碼
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63後端
yum install quaggabash
!
! 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服務器
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
!session
systemctl enable zebra.service
systemctl enable ospfd.service
systemctl start zebra.service
systemctl start ospfd.service架構
vim /etc/sysconfig/quagga
WATCH_DAEMONS="zebra ospfd"
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以下:
[Java] 純文本查看 複製代碼
?
1
2
3
4
5
6
7
server context listen增長以下:
listen 80 proxy_protocol;
listen 443 http2 proxy_protocol;
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 四層代理配置
[Java] 純文本查看 複製代碼
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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管理,並加入開機啓動
[Java] 純文本查看 複製代碼
?
01
02
03
04
05
06
07
08
09
10
11
12
13
[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
4、運維管理
新增IP
[Java] 純文本查看 複製代碼
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
cat addip.sh
ip=$1
pswd="test123"
expect -c " set timeout 30
eval spawn -noecho telnet 127.0.0.1 2604
expect "Password:"
send "$pswdr"
expect " *>"
send "enabler"
expect "Password:"
send "$pswdr"
expect " *#"
send "configure tr"
expect " *(config)#"
send "router ospfr"
expect " *(config-router)#"
send "network $ip/32 area 0.0.0.0r"
expect " *(config-router)#"
send "wr"
send "exitr"
send "exitr"
send "exitr"
interact" >/dev/null
ip addr add 10.10.100.103/32 dev lo:1
ip rule add from 10.10.100.103 table wan41
from 10.10.100.103 table wan41
保活
[Java] 純文本查看 複製代碼
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
set mailserver mail.test.com port 25
set mail-format {
from:devops-notice@test.com
subject:Nginx-L4 $SERVICE $EVENT at $DATE
message:Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION.
}
set alert [email]admin@test.com[/email]
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進行展現。