如何在两个Docker容器之间实现Nginx反向代理负载平衡? [英] How to make an nginx reverse-proxy load balance between two docker containers?

查看:400
本文介绍了如何在两个Docker容器之间实现Nginx反向代理负载平衡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用相同的nodejs应用在两个容器之间实现nginx反向代理负载平衡.

I try to make an nginx reverse proxy load balance between two containers with the same nodejs app.

目录结构:

.
+-- docker-compose.yml
+-- nginx
+-- nodejs
|   +-- index.js
|   +-- …
+-- php

docker-compose.yml:

docker-compose.yml:

version: "3.1"

services:

  nginx-proxy:
    image: nginx:alpine
    ports:
      - "8000:80"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    links:
      - php:php-app
      - nodejs:nodejs-app

  nodejs:
    image: node:alpine
    environment: 
      NODE_ENV: production
    working_dir: /home/app
    restart: always
    volumes:
      - ./nodejs:/home/app
    command: ["node", "index.js"]

  php:
    image: php:apache
    volumes:
      - ./php:/var/www/html

index.js侦听端口8080

nginx conf default.conf:

nginx conf default.conf:

upstream nodejs-upstream {
  server nodejs-app:8080;
}

server {
  listen 80;
  root /srv/www;

  location / {
    try_files $uri @nodejs;  
  }

  location @nodejs {
    proxy_pass http://nodejs-upstream:8080;
    proxy_set_header Host $host;
  }

  location /api {
    proxy_pass http://php-app:80/api;
    proxy_set_header Host $host;
  }
}

现在我用

docker-compose up  --scale nodejs=2

负载均衡吗?

  • 我不这样认为,因为nodejs应用程序的两个实例在同一端口8080上侦听.
  • I don't think so because the two instances of the nodejs app listen on the same port 8080.

如何在nodejs应用的两个实例之间实现nginx服务器的负载平衡?

有更好的方法吗?

编辑1

我仍然很想知道如何在没有jwilder/nginx-proxy的情况下做到这一点.谢谢

I am still curious to know how to do that without jwilder/nginx-proxy. Thanks

编辑2

我有一些可以配合使用的东西:

I have something which kind of works with:

default.conf:

upstream nodejs-upstream {
  server nodejs_1:8080;
  server nodejs_2:8080;
}

这在两个nodejs容器启动时起作用.当我docker stop nodejs_2时,该应用程序仍然可用(负载平衡似乎可以正常工作),但是请求的结实速度可能很慢(在本地主机上长达1分钟).如果我重新启动此容器,它将再次正常工作……

This works while the two nodejs containers are up. When I docker stop nodejs_2, the app is still available (load-balancing seems to work), but the request can be really slow to end up (up to 1min on localhost). If I restart this container, it works fine again…

推荐答案

问有更好的方法吗?

Q. Is there a better way to do this?

是的,IMO.使用jwilder/nginx方法.它会自动更新并发现任何新容器,并将其添加到其平衡池中.

Yes, IMO. Use jwilder/nginx approach. It is automatically updated and discover any new container, adding it to its balancing pool.

https://github.com/jwilder/nginx-proxy

version: "3.1"

services:

  nginx-proxy:
    image: jwilder/nginx
    ports:
      - "8000:80"
     volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro

  nodejs:
    image: node:alpine
    environment: 
      NODE_ENV: production
      VIRTUAL_ENV: localhost
      VIRTUAL_PORT: 8080
    working_dir: /home/app
    restart: always
    volumes:
      - ../nodejs:/home/app
    command: ["node", "index.js"]

缩放时会自动更新Nginx.注意应用程序服务中的VIRTUAL_ENV环境变量.该变量将从nginx中读取.而且您不需要任何进一步的配置. (没有.conf文件)

Nginx wil be automatically updated when scaling. Note the VIRTUAL_ENV env var in the app service. That var will be read from nginx. And you don't need any further config. (None .conf file)

这篇关于如何在两个Docker容器之间实现Nginx反向代理负载平衡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆