Traefik背后的Docker Swarm Nginx服务给出502错误

时间:2019-06-25 07:48:14

标签: docker nginx traefik

我有一个docker swarm配置为使用Traefik作为反向代理。我的集群中的一个容器正在运行Nginx服务器,但是导航到该特定端点时出现502 Bad Gateway错误。 Traefik的设置如下:

version: '3.5'
services:
  traefik:
    image: traefik:alpine
    command: |-
      --entryPoints="Name:http Address::80 Redirect.EntryPoint:https"
      --entryPoints="Name:https Address::443 TLS"
      --defaultentrypoints="http,https"
      --accesslogsfile="/var/log/access.log"
      --acme 
      --acme.acmelogging="true"
      --acme.domains="${SERVER},${SANS1}"
      --acme.email="${ACME_EMAIL}"
      --acme.entrypoint="https"
      --acme.httpchallenge
      --acme.httpchallenge.entrypoint="http"
      --acme.storage="/opt/traefik/acme.json"
      --acme.onhostrule="true"
      --docker
      --docker.swarmmode
      --docker.domain="${SERVER}"
      --docker.network="frontend"
      --docker.watch
      --api
    networks:
      - frontend
    ports: 
      - target: 80
        published: 80
        mode: host
      - target: 443
        published: 443
        mode: host
      - target: 8080
        published: 8080
        mode: host
    volumes:
      - traefik_acme:/opt/traefik
      - traefik_logs:/var/log/access.log
      - /var/run/docker.sock:/var/run/docker.sock:ro
    deploy:
      replicas: 1
      placement:
        constraints: [node.role == manager]
networks:
  frontend:
    name: "frontend"
    driver: overlay
volumes:
  traefik_acme:
  traefik_logs:

此撰写文件提供了overlay网络和Traefik服务。我的其他团队在以下撰写文件中定义:

version: "3.5"
services:
  test:
    image: emilevauge/whoami
    deploy:
      labels:
        traefik.enable: "true"
        traefik.frontend.rule: "Host:${SERVER};PathPrefixStrip:/test"
        traefik.port: 80
    networks:
      - frontend
  octeditor:
    image: ${DOCKER_OCTEDITOR_IMAGE_TAG}
    deploy:
      replicas: 1
      labels:
        traefik.enable: "true"
        traefik.frontend.rule: "Host:${SERVER}"
        traefik.port: 3000
    networks:
      - frontend
    ports:
      - "3000:80"
  octserver:
    image: ${DOCKER_OCTSERVER_IMAGE_TAG}    
    deploy:
      replicas: 1
      labels:
        traefik.enable: "true"
        traefik.frontend.rule: "Host:${SERVER};PathPrefixStrip:/api"
        traefik.port: 4000
    networks:
      - frontend
    ports:
      - "4000:4000" 
  visualizer:
    image: dockersamples/visualizer:stable
    deploy:
      placement:
        constraints:
          - 'node.role == manager'
      labels:
        traefik.enable: "true"
        traefik.frontend.rule: "Host:${SERVER};PathPrefixStrip:/visualizer"
        traefik.port: 8001
    networks:
      - frontend
    ports: 
      - "8001:8080"
    stop_grace_period: 1m30s
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
networks:
  frontend:
    external: true

相关配置适用于octeditor服务:

  octeditor:
    image: ${DOCKER_OCTEDITOR_IMAGE_TAG}
    deploy:
      replicas: 1
      labels:
        traefik.enable: "true"
        traefik.frontend.rule: "Host:${SERVER}"
        traefik.port: 3000
    networks:
      - frontend
    ports:
      - "3000:80"

我正在将端口80(默认为Nginx监听)映射到端口3000,其中Traefik被配置为定位此服务。这是运行Nginx的服务的Dockerfile

FROM node:latest as builder

WORKDIR /usr/src/app

COPY package.json . 

RUN npm install

COPY . .

RUN npm run build

FROM nginx

COPY --from=builder /usr/src/app/build /usr/share/nginx/html

我只是构建一个react app并将构建文件夹复制到/usr/share/nginx/html文件夹。我尝试将Dockerfile作为一个独立的容器构建和运行,并且它可以正常工作,并且我已经检查了html文件夹的内容,并且看起来一切正确。除visualizer服务外,其他服务均正常运行。仅此octedtior服务和visualizer服务给了我502错误。谁能提出解决方案,甚至如何检查发送到Nginx容器的流量?我已经尝试过docker ps servicename,但是看不到来自该服务的任何错误。

编辑:

如果我将octeditor的配置更改为此:

  octeditor:
    image: ${DOCKER_OCTEDITOR_IMAGE_TAG}
    deploy:
      replicas: 1
      labels:
        traefik.enable: "true"
        traefik.frontend.rule: "Host:${SERVER}"
        traefik.port: 80
    networks:
      - frontend
    ports:
      - "80:80"

并删除以前在端口test上监听的80服务,它似乎可以正常工作。我不知道以前的配置有什么问题吗?我以前以为是将流量从端口3000映射到容器的端口80,而现在是从端口80映射到80,但是从容器的角度来看应该没有什么改变了,对吧?

1 个答案:

答案 0 :(得分:1)

  

相关配置适用于octeditor服务:

octeditor:
    image: ${DOCKER_OCTEDITOR_IMAGE_TAG}
    deploy:
      replicas: 1
      labels:
        traefik.enable: "true"
        traefik.frontend.rule: "Host:${SERVER}"
        traefik.port: 3000
    networks:
      - frontend
    ports:
      - "3000:80"
     

我正在将端口80(Nginx默认监听)映射到端口3000   Traefik配置为可找到此服务的位置。

traefik端口需要为80,而不是3000。端口映射将创建从3000上的主机到80上的容器的转发。但是traefik通过共享网络(frontend直接与容器对话并且您需要为其提供容器端口。

除非您需要在没有代理的情况下直接访问它们,否则无需为通过traefik或任何其他反向代理访问的服务发布主机端口(这使您在这些情况下是否需要反向代理存在疑问)。换句话说,这可以在没有端口的情况下编写:

  octeditor:
    image: ${DOCKER_OCTEDITOR_IMAGE_TAG}
    deploy:
      replicas: 1
      labels:
        traefik.enable: "true"
        traefik.frontend.rule: "Host:${SERVER}"
        traefik.port: 80
    networks:
      - frontend