使用Traefik和Docker错误在EBS(弹性Beanstalk服务)中提供Django应用服务:502错误网关

时间:2020-08-23 10:34:14

标签: django docker amazon-elastic-beanstalk traefik cookiecutter-django

我正在尝试使用带有TravisCI,Docker的Django Cookiecutter(https://github.com/pydanny/cookiecutter-django)以及使用Traefik作为路由器来部署多容器应用程序。我已经通过绿色健康检查成功地将应用程序部署到了AWS中的EBS(弹性Beanstalk服务)应用程序。我当前遇到的唯一问题是,如果我尝试访问分配的主机名或域,则会收到502 bad gateway错误。我怀疑traefik.yml文件中的配置错误或Dockerrunaws.json文件中的端口映射错误。请帮助我找到错误。

以下是配置:

请注意,“ myhostname.com”是指我的实际DNS主机名,“ myusername”是指我的实际docker集线器用户名。

我还使用Route 53将带有A名称记录的主机名映射到我的ELB实例。

traefik.yml

log:
  level: INFO

entryPoints:
  web:
    # http
    address: ":80"

  flower:
    address: ":5555"

http:
  routers:
    web-router:
      rule: "Host(`myhostname.com`)"
      entryPoints:
        - web
      middlewares:
        - csrf
      service: django

    flower-router:
      rule: "Host(`myhostname.com`)"
      entryPoints:
        - flower
      service: flower

  middlewares:
    csrf:
      # https://docs.traefik.io/master/middlewares/headers/#hostsproxyheaders
      # https://docs.djangoproject.com/en/dev/ref/csrf/#ajax
      headers:
        hostsProxyHeaders: ["X-CSRFToken"]

  services:
    django:
      loadBalancer:
        servers:
          - url: http://django:5000

    flower:
      loadBalancer:
        servers:
          - url: http://flower:5555

providers:
  # https://docs.traefik.io/master/providers/file/
  file:
    filename: /etc/traefik/traefik.yml
    watch: true

docker-compose.yml

version: "3"

volumes:
  production_traefik: {}

services:
  django: &django
    build:
      context: .
      dockerfile: ./compose/production/django/Dockerfile
    image: vincegnzls/test-django
    command: /start

  traefik:
    build:
      context: .
      dockerfile: ./compose/production/traefik/Dockerfile
    image: vincegnzls/test-traefik
    depends_on:
      - django
    volumes:
      - production_traefik:/etc/traefik/acme
    ports:
      - "0.0.0.0:80:80"
      - "0.0.0.0:443:443"
      - "0.0.0.0:5555:5555"

  celerybeat:
    <<: *django
    image: vincegnzls/test-celerybeat
    command: /start-celerybeat

  flower:
    <<: *django
    image: vincegnzls/test-flower
    command: /start-flower

Dockerrun.aws.json

{
    "AWSEBDockerrunVersion": 2,
    "containerDefinitions": [
        {
            "command": [
                "/start"
            ],
            "image": "myusername/test-1",
            "name": "django",
            "memory": 256
        },
        {
            "essential": true,
            "image": "myusername/test-2",
            "name": "traefik",
            "portMappings": [
                {
                    "containerPort": 80,
                    "hostPort": 80
                },
                {
                    "containerPort": 443,
                    "hostPort": 443
                },
                {
                    "containerPort": 5555,
                    "hostPort": 5555
                }
            ],
            "memory": 128
        },
        {
            "command": [
                "/start-celerybeat"
            ],
            "image": "myusername/test-3",
            "name": "celerybeat",
            "memory": 128
        },
        {
            "command": [
                "/start-flower"
            ],
            "image": "myusername/test-4",
            "name": "flower",
            "memory": 128
        }
    ]
}

Traefik Dockerfile

FROM traefik:v2.0
RUN mkdir -p /etc/traefik/acme
RUN touch /etc/traefik/acme/acme.json
RUN chmod 600 /etc/traefik/acme/acme.json
COPY ./compose/production/traefik/traefik.yml /etc/traefik

Django相关设置:

ALLOWED_HOSTS=.myhostname.com
DJANGO_SECURE_SSL_REDIRECT=False

其余的Django环境设置已经在我的ELB的环境属性中。

0 个答案:

没有答案