目 录CONTENT

文章目录

正确使用Docker部署Traefik和演示middleware使用

Administrator
2025-09-26 / 0 评论 / 0 点赞 / 0 阅读 / 0 字

 

字数 511,阅读大约需 3 分钟

正确使用Docker部署Traefik和演示middleware使用

什么是traefik

Traefik(发音为traffic)是一款现代化的HTTP反向代理和负载均衡器,能轻松部署微服务。Traefik可与您现有的基础设施组件(Docker、Swarm模式、Kubernetes、Consul、Etcd、Rancher v2、Amazon ECS等)集成,并能自动且动态地进行自我配置。

Github:traefik/traefik: The Cloud Native Application Proxy[1]
官网:Run APIs Easily. Anywhere. | Traefik Labs[2]

docker部署traefik

version: "3"

services:
  traefik:
    image: docker.1ms.run/traefik:v3.0
    restart: always
    ports:
      - 8083:80
      - 83:8080
    command:
      - "--api=true"
      # - "--api.dashboard=true"
      - "--api.insecure=true"
      - "--ping=true"
      - "--entrypoints.http.address=:80"
      # - "--entrypoints.web.address=:80"
      - "--providers.docker=true"
      - "--providers.docker.endpoint=unix:///var/run/docker.sock"
    labels:
      - "traefik.http.middlewares.gzip.compress=true"
      - "traefik.http.routers.traefik-dashboard.middlewares=gzip@docker"
      - "traefik.http.routers.traefik-dashboard-api.middlewares=gzip@docker"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./traefik/traefik.yaml:/etc/traefik/traefik.yaml
      - ./traefik/routes:/etc/traefik/routes
    healthcheck:
      test: ["CMD-SHELL", "wget -q --spider --proxy off localhost:8080/ping || exit 1"]
      interval: 3s
      retries: 10
    logging:
      driver: "json-file"
      options:
        max-size: "1m"
    networks:
      - platform-common-net
    hostname: traefik.local

  # whoami:
  #   # 验证traefik服务
  #   image: "traefik/whoami"
  #   labels:
  #     - "traefik.enable=true"
  #     - "traefik.http.routers.whoami.rule=Host(`whoami.localhost`)"
  #     - "traefik.http.routers.whoami.entrypoints=http"
  #   networks:
  #     - platform-common-net
  #   hostname: whoami.local

networks:
  platform-common-net:
    external: true

插件使用

The Traefik plugin architecture makes it easy for developers to create new plugins, modify existing ones, and share plugins with the Traefik community.

traefik提供2种方式的插件:Plugin Catalog有大量现成插件 + 允许用户开发自定义的插件(本次仅演示在docker环境里如何使用现成插件)

traefik.yaml

# yaml-language-server: $schema=https://json.schemastore.org/traefik-v3.json
global:
  checkNewVersion: false
  sendAnonymousUsage: false
api:
  dashboard: true
  insecure: true # enable dashboard on port 8080
  debug: true
  disableDashboardAd: true
ping: {}
log:
  level: INFO
  # format: common
  noColor: true
  # filePath: foobar
  # maxSize: 42
  # maxAge: 42
  # maxBackups: 42
  # compress: true
accessLog:
  # Enables accessLogs for internal resources (e.g.: ping@internal)
  # addInternals: false
  fields:
    defaultMode: keep
    # names:
      # name0: keep
      # name1: drop
    headers:
      defaultMode: drop
      names:
        "User-Agent": keep
        "Authorization": redact
        "Content-Type": keep
providers:
  docker:
    endpoint: unix:///var/run/docker.sock
  file:
    directory: /etc/traefik/routes

entryPoints:
  http:
    address: ":80"
    http:
      middlewares:
        # - http-compress@file
        - traceid@file  # 明确指定使用 file 提供者

experimental:
  plugins:
    traceid:
      modulename: "github.com/trinnylondon/traefik-add-trace-id"
      version: "v0.1.3"

middleware.yaml

# /etc/traefik/routes/middlewares.yml
http:
  middlewares:
  # Traefik 中间件配置中的一个中间件名称
    traceid:
      plugin:
        traceid:
          headerPrefix: ""
          headerName: "X-Trace-Id-traefik"

启用中间件

在traefik.yaml的配置里可自定义配置,下面演示的全局http生效

entryPoints:
  http:
    address: ":80"
    http:
      middlewares:
        # - http-compress@file
        - traceid@file  # 明确指定使用 file 提供者

基本演示

引用链接

[1] traefik/traefik: The Cloud Native Application Proxy: https://github.com/traefik/traefik
[2] Run APIs Easily. Anywhere. | Traefik Labs: https://traefik.io/

 

0
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区