如何在docker中为SSL添加适当的标签?

时间:2017-10-02 23:09:42

标签: ssl docker docker-compose traefik

我设法将Traefik设置为使用我的docker swarm,对于HTTP请求,它工作得很好。但是,我不知道如何为我的容器的某些设置SSL。我将使用letsencrypt生成证书。

traefik.toml(部分)

defaultEntryPoints = ["https","http"]

[entryPoints]
  [entryPoints.http]
  address = ":80"
  [entryPoints.https]
  address = ":443"
  [entryPoints.https.tls]

[acme]
email = "acme@example.com"
storage = "acme.json"
entryPoint = "https"
onHostRule = true
caServer = "https://acme-staging.api.letsencrypt.org/directory"

搬运工-compose.yml

version: '3'
services:
  web:
    ...
    deploy:
      labels:
        - "traefik.enable=true"
        - "traefik.frontend.rule=Host:example.com,www.example.com"
        - "traefik.docker.network=public"
        - "traefik.frontend.entryPoints=http"
        - "traefik.backend=service_web"

在此配置中,我的应用程序永远不会到达SSL,因为我的容器没有SSL入口点设置。如果我将“traefik.frontend.entryPoints”更改为“https”,则会调用Letsencrypt(由于暂存而导致LE givges错误,但此时此刻对我无关紧要。)

我最大的问题是,我仍然不知道如何将traefik TOML配置转换为docker-compose标签。例如,Traefik docs解释了入口点,但我有许多服务生活在不同的域下。有些人有SSL,有些人没有SSL;因此,我希望能够使用docker-compose设置http和https entryPoints,http到https重定向等。

此外,一旦我能够在docker-compose中设置入口点,我是否需要在traefik.toml中保留[entryPoints]块?

1 个答案:

答案 0 :(得分:0)

AHOI!

要求:Local-Persist卷插件: https://github.com/CWSpear/local-persist(否则必须更改卷驱动程序) 必须预先创建Traefik网络:“docker network create proxy -d overlay”

(1)Fire Up Traefik:

version: "3"

services:
  traefik:
    image: traefik
    #command: --consul --consul.endpoint=consul:8500
    #command: storeconfig --consul --consul.endpoint=consul:8500
    networks:
      - proxy
    ports:
      - 80:80
      - 443:443
      #- 8080:8080
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - traefikdata:/etc/traefik/
    deploy:
      #replicas: 3
      replicas: 1
      placement:
        constraints: [node.role == manager]
      update_config:
        parallelism: 1
        delay: 45s
        monitor: 15s
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 10
        window: 60s

volumes:
  traefikdata:
    driver: local-persist
    driver_opts:
      mountpoint: /data/docker/proxy

networks:
  proxy:
    external: true
  

重要提示:使用ACME并且想要扩展Traefik时(如此处3),您必须使用Consul或ETCD作为Config的“存储”。   如果您只使用一个Traefik实例,则不使用Consule或ETCD。    使用普通证书ETCD&从不需要领事。

(2)安装traefik.toml

logLevel = "WARN"
debug = false
defaultEntryPoints = ["http", "https"]

[entryPoints]
 [entryPoints.http]
 address = ":80"
 compress = false
   [entryPoints.http.redirect]
      entryPoint = "https"
  [entryPoints.https]
  address = ":443"
    [entryPoints.https.tls]

#Letsencrypt
[acme]
email = "admin@berndklaus.at"
storage = "traefik/acme/account"
entryPoint = "https"
onHostRule = true
onDemand = true

#[[acme.domains]]
# main = "yourdomain.at"
# sans = ["sub1.yourdomain.at", "www.yourdomain.at"]
#[[acme.domains]]
# main = "anotherdomain.at"


#[web]
#address = ":8080"

[docker]
domain = "docker.localhost"
watch = true
swarmmode = true
  

未注释的部分不是强制性的

(3)启动任何服务

version: '3'

services:
  nginx:
    image: nginx
    deploy:
      labels:
        - "traefik.port=80"
        - "traefik.docker.network=proxy"
        - "traefik.frontend.rule=Host:sub1.yourdomain.at"
        - "traefik.backend=nginx"
        - "traefik.frontend.entryPoints=http,https"
      replicas: 1
    networks:
      proxy:
        aliases:
          - nginx
    volumes:
      - html:/usr/share/nginx/html
    environment:
      - NGINX_HOST=sub.yourdomain.at
      - NGINX_PORT=80
    #command: /bin/bash -c "envsubst < /etc/nginx/conf.d/mysite.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"

networks:
  proxy:
    external: true
  default:
    driver: overlay

volumes:
  html:
    driver: local-persist
    driver_opts:
      mountpoint: /data/docker/html

更多示例:https://github.com/Berndinox/compose-v3-collection