1. 程式人生 > >使用nginx實現負載均衡

使用nginx實現負載均衡

logs sta all bsp erro 做了 ray net 部署

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

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

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

之前查過負載均衡的相關資料,由於資金所限無法直接


一、前提條件
- 環境中已經部署了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

使用nginx實現負載均衡