如何使用letsencrypt和多个服务配置Traefik

时间:2018-01-22 19:44:05

标签: docker traefik

我正在努力了解Traefik,但由于我缺乏知识,我不确定它是如何运作的。我想要创建以下场景

Frontend --> Static. www.example.com example.com with LE
Backend --> api.example.com LE
Redis --> Local network only
Mongodb --> Local network only.

我阅读了文档,我想出了以下docker-compose.yml文件,但我不知道它是否正确。我不确定nginx将如何映射到端口80以及traefik将如何创建LE证书。

version: '3'

services:
    redis:
      restart: always
      image: redis:alpine
      networks:
        - internal

    mongo:
      restart: always
      image: mongodb
      networks:
        - internal

    frontend:
      image: nginx:1-alpine
      command: [nginx-debug, '-g', 'daemon off; error_log /dev/stdout info;']
      volumes:
        - "./static_assets:/usr/share/nginx/html:ro"
        - "./nginx_config/default.conf:/etc/nginx/conf.d/default.conf"
      labels:
        - "traefik.enable=true"
        - "traefik.frontend.rule=PathPrefixStrip: /assets"
        - "traefik.port=80"
        - "traefik.frontend.rule=Host:example.com,www.example.com"

    api:
      image: MYAPIIMAGE
      ports:
        - "3000:3000"
      networks:
        - web
        - internal
      labels:
        - "traefik.backend=api"
        - "traefik.docker.network=web"
        - "traefik.enable=true"
        - "traefik.port=3000"
        - "traefik.frontend.rule=Host:api.example.com"
    traefik:
      image: traefik:1.4.5
      restart: always
      ports:
        - 80:80
        - 443:443
      networks:
        - web
      volumes:
        - "./acme.toml:/etc/traefik/conf/acme.toml:ro"
        - "/var/run/docker.sock:/var/run/docker.sock:ro"
        - "./acme.json:/etc/traefik/conf/acme.json:rw"
      container_name: traefik

networks:
  web:
    external:
      name: web
  internal:
    external:
      name: internal

1 个答案:

答案 0 :(得分:0)

Traefik将根据您的前端规则接收请求并将其映射到容器的端口。除非在Traefik配置中另有规定,否则traefik将始终将其端口80映射到您在traefik.port中指定的任何端口。这些是在Traefik的entrypoints.http配置中配置的。

只要您在traefik配置中将acme.OnHostRule设置为true,Traefik就会尝试获取Let的加密证书。