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


    前言

    我们俗称的3层,4层,7层都是相对于网络结构而言的, 表示是在网络7层架构的哪个层次实现的负载均衡; 四层负载均衡:工作在传输层,由于在传输层,只有TCP/UDP协议,这两种协议中除了包含源IP、目标IP以外,还包含源端口号及目的端口号。四层负载均衡服务器在接受到客户端请求后,以后通过修改数据包的地址信息(IP+端口号)将流量转发到应用服务器。 今天我们这个文章就给大家实战一下,通过docker使用nginx来实现4层的负载均衡。

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

    安装Nginx
    使用docker安装即可

    docker image pull nginx

    查看是否启动stream

    root@c6461d00cb4c:/etc/nginx# nginx -V
    1. nginx version: nginx/1.21.5
    2. built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
    3. built with OpenSSL 1.1.1k 25 Mar 2021
    4. TLS SNI support enabled
    5. configure arguments: --prefix=/etc/nginx
    6. --sbin-path=/usr/sbin/nginx
    7. --modules-path=/usr/lib/nginx/modules
    8. --conf-path=/etc/nginx/nginx.conf
    9. --error-log-path=/var/log/nginx/error.log
    10. --http-log-path=/var/log/nginx/access.log
    11. --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock
    12. --http-client-body-temp-path=/var/cache/nginx/client_temp
    13. --http-proxy-temp-path=/var/cache/nginx/proxy_temp
    14. --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
    15. --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
    16. --http-scgi-temp-path=/var/cache/nginx/scgi_temp
    17. --user=nginx --group=nginx
    18. --with-compat --with-file-aio --with-threads
    19. --with-http_addition_module
    20. --with-http_auth_request_module --with-http_dav_module
    21. --with-http_flv_module
    22. --with-http_gunzip_module --with-http_gzip_static_module
    23. --with-http_mp4_module
    24. --with-http_random_index_module --with-http_realip_module
    25. --with-http_secure_link_module --with-http_slice_module
    26. --with-http_ssl_module --with-http_stub_status_module
    27. --with-http_sub_module --with-http_v2_module --with-mail
    28. --with-mail_ssl_module
    29. --with-stream --with-stream_realip_module --with-stream_ssl_module
    30. --with-stream_ssl_preread_module --with-cc-opt='-g -O2
    31. -ffile-prefix-map=/data/builder/debuild/nginx-1.21.5/debian/debuild-base/nginx-1.21.5=.
    32. -fstack-protector-strong -Wformat -Werror=format-security -Wp,
    33. -D_FORTIFY_SOURCE=2
    34. -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'

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

    修改/etc/nginx/nginx.conf

    1. stream{
    2. upstream netty_test{
    3. server 192.168.56.1:6666 weight=1;
    4. server 192.168.56.1:7777 weight=1;
    5. }
    6. server {
    7. listen 6665;
    8. proxy_pass netty_test;
    9. }
    10. }

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

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

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

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

    1. user nginx;
    2. worker_processes auto;
    3. error_log /var/log/nginx/error.log notice;
    4. pid /var/run/nginx.pid;
    5. events {
    6. worker_connections 1024;
    7. }
    8. stream{
    9. upstream netty_test{
    10. server 192.168.85.1:6666 weight=1;
    11. server 192.168.85.1:7777 weight=1;
    12. }
    13. server {
    14. listen 6555;
    15. proxy_pass netty_test;
    16. }
    17. }
    18. http {
    19. include /etc/nginx/mime.types;
    20. default_type application/octet-stream;
    21. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    22. '$status $body_bytes_sent "$http_referer" '
    23. '"$http_user_agent" "$http_x_forwarded_for"';
    24. access_log /var/log/nginx/access.log main;
    25. sendfile on;
    26. #tcp_nopush on;
    27. keepalive_timeout 65;
    28. #gzip on;
    29. include /etc/nginx/conf.d/*.conf;
    30. }

    重新加载配置

    root@c6461d00cb4c:/etc/nginx# nginx -t
    1. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    2. 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层的负载均衡。 欢迎大家关注。

  • 相关阅读:
    进程与线程
    Flutter入门-Dart端和Native端的页面跳转
    【从部署服务器到安装autodock vina】
    TeleVis:基于NLP的新冠新闻舆情可视化项目
    数据结构───顺序表(梦开始的地方)
    C++指针访问数组 & 函数中用指针传参
    JS-37-jQuery06-ajax
    App性能指标(安装、冷启动、卸载、平均内存/cpu/fps/net)测试记录
    数据库 备份和恢复
    liteide 找不到 go 路径错误修复
  • 原文地址:https://blog.csdn.net/inthirties/article/details/126604925