当前位置:首页 > 技术文章 > 正文内容

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

arlanguage4个月前 (01-11)技术文章25

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层的负载均衡。 欢迎大家关注。

?

扫描二维码推送至手机访问。

版权声明:本文由AR编程网发布,如需转载请注明出处。

本文链接:http://www.arlanguage.com/post/752.html

标签: nginx sub
分享给朋友:

“Docker实战-使用NGINX实现4层的负载均衡” 的相关文章

nginx限制php程序“跨站”访问 nginx限制只能域名访问

我秀站外合作有一个需求:需在一台web服务器上增加一个虚拟主机用来做图片资源站,所用程序为第三方,担心有后门程序,因此希望最好隔断与原机器其他服务的关系。思考了一下,确实有一些风险存在。目前我们服务器上都统一使用nobody用户启动nginx和php,包括web目录,这些机器上部分有多个域名在一起运...

Linux服务器Centos7系统Nginx安装与配置

一 安装编译工具及库文件yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel二首先要安装 PCREPCRE 作用是让 Nginx 支持 Rewrite 功能。2.1 下载 PCRE 安装包cd /usr/...

nginx配置集群 -websocket

nginx配置集群 -websocket前几天做一个nginx的反向代理,来代理websocket。因为上线时间的问题,所以是单节点运行。现在准备做集群优化,然后上容器环境。这样就需要配置nginx的负载均衡。不废话了,下面是配置文件。当然配置后需要验证,验证的时候会出现很多奇怪的问题。这里就不进行...

性能优化大揭秘:从代码到架构,全方位提升系统性能的实战技巧

在现代软件开发中,系统性能优化是一个永恒的话题。无论是移动应用、Web应用还是分布式系统,性能始终是用户体验、系统稳定性以及业务可扩展性的关键因素。性能瓶颈的存在不仅可能导致用户流失,还可能增加系统维护成本,甚至影响到业务的正常运行。因此,从代码层面到架构层面,全面的性能优化是每个技术人员必须掌握的...

php手把手教你做网站(二十四)jquery ajax上传大的附件

上传大的附件分为两种情况,可以改变服务器配置;不能改变配置;第2种使用分片上传优势:可以突破服务器上传大小的限制,可以web存储上传到哪一块了,在浏览器关闭或者刷新的情况下可以断点续传;劣势:上传速度慢,在我本地电脑测试,200M的文件,改变配置按照正常方式上传大约需要12到15秒,但是使用第2种分...

一文教会你如何使用 iLogtail SPL 处理日志

作者:阿柄随着流式处理的发展,出现了越来越多的工具和语言,使得数据处理变得更加高效、灵活和易用。在此背景下,SLS 推出了 SPL(SLS Processing Language) 语法,以此统一查询、端上处理、数据加工等的语法,保证了数据处理的灵活性。iLogtail 作为日志、时序数据采集器,在...