zoukankan      html  css  js  c++  java
  • 使用nginx实现负载均衡

    ===============================================

     2018/11/11_第1次修改                       ccb_warlock

     

    ===============================================

     之前查过负载均衡的相关资料,由于资金所限无法直接买负载均衡来玩,但是又不了解大厂是怎么实现负载均衡方案。后来了解到nginx可以实现简单的负载均衡,就针对功能进行了试验。

     


     一、前提条件
    - 环境中已经部署了docker swarm(http://www.cnblogs.com/straycats/p/8978135.html)
    - 最好也部署了portainer(http://www.cnblogs.com/straycats/p/8978201.html)
    - 部署nginx容器的服务器IP:192.168.12.7
    - 业务服务器IP:192.168.13.1、192.168.13.2、192.168.13.3
    - 默认swarm创建了network:my-net

     


     二、创建nginx容器

     2.1 创建目录

    mkdir -p /usr/docker-vol/nginx/conf/conf.d

     2.2 编辑nginx.conf

    vi /usr/docker-vol/nginx/conf/nginx.conf

     # 添加下面的内容到nginx.conf中,wq保存。

    user nginx;
    worker_processes auto;
    pid /run/nginx.pid;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
        keepalive_timeout  65;
    
        sendfile on;
        tcp_nopush     on;
        gzip  on;
        gzip_disable "msie6";
    }

     2.3 编辑www.conf

    vi /usr/docker-vol/nginx/conf/conf.d/www.conf

     # 添加下面的内容到nginx配置文件内,wq保存。

    upstream abtest{
        # 设置负载的权重比为1:1:2(即将本服务器1/4的请求转到13.1的8080端口,1/4的请求转到13.1的7777端口,1/2的请求转到13.2的8080端口,13.3当前不会收到请求,13.4在其他3个server忙的时候才会收到请求)
        server 192.168.13.1:8080 weight=1;
        server 192.168.13.1:7777 weight=1;# 也支持伪集群的负载均衡
        server 192.168.13.2:8080 weight=2;
        server 192.168.13.3:8080 weight=3 down;# down表示该服务器当前不进行负载
        server 192.168.13.4:8080 weight=3 backup;# down表示该服务器当前不进行负载
    }
    
    server {
        listen 80; 
    
        access_log  off;
        error_log   off;
    
        location = /abc.txt {
            proxy_pass http://abtest/abc.txt;
    
            client_max_body_size   10m;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    
    }

     2.4 启动容器

     # 登录protainer,将下面的内容添加到一个新的栈vedi-stack中,wq保存。

    version: '3.6'
    services:
    
      nginx:
        image: nginx:1.14.0-alpine
        environment:
          - TZ=Asia/Shanghai
        volumes:
          - /usr/docker-vol/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
          - /usr/docker-vol/nginx/conf/conf.d:/etc/nginx/conf.d
        deploy:
          replicas: 1
          restart_policy:
            condition: any
          resources:
            limits:
              cpus: "1"
              memory: 500M
          update_config:
            parallelism: 1
            delay: 5s
            monitor: 10s
            max_failure_ratio: 0.1
            order: start-first
        ports:
          - 80:80
        networks:
          - my-net
    
    networks:
      my-net:
        external: true

    由于nginx做了负载均衡,访问http://192.168.12.7/abc.txt会负载到http://192.168.13.1:8080/abc.txt、http://192.168.13.1:7777/abc.txt、http://192.168.13.2:8080/abc.txt

     

     

    PS.这里特别说明,upstream在做负载均衡时不支持灵活配置端口,所以不能写成下面的格式:

    upstream abtest{
        server 192.168.13.1 weight=1;
        server 192.168.13.2 weight=2;
        server 192.168.13.3 weight=3 down;
        server 192.168.13.4 weight=3 backup;
    }
    
    server {
        listen 80; 
    
        access_log  off;
        error_log   off;
    
        location = /abc.txt {
            proxy_pass http://abtest:8080/abc.txt;
    
            client_max_body_size   10m;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    
    }

    如果写成上面的格式,nginx的启动后会提示异常:

    nginx: [emerg] upstream "abtest" may not have port 8080 in /etc/nginx/conf.d/www.conf:14

     参考资料:

     1.https://www.cnblogs.com/ChoviWu/p/9004725.html
     2.https://www.cnblogs.com/zhoading/p/8036205.html
     3.https://www.cnblogs.com/wzjhoutai/p/6932007.html

     

     

  • 相关阅读:
    获取ip地址,
    手机div侧滑删除
    swiper左右选项卡滑动
    table-cell使用
    返回和刷新
    电脑浏览器计算高度和宽度
    css 空格
    时间js
    Nodejs仿Apache的部分功能
    Nodejs中的JavaScript
  • 原文地址:https://www.cnblogs.com/straycats/p/9944412.html
Copyright © 2011-2022 走看看