使用Docker Compose启动NGINX Load Balancer

时间:2016-09-06 07:34:53

标签: nginx docker-compose

我一直关注如何使用docker-compose和nginx制作负载均衡的应用程序tutorial。但是,我的负载均衡器/协调器不起作用 - 我想要做的是让nginx接受请求并在三个worker之间拆分它们,我希望nginx和三个worker在不同的docker容器中运行,但是我得到了跟随错误。我的compilerwebservice_worker确实正常工作,我可以在docker ps中看到所有三个,我可以在他们正在收听的localhost帖子上用wget ping它们。

错误消息

$ docker-compose up
Starting compilerwebservice_worker1_1
Starting compilerwebservice_worker3_1
Starting compilerwebservice_worker2_1
Starting compilerwebservice_nginx_1
Attaching to compilerwebservice_worker1_1, compilerwebservice_worker3_1, compilerwebservice_worker2_1, compilerwebservice_nginx_1
nginx_1   | 2016/09/06 07:17:47 [emerg] 1#1: host not found in upstream "compiler-web-service" in /etc/nginx/nginx.conf:14
nginx_1   | nginx: [emerg] host not found in upstream "compiler-web-service" in /etc/nginx/nginx.conf:14
compilerwebservice_nginx_1 exited with code 1

NGINX配置

http {
    upstream compiler {
        least_conn;
        server worker1:4567;
        server worker2:4567;
        server worker3:4567;
    }

    server {
        listen 4567;
        location / {
                proxy_pass http://compiler;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
       }
    }
}

我的Docker-compose文件

nginx:
  build: ./src/main/nginx
  links:
      - worker2:worker2
      - worker3:worker3
      - worker1:worker1
  ports:
      - "4567:4567"

worker1:
  build: .
  ports:
      - "4567"

worker2:
  build: .
  ports:
      - "4567"

worker3:
  build: .
  ports:
      - "4567"

NGINX Docker文件

# Set nginx base image
FROM nginx

# Copy custom configuration file from the current directory
COPY nginx.conf /etc/nginx/nginx.conf

2 个答案:

答案 0 :(得分:1)

在下面的演示中,有 2 个 express 应用程序在 localhost 中使用端口 1111 和 2222 运行,在调用 http://localhost:8080 时,它应该自动选择端口 1111 或 2222 中的任何一个。这里 nginx 使用循环法

index.js 文件

const express = require('express');
const app = express();
const appId = process.env.APPID;
const PORTNUMBER = appId;
app.get('/', (req, res) => {
  res.send({
    message: `Welcome to ${appId} home page running on port ${appId}`
  });
});

app.listen(PORTNUMBER, () => {
  console.log(`APP STARTED ON PORT ${appId} for APP id ${appId}`);
})
 

快速应用 docker 文件

FROM node:12.13.0-alpine

WORKDIR /EXPRESSAPP

COPY ./API/package.json /EXPRESSAPP

RUN npm install

COPY ./API/. /EXPRESSAPP

CMD ["npm", "start"]
 

nginx 文件

 http {
  upstream backend {
    server 127.0.0.1:1111;
    server 127.0.0.1:2222;
  }

  server {
    listen 8080 default_server;
    listen [::]:8080 default_server;
    # listen [::]:8080 default_server ipv6only=on;
    server_name localhost;
    proxy_read_timeout 5m; 
    location / {
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_cache_bypass $http_upgrade;
      proxy_pass http://backend;
    }
  }
}

nginx dockercompose 文件

FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf

docker-compose.yml 文件

    version: '3'

services:
  myapp1:
    restart: always
    container_name: myapp1
    build: ./APICONTAINER
    environment: 
      - APPID=1111
    ports:
      - "1111:1111"
    network_mode: host

  myapp2:
    restart: always
    container_name: myapp2
    build: ./APICONTAINER
    environment:
      - APPID=2222
    ports:
      - "2222:2222"
    network_mode: host

  myproxy:
    container_name: myproxy
    build: ./NGINXCONTAINER
    ports: 
      - "127.0.0.1:8080:8080"
    depends_on: 
      - myapp1
      - myapp2
    network_mode: host
 

使用以下命令启动容器

sudo docker-compose down && sudo docker-compose up --build --force-recreate

转到下面的链接查看循环 nginx 负载均衡器

http://localhost:8080

reference github link to get the full code

答案 1 :(得分:0)

我需要在配置更改之间使用docker-compose build重建。当我更改应用程序的名称时,错误消息指示服务器,其名称是我选择的原始服务器而不是我不断更改的服务器。