Traefik 2:让我们加密和重定向 HTTPS

本文遵循以下文章:

  • Traefik 2:概念

  • Traefik 2:贡献和调试

需要

我需要 :

  • 发布 HTTPS 站点

  • 有一个有效的证书

  • 在 HTTPS 上重定向 HTTP 流。

让我们加密

所以让我们添加Let's encrypt作为 Traefik 的certificatesResolvers

为此,我们需要:

  • Let's encrypt的联系电子邮件地址

  • 选择证书存储的格式和文件

  • 选择Let's encrypt用于创建和更新的挑战模式。

就我而言,我使用了与 Traefik V1 相同的acme.json文件。如果您从头开始,您可以创建一个空白。

对于挑战模式,我更喜欢 HTTP,因为它允许我从 Traefik 做所有事情。

现在我可以将我的块添加到traefik.yaml文件中

certificatesResolvers:
  letsencrypt:
    acme:
      email: "jade.kharats@example.tld"
      storage: acme.json
      httpChallenge:
        entryPoint: http

为了在我的 Traefik 实例之间共享这个文件,我需要在stack-traefik.yml中添加它

services:
  traefik:
    volumes:
      - /swarm/traefik2/acme.json:/acme.json

从现在开始,Traefik 将为请求的域生成证书。

让我们去认证我们的仪表板。

TLS

我将在entryPointhttps 上添加一个router。在这个router上,我将启用 TLS 并告诉它使用Let's encrypt作为解析器。

它发生在stack-traefik.yml

services:
  traefik:
    deploy:
      labels:
        - "traefik.http.routers.traefik-router.entrypoints=https"
        - "traefik.http.routers.traefik-router.rule=Host(`traefik.example.tld`)"
        - "traefik.http.routers.traefik-router.tls=true"
        - "traefik.http.routers.traefik-router.tls.certresolver=letsencrypt"

部署新版本后,我在 https 中使用有效证书为我的域提供服务。很简单,不是吗?

HTTPS 重定向

为什么在我们的 HTTPS 可用时继续允许 HTTP 访问?我们将要重定向流。

Traefik 2 为此提供了一个中间件。

另一个标签放在我们的stack-traefik.yml

services:
  traefik:
    deploy:
      labels:
        - "traefik.http.routers.traefik-router0.entrypoints=http"
        - "traefik.http.routers.traefik-router0.rule=Host(`traefik.example.tld`)"
        - "traefik.http.routers.traefik-router0.middlewares=traefik-redirectscheme,auth"
        - "traefik.http.middlewares.traefik-redirectscheme.redirectscheme.scheme=https"

所以我们添加了"traefik.http.middlewares.traefik-redirectscheme.redirectscheme.scheme=https"来定义重定向。

我将此中间件称为traefik-redirectscheme,但我冒着将其用于 Traefik 之外的其他routers的风险。我会在这种情况下更改名称。

然后我将此中间件添加到我的router0的中间件列表中

瞧。

结论

使用 Traefik 2 可以轻松管理 HTTPS 和 TLS 部分。

第二个版本的一大优点是增加了 TCP 管理。

在接下来的文章中,我将设置一个带有 HTTPS 路由器和 TCP 路由器的 GITEA 服务器。

traefik.yaml

global:
  checkNewVersion: true
  sendAnonymousUsage: true
api:
  dashboard: true
  debug: true
entryPoints:
  ssh:
    address: ":22"
  http:
    address: ":80"
  https:
    address: ":443"
providers:
  docker:
    watch: true
    swarmMode: true
    useBindPortIP: true
    endpoint: "unix:///var/run/docker.sock"
certificatesResolvers:
  letsencrypt:
    acme:
      email: "jade.kharats@example.tld"
      storage: acme.json
      httpChallenge:
        entryPoint: http
log:
  filePath: "log/traefik.log"
  level: WARN
accessLog:
  filePath: "log/access.log"
  bufferingSize: 100

堆栈traefik.yml

version: "3.3"

networks:
  traefik-net:
    external: true

configs:
  traefik.yaml:
    file: ./traefik.yaml


services:
  traefik:
    image: traefik:v2.0
    ports:
      - 80:80
      - 443:443
      - 22:22
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - /swarm/traefik2/acme.json:/acme.json
      - /swarm/traefik2/log:/log
    configs:
      - source: traefik.yaml
        target: /etc/traefik/traefik.yaml

    networks:
      - traefik-net
    deploy:
      mode: global
      placement:
        constraints: [node.role == manager]
      labels:
        - "traefik.docker.network=traefik-net"
        - "traefik.http.routers.traefik-router0.entrypoints=http"
        - "traefik.http.routers.traefik-router0.rule=Host(`traefik.example.tld`)"
        - "traefik.http.routers.traefik-router0.middlewares=traefik-redirectscheme,auth"
        - "traefik.http.routers.traefik-router.entrypoints=https"
        - "traefik.http.routers.traefik-router.rule=Host(`traefik.example.tld`)"
        - "traefik.http.routers.traefik-router.tls=true"
        - "traefik.http.routers.traefik-router.tls.certresolver=letsencrypt"
        - "traefik.http.routers.traefik-router.middlewares=auth"
        - "traefik.http.middlewares.traefik-redirectscheme.redirectscheme.scheme=https"
        - "traefik.http.middlewares.auth.basicauth.users=jade:$$2y$$..."
        - "traefik.http.services.traefik-service.loadbalancer.server.port=8080"
点击阅读全文
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐