【问题标题】:Trouble scaling docker container无法扩展 docker 容器
【发布时间】:2019-02-13 08:56:08
【问题描述】:

我有一个 dockerized 烧瓶应用程序,它具有以下结构

code_docker
├── docker-compose.yml
├── micr
│   ├── app.py
│   ├── Dockerfile
│   ├── requirements.txt
│   └── templates
│       └── index.html
├── nginx
│   ├── Dockerfile
│   └── nginx.conf
└── README.md

我使用nginx作为负载均衡器,docker-compose.yml和nginx.conf里面的代码在下面提到

docker-compose.yml

version: '3'

services:

  nginx:
    container_name: nginx
    build: ./nginx
    restart: always
    deploy:
      restart_policy:
        condition: on-failure
    ports:
      - "8540:8540"
    depends_on:
      - micr

  micr:
    build: ./micr
    restart: always 
    deploy: 
      resources:
        limits:
          cpus: '0.50' # 50% of available processing time (CPU)
          memory: 50M  # 50M of memory
        reservations:
          cpus: '0.25'
          memory: 20M
      restart_policy:
        condition: on-failure   
    expose:
      - "8530"
    command: gunicorn -w 1 -b :8530 app:app

nginx.conf

user nginx;

worker_processes 4;

events {
    worker_connections 1024;
}

http {
    keepalive_timeout 15;

    server {
        listen 8540;
        server_name localhost;

        location / {
            proxy_pass http://localhost:8530;
            #proxy_redirect off;
            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-Forward-Host $server_name;
        }
    }
}

我可以发出以下命令来缩放容器而不会出现任何问题,这可以从message 看到

docker-compose up --scale micr=4

但是当我尝试访问它们时,我得到了这个错误

【问题讨论】:

  • 你是如何连接的? 0.0.0.0 看起来不对。
  • @tripleee http://localhost:8530/ 或 http://0.0.0.0:8530/
  • localhost 是 127.0.0.1,而不是 0.0.0.0

标签: python docker nginx flask docker-compose


【解决方案1】:
  • 您需要为 Nginx 和代理容器创建网络。详情请见this。
services:
  nginx:
    networks:
      - proxy
    ...

  micr:
    networks:
      - proxy
    ...

networks:
  proxy:
  • 您需要使用服务名称作为容器主机地址来归档容器之间的通信。 所以将proxy_pass 设置为http://micr:8530
  • 您应该将 docker Embedded dns 设置为 nginx 解析器,以实现无需重新启动 nginx 容器即可重新创建容器的能力。详情请见this

【讨论】:

  • 你是救世主
  • (Docker Compose 会自动为你创建一个网络,你不需要明确地创建一个;但是使用容器名称作为主机名的建议是正确的。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-28
相关资源
最近更新 更多