使用Nginx为nginx docker容器做负载均衡的一次实践

使用Nginx为nginx docker容器做负载均衡的一次实践

Author:apt
Date:2018.12.18
usage:一次使用Nginx为五台docker容器中的nginx做负载均衡的实践笔记

架构图

架构图

测试环境

版本信息

  • 操作系统:CentOS Linux release 7.6.1810
  • Docker-ce:18.03.1
  • Nginx:1.15.7(Docker image)
  • Nginx:1.12.2(rpm)

环境简介

内网IPHostname操作系统安装软件作用
172.16.37.4apt-1CentOS 7.6Nginx反向代理和负载均衡作用
172.16.37.5apt-2CentOS 7.6DockerDocker运行的5个Nginx container作为被代理方

其他注意事项

  • Docker版本的选择:商业版可以选择docker-ee非商业版可以选择docker-ce
  • Nginx版本的选择:(待商榷)
  • Nginx镜像的选择:(待商榷)
  • 操作系统版本的选择:Centos7以上或者Ubuntu16.04以上(Ubuntu更适合使用Docker)

测试操作步骤

下面的内容直接使用Hostname代替IP

  1. 对两台服务器做一些初始化操作以及安全加固配置等(略过)

  2. 在apt-1上面安装nginx

    这里为了方便快速进行测试就直接使用yum进行安装了

    [root@apt-1 ~]# yum -y install nginx 
    
  3. 在apt-2上面安装Docker-CE

    安装Docker-CE的步骤

    # step 1: 安装必要的一些系统工具
    [root@apt-2 ~]# yum install -y yum-utils device-mapper-persistent-data lvm2
    # Step 2: 添加软件源信息
    [root@apt-2 ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    # Step 3: 更新并安装 Docker-CE
    [root@apt-2 ~]# yum makecache fast
    [root@apt-2 ~]# yum -y install docker-ce
    # Step 4: 开启Docker服务并且设置为开机启动
    [root@apt-2 ~]# systemctl start docker
    [root@apt-2 ~]# systemctl enable docker 
    

    检查一下docker的版本

    [root@apt-2 ~]# docker version 
    Client:
     Version:           18.09.0
     API version:       1.39
     Go version:        go1.10.4
     Git commit:        4d60db4
     Built:             Wed Nov  7 00:48:22 2018
     OS/Arch:           linux/amd64
     Experimental:      false
    
    Server: Docker Engine - Community
     Engine:
      Version:          18.09.0
      API version:      1.39 (minimum version 1.12)
      Go version:       go1.10.4
      Git commit:       4d60db4
      Built:            Wed Nov  7 00:19:08 2018
      OS/Arch:          linux/amd64
      Experimental:     false
    
  4. 在apt-2上面pull Nginx Image

    [root@apt-2 ~]# docker pull nginx
    Using default tag: latest
    latest: Pulling from library/nginx
    a5a6f2f73cd8: Pull complete 
    1ba02017c4b2: Pull complete 
    33b176c904de: Pull complete 
    Digest: sha256:5d32f60db294b5deb55d078cd4feb410ad88e6fe77500c87d3970eca97f54dba
    Status: Downloaded newer image for nginx:latest
    
  5. 在apt-2本地 /data/nginx 目录下分别创建nginx{1…5}用来存放5个nginx container需要的文件

    [root@apt-2 ~]# mkdir -p /data/nginx/nginx{1..5}
    [root@apt-2 ~]# mkdir -p /data/nginx/nginx{1..5}/conf.d    # 用来存放nginx的配置类文件
    [root@apt-2 ~]# mkdir -p /data/nginx/nginx{1..5}/html      # 用来存放html资源
    
  6. 对apt-1的Nginx配置和apt-2的Docker Nginx配置文件进行修改

    修改apt-1的Nginx配置文件,使访问Nginx能对nginx container进行轮询访问。如下所示:

    [root@apt-1 ~]# vim /etc/nginx/nginx.conf
    
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        
        upstream docker	   # 添加需要进行轮询访问的nginx container的IP与PORT
        {
            server 172.16.37.5:801;
            server 172.16.37.5:802;
        }
    
        server {
            listen       80;
            server_name  47.98.211.195;
            
            location / {
                proxy_pass http://docker;
            }
    
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }
    

    修改apt-2中nginx1与nginx2的default.conf配置文件(其他都可以默认,只要修改server_name为本机IP即可)

    [root@apt-2 ~]# vim /data/nginx/nginx1/conf.d/default.conf /data/nginx/nginx2/conf.d/default.conf  
    
    server {
        listen       80;
        server_name  172.16.37.5;
    
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
    
  7. 在apt-2的/data/nginx/nginx{1..5}/html目录下创建好index.html50x.html文件(这个是nginx container配置文件里面默认存在的,暂时就先按着一样的来操作比较好)

    编写nginx1的index.html

    [root@apt-2 nginx1]# vim /data/nginx/nginx1/html/index.html
    
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    this is nginx1 !!!
    </html>
    

    编写nginx2的index.html

    [root@apt-2 nginx1]# vim /data/nginx/nginx2/html/index.html
    
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    this is nginx2 !!!
    </html>
    

    50x.html则直接复制原来的文件内容即可,下面附上源文件内容

    root@97aae2c7c344:/usr/share/nginx/html# cat 50x.html 
    
    <!DOCTYPE html>
    <html>
    <head>
    <title>Error</title>
    <style>
        body {
            width: 35em;
            margin: 0 auto;
            font-family: Tahoma, Verdana, Arial, sans-serif;
        }
    </style>
    </head>
    <body>
    <h1>An error occurred.</h1>
    <p>Sorry, the page you are looking for is currently unavailable.<br/>
    Please try again later.</p>
    <p>If you are the system administrator of this resource then you should check
    the error log for details.</p>
    <p><em>Faithfully yours, nginx.</em></p>
    </body>
    </html>
    
  8. 在apt-2上面创建运行5个Nginx Container(其实2个就可以测试,所以暂时只使用2个container)

    创建运行2个nginx container,并把nginx需要的资源目录和nginx.conf配置文件挂载到container里面

    分别指定nginx的80端口映射到 801,802

    [root@apt-2 ~]# docker run -d -v "/data/nginx/nginx1/conf.d/:/etc/nginx/conf.d/" -v "/data/nginx/nginx1/html:/usr/share/nginx/html/" -p 801:80   --name nginx1 nginx:laster
    
    [root@apt-2 ~]# docker run -d -v "/data/nginx/nginx2/conf.d/:/etc/nginx/conf.d/" -v "/data/nginx/nginx2/html:/usr/share/nginx/html/" -p 802:80   --name nginx2 nginx:laster
    
  9. 启动所有的服务

    启动apt-1的Nginx服务

    [root@apt-1 ~]# systemctl start nginx 
    
  10. 测试验证

    在任意一台机器上访问 http://172.16.37.4:80,结果如下:

    [root@apt-1 ~]# curl 172.16.37.4
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    this is nginx2 !!!
    </html>
    [root@apt-1 ~]# curl 172.16.37.4
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    this is nginx1 !!!
    </html>
    [root@apt-1 ~]# curl 172.16.37.4
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    this is nginx2 !!!
    </html>
    [root@apt-1 ~]# curl 172.16.37.4
    <!DOCTYPE html>
    <html>
    <head>
    <title>Welcome to nginx!</title>
    this is nginx1 !!!
    </html>
    
  11. 总结

    通过本次的测试步骤,已经成功的完成了通过nginx服务做反向代理与负载均衡实现了对docker容器内部的5个nginx container进行轮询访问

参考博客、文章

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值