通過nginx進行udp報文負載均衡

YaoSmallWhite發表於2019-02-25

1. 安裝nginx

1.1 通過yum安裝

[root@yaoxiang ~]# yum install nginx
複製程式碼

1.2 檢視nginx的版本

[root@yaoxiang ~]# nginx -v
nginx version: nginx/1.12.2
複製程式碼

nginx的版本必須高於1.9.0,因為從1.9開始nginx就支援對TCP的轉發,而到了1.9.13時,UDP轉發也支援了。

1.3 檢視預設編譯引數

[root@yaoxiang ~]# nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_auth_request_module --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
複製程式碼

nginx實現TCP/UDP的轉發依靠的是Stream模組,檢視預設編譯引數中是含有**--with-stream引數,可以看到上面的引數裡含有--with-stream=dynamic**,說明已動態載入Stream模組

2. 修改nginx配置

2.1 增加stream塊

[root@yaoxiang ~]# vim /etc/nginx/nginx.conf
...
events {
    worker_connections 1024;
}
stream {
#定義被代理的伺服器組upstream 組名
upstream dns {
 # zone   dns 64k;
  server 172.27.9.204:4005;#代理的伺服器地址
  server 172.27.9.204:4006 weight=5;#weight 權重,預設都是1
  server 172.27.9.204:4007 max_fails=3 fail_timeout=30s;#max_fails - NGINX將伺服器標記為不可用的連續失敗嘗試次數。fail_timeout為NGINX認為伺服器不可用的時間長度。
  server 172.27.9.204:4008 backup;#backup 備用伺服器,其他伺服器不可用時,才會收到轉發
}

server {
  listen 4000 udp;#監聽udp4000埠,如不加udp則預設監聽tcp

  proxy_responses 1;#1代表需要回應,並將迴應轉發;0代表不需要回應

  proxy_timeout 20s;#迴應超時時間,超時未迴應暫停轉發

  proxy_pass dns;#代理伺服器、伺服器組

  proxy_buffer_size 512k;#響應緩衝區大小,如果後端傳送的響應頭過大可以嘗試增加此緩衝。

}

}
http {
...}
複製程式碼

2.2 負載均衡策略

  • round-robin(輪詢)——預設,Nginx使用輪詢演算法負載均衡通訊。因為是預設方法,所以沒有round-robin指令;只建立upstream配置塊在頂級stream上下文並像之前步驟新增server指令。

  • last_conn(最少連線)——Nginx選擇當前活躍連線數較少的伺服器。

  • least_tome——Nginx選擇最低平均延遲和最少活躍連線的伺服器。最低活躍連線基於least_time指令的以下引數計算:

    • connect——連線upstream伺服器的時間

    • first_byte——接收第一個資料位元組的時間

    • last_byte——從伺服器接收完整響應的時間

  • hash——Nginx基於使用者定義鍵選擇伺服器,例如,源IP地址

#####例子:採用最少連線和hash負載均衡策略

upstream dns {
 last_conn;#最少連線策略
 server 172.27.9.204:4005;#代理的伺服器地址
 server 172.27.9.204:4006;
 server 172.27.9.204:4007;
 server 172.27.9.204:4008;
 }
複製程式碼
upstream stream_backend {

    hash $remote_addr;

    server backend1.example.com:12345;

    server backend2.example.com:12345;

    server backend3.example.com:12346;

}
複製程式碼

3.啟動nginx

[root@yaoxiang nginx]# sudo nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
複製程式碼

出現以上提示說明埠被佔用,修改nginx.conf裡http模組中的埠號

4. 測試

4.1 向nginx主機傳送報文

5c6cc58eca105

4.2 可以看到代理伺服器組主機的不同埠都接收到了

4006埠

*5c6cc5f85088e*

4005埠

5c6cc63f11161

4.3 接收端代理伺服器向nginx主機回覆訊息

5c6cc6d772438

4.4 可以看到傳送端收到了回覆

5c6cc7812a6dc

相關文章