Traefik为服务添加HTTPS支持

背景

  • 紧接着Traefik学习这篇文章,尝试使用Let's Encrypt为Traefik的服务添加HTTPS支持

Let's Encrypt

  • Let’s Encrypt 是一家免费、开放、自动化的证书颁发机构(CA),为公众的利益而运行。它是一项由 Internet Security Research Group(ISRG)提供的服务。以尽可能对用户友好的方式免费提供为网站启用 HTTPS(SSL/TLS)所需的数字证书。

Traefik的支持

  • 创建acme.json文件用来存储证书信息

    touch acme.json
    chmod 600 acme.json
    • 注意务必设置权限为600,否则会报错
  • traefik Docker Compose配置文件 traefik.yaml:

    version: '3'
    services:
      reverse-proxy:
        image: traefik
        restart: always
        ports:
          - "80:80"
          - "443:443"
        networks:
          - traefik
        volumes:
          - ./traefik.toml:/etc/traefik/traefik.toml
          - /var/run/docker.sock:/var/run/docker.sock
          - ./config/:/etc/traefik/config/:ro
          - ./acme.json:/letsencrypt/acme.json
        container_name: traefik
        # 网关健康检查
        healthcheck:
          test: ["CMD-SHELL", "wget -q --spider --proxy off localhost:8080/ping || exit 1"]
          interval: 3s
          timeout: 5s
    # 创建外部网卡 docker network create traefik
    networks:
      traefik:
        external: true
    • 关键部分是使用volume向容器引入acme.json
  • traefik静态配置文件 traefik.toml

    # Let's Encrypt
    [certificatesResolvers.myresolver.acme]
      email = "example.com"
      storage="/letsencrypt/acme.json"
      [certificatesResolvers.myresolver.acme.tlsChallenge]
  • 为服务添加HTTPS支持,这里还是以Halo博客为例

    version: '3.1'
    
    services:
    
      halo:
        image: halohub/halo
        container_name: halo
        restart: unless-stopped
        volumes:
          - /root/blog/halo:/root/.halo 
        labels:
          - traefik.http.routers.halo.rule=Host(`blog.demoli.xyz`)
          - traefik.http.routers.halo.tls=true
          - traefik.http.routers.halo.tls.certResolver=myresolver
          - traefik.http.routers.halo.entrypoints=https
          - traefik.http.services.halo.loadbalancer.server.port=8090
          - traefik.http.routers.halo-redirect-https.rule=Host(`blog.demoli.xyz`)
          - traefik.http.routers.halo-redirect-https.entrypoints=http
          - traefik.http.routers.halo-redirect-https.service=noop@file
          - traefik.http.routers.halo-redirect-https.middlewares=https-redirect@file
          - traefik.http.routers.halo-redirect-https.priority=100
    networks:
      default:
        external:
          name: traefik
    • 关键配置是traefik.http.routers.halo.tls=truetraefik.http.routers.halo.tls.certResolver=myresolver

注意事项

参考


demoli
16 声望0 粉丝

bug创建者