计算机系统应用教程网站

网站首页 > 技术文章 正文

Docker实战-使用NGINX实现4层的负载均衡

btikc 2025-01-15 11:05:25 技术文章 23 ℃ 0 评论

Docker实战-使用NGINX实现4层的负载均衡

前言

我们俗称的3层,4层,7层都是相对于网络结构而言的, 表示是在网络7层架构的哪个层次实现的负载均衡。 今天我们这个文章就给大家实战一下,通过docker使用nginx来实现4层的负载均衡。

四层负载均衡:工作在传输层,由于在传输层,只有TCP/UDP协议,这两种协议中除了包含源IP、目标IP以外,还包含源端口号及目的端口号。四层负载均衡服务器在接受到客户端请求后,以后通过修改数据包的地址信息(IP+端口号)将流量转发到应用服务器。

Nginx在web端应用广泛,更多的是在web层作为前端的proxy和负载均衡层, 在nginx1.9以后,支持stream方式,通过stream方式来实现TCP/UCP层的负载均衡;我们这里就是使用stream来实现的。


安装Nginx
使用docker安装即可

docker image pull nginx


查看是否启动stream

root@c6461d00cb4c:/etc/nginx# nginx -V
nginx version: nginx/1.21.5
built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
built with OpenSSL 1.1.1k  25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx 
--sbin-path=/usr/sbin/nginx 
--modules-path=/usr/lib/nginx/modules 
--conf-path=/etc/nginx/nginx.conf 
--error-log-path=/var/log/nginx/error.log 
--http-log-path=/var/log/nginx/access.log 
--pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock 
--http-client-body-temp-path=/var/cache/nginx/client_temp 
--http-proxy-temp-path=/var/cache/nginx/proxy_temp 
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp 
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp 
--http-scgi-temp-path=/var/cache/nginx/scgi_temp 
--user=nginx --group=nginx 
--with-compat --with-file-aio --with-threads 
--with-http_addition_module 
--with-http_auth_request_module --with-http_dav_module 
--with-http_flv_module 
--with-http_gunzip_module --with-http_gzip_static_module 
--with-http_mp4_module 
--with-http_random_index_module --with-http_realip_module 
--with-http_secure_link_module --with-http_slice_module 
--with-http_ssl_module --with-http_stub_status_module 
--with-http_sub_module --with-http_v2_module --with-mail 
--with-mail_ssl_module 
--with-stream --with-stream_realip_module --with-stream_ssl_module 
--with-stream_ssl_preread_module --with-cc-opt='-g -O2 
-ffile-prefix-map=/data/builder/debuild/nginx-1.21.5/debian/debuild-base/nginx-1.21.5=. 
-fstack-protector-strong -Wformat -Werror=format-security -Wp,
-D_FORTIFY_SOURCE=2 
-fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'

--with-stream 表示已经支持到了stream模块


修改/etc/nginx/nginx.conf

 stream{
         upstream netty_test{
           server 192.168.56.1:6666 weight=1;
           server 192.168.56.1:7777 weight=1;
        }
      
      server {           
            listen 6665;
            proxy_pass netty_test;
          }  
      }


注意这里有一个搞了好久的坑
stream这段,和http是一个级别的,所以stream这一段不能直接放在conf.d的文件夹里,启动会报错
错误如下:


nginx: [emerg] "stream" directive is not allowed here 
in /etc/nginx/conf.d/netty.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed


和http的定义放在一个层次, 可以参考nginx.conf里的写法

把stream这块直接放到nginx.conf的http段的上方

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}

 stream{
         upstream netty_test{
           server 192.168.85.1:6666 weight=1;
                                           
           server 192.168.85.1:7777 weight=1;
        }                                                                    
                                                                             
      server {                                                               
                                                                             
            listen 6555;                                                     
            proxy_pass netty_test;                                           
                                                                             
                                                                             
                                                                             
          }                                                                  
      }                                                                      
                                                                             
http {                                                                       
    include       /etc/nginx/mime.types;                                     
    default_type  application/octet-stream;                                  
                                                                             
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '  
                      '"$http_user_agent" "$http_x_forwarded_for"';
                                                
    access_log  /var/log/nginx/access.log  main;
                                     
    sendfile        on;              
    #tcp_nopush     on;              
                                     
    keepalive_timeout  65;           
                                     
    #gzip  on;                       
                                     
    include /etc/nginx/conf.d/*.conf;
}


重新加载配置

root@c6461d00cb4c:/etc/nginx# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful


TCP服务器代码

代码 https://gitee.com/inthirties_admin/netty
使用netty写的简单的服务端程序


运行服务端程序


重现启动容器

root@boot2docker:~# docker run -d -p 80:80 -p 6555:6555 -v nginx:/etc/nginx --name nginx nginx


36d2df0a1f1035c10f1a8bbb8139659a7b4836b83dbad33a2cd511303de0b31d


负载均衡测试



重新建立连接

?


结束语

Nginx在web端应用广泛,更多的是在web层作为前端的proxy和负载均衡层, 在nginx1.9以后,支持stream方式,通过stream方式来实现TCP/UCP层的负载均衡; 在本文章中使用docker的方式快速的搭建Nginx的负载均衡环境,并使用tcp客户端进行负载均衡的测试,nginx的stream方式是层结构的,nginx本身还提供7层的负载均衡实现,即简单的proxy反向代理的方式,这里就不介绍了。


下一篇文章将介绍使用docker环境使用haproxy来实现4层的负载均衡。 欢迎大家关注。

?

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表