在 Traefik Proxy 2.5 中使用/开发私有插件

开发 开发工具
随着 Traefik Proxy v2.5 的发布,有一种新方法可以直接从本地存储加载插件(并且无需启用 Traefik Pilot)。只需将您的插件源代码放入一个名为 /plugins-local​ 的新目录中。

Traefik Proxy 在设计上是一个模块化路由器,允许您将中间件放入您的路由中,并在请求到达预期的后端服务目的地之前对其进行修改。Traefik 内置了许多这样的中间件,还允许您以插件的形式加载自己的中间件。

  • https://doc.traefik.io/traefik/middlewares/overview/

查找和安装中间件插件的最简单方法是通过 Traefik Pilot。Traefik Pilot 是一个软件即服务 (SaaS) 平台,它为您的所有 Traefik 代理实例提供全球指标和警报系统,并具有免费使用的内置插件商店。在商店内,您可以浏览所有可用的开源插件,然后单击按钮进行安装。

  • https://doc.traefik.io/traefik-pilot/
  • https://pilot.traefik.io/plugins

随着 Traefik Proxy v2.5 的发布,有一种新方法可以直接从本地存储加载插件(并且无需启用 Traefik Pilot)。只需将您的插件源代码放入一个名为 /plugins-local 的新目录中。(您将相对于当前工作目录 [从您调用 traefik 的位置] 创建此目录,如果您使用的是 traefik docker 映像,则入口点始终是根目录 /。) Traefik Proxy 本身将负责构建(解释 ) 你的插件,所以你所要做的就是编写源代码,并在正确的目录中提供它以便 Traefik Proxy 加载它。插件每次启动仅加载一次(即,每次您希望重新加载插件源代码时都必须重新启动 traefik)。

  • https://github.com/traefik/traefik/pull/8224

在以下场景中,您将找到使用 Traefik Proxy v2.5 编写自己的 Docker 容器镜像并将插件源代码捆绑到该镜像的 /plugins-local 目录中的示例。在使用 Docker 在开发环境中测试您的插件之后(并且可能在为其创建持续集成构建之后),您可以将此镜像推送到容器 registry,并在生产 Docker 服务器和/或 Kubernetes 集群中引用此镜像。您可以将镜像保密,也可以将其发布并在任何地方共享您的插件。

构建 Traefik Proxy 容器镜像并捆绑 demo 插件

这是一个示例 Dockerfile,它重新混合了标准 traefik:v2.5 docker 映像,并添加了一个从可配置的 git 存储库自动克隆的插件。

在某个地方创建一个临时目录,并在其中创建一个名为 Dockerfile.demo 的新文件:​

# Dockerfile.demo - Example for Traefik Proxy and a demo plugin from git:FROM alpine:3ARG PLUGIN_MODULE=github.com/traefik/plugindemoARG PLUGIN_GIT_REPO=https://github.com/traefik/plugindemo.gitARG PLUGIN_GIT_BRANCH=masterRUN apk add --update git && \
git clone ${PLUGIN_GIT_REPO} /plugins-local/src/${PLUGIN_MODULE} \
--depth 1 --single-branch --branch ${PLUGIN_GIT_BRANCH}FROM traefik:v2.5COPY --from=0 /plugins-local /plugins-local

默认构建参数加载 Traefik Labs 发布的示例插件 demo,它本质上是内置 headers.customRequestHeaders 中间件的克隆,但作为插件。

  • https://github.com/traefik/plugindemo
  • https://doc.traefik.io/traefik/middlewares/headers/#customrequestheaders

在与 Dockerfile.demo 相同的目录中,构建镜像:

docker build -f Dockerfile.demo --tag traefik-with-demo-plugin .

您现在刚刚构建了一个 docker 镜像,其中包含 Traefik v2.5 和演示插件。您现在可以运行镜像来测试它:

docker run --rm -it traefik-with-demo-plugin \
--log.level=DEBUG \
--experimental.localPlugins.demo.moduleName=github.com/traefik/plugindemo

日志将打印显示插件已加载且 Traefik 代理将运行的配置。你可以肯定地测试一下,按 Ctrl-C 停止容器,然后重新运行将 moduleName= 更改为 github.com/something/different 的命令,你会得到一个错误,说它不存在并立即退出。

 使用您的自定义插件构建 Traefik Proxy 容器镜像

要创建您自己设计的新插件,请分叉此演示存储库。(要直接在 GitHub 上执行此操作,您可以单击标有 Use this template 的绿色按钮,或者您可以将存储库克隆到另一台服务器)。您可以选择将此新存储库设为公共或私有,但说明会有所不同,具体取决于它是否需要身份验证才能克隆它,因此将分别介绍每种情况。

  • https://github.com/traefik/plugindemo

将您的分叉存储库克隆到您的工作站,并阅读 readme.md 文件中的开发说明。创建您的插件代码,更新 .traefik.yml 中的 import 行以匹配您的存储库名称,将更改提交到 git,然后将更改推送回您的 git 服务器 (GitHub)。如果您只想测试示例插件代码,则无需提交任何更改。此外,Traefik 不需要编译插件源代码:插件通过原始源代码加载,并在运行时由 Yaegi 解释。

  • https://github.com/traefik/plugindemo/blob/master/readme.md
  • https://github.com/traefik/yaegi

从公共存储库构建镜像

如果您将存储库公开,则构建镜像很容易。打开您的 shell 终端,并创建这些临时环境变量以用作构建参数:

## Create temporary variables for your plugin and git repository details:## Optionally save these to build-env.sh and run "source build-env.sh" after.export DOCKER_IMAGE=traefik-with-my-pluginexport PLUGIN_MODULE=github.com/YOUR_NAME/YOUR_REPOSITORYexport PLUGIN_GIT_REPO=https://github.com/YOUR_NAME/YOUR_REPOSITORY.gitexport PLUGIN_GIT_BRANCH=master

更改这些变量以适合您的分叉插件存储库:

  • DOCKER_IMAGE 是你的新 Docker 镜像的 tag,它将捆绑 Traefik 和你的插件代码。
  • PLUGIN_MODULE 是插件的 Go 模块的名称(例如 github.com/traefik/plugindemo)。使用您自己的服务器、组织和分叉存储库名称。
  • PLUGIN_GIT_REPO 是插件存储库中心的完整 git clone URL。(此示例假设使用了公共存储库,并且不需要身份验证,否则请参阅下一节。)
  • PLUGIN_GIT_BRANCH 是您希望克隆和安装的 git 分支名称。

在克隆存储库的根目录中,创建一个名为 Dockerfile.public 的新文件:

## Dockerfile.public - Bundle a Traefik plugin from a public git repositoryFROM alpine:3ARG PLUGIN_MODULE=github.com/traefik/plugindemoARG PLUGIN_GIT_REPO=https://github.com/traefik/plugindemo.gitARG PLUGIN_GIT_BRANCH=masterRUN apk update && \
apk add git && \
git clone ${PLUGIN_GIT_REPO} /plugins-local/src/${PLUGIN_MODULE} \
--depth 1 --single-branch --branch ${PLUGIN_GIT_BRANCH}FROM traefik:v2.5COPY --from=0 /plugins-local /plugins-local

构建并标记镜像,从环境中传递参数:

docker build -f Dockerfile.public \
--tag ${DOCKER_IMAGE} \
--build-arg PLUGIN_MODULE \
--build-arg PLUGIN_GIT_REPO \
--build-arg PLUGIN_GIT_BRANCH .

 从私有 git 存储库构建镜像

从私有 git 存储库构建镜像更具挑战性,因为您需要将 SSH 凭据传递到 Docker 构建过程,以便按照 Dockerfile 中的脚本从私有 git 存储库进行克隆。

您需要将 Docker 安装更新到版本 >=18.09,这允许在 docker 镜像构建过程中加载与 ssh-agent 通信和临时使用工作站用户帐户的 SSH 密钥所需的实验性 BuildKit 增强功能。

  • https://docs.docker.com/develop/develop-images/build_enhancements

在你的 shell 中设置这些环境变量:

## Optionally save these to build-env.sh and run "source build-env.sh" after.## Docker BuildKit is required for ssh-agent forwarding:export DOCKER_BUILDKIT=1## Edit these variables for your plugin and git repository:export DOCKER_IMAGE=traefik-with-my-pluginexport PLUGIN_MODULE=github.com/YOUR_NAME/YOUR_REPOSITORYexport PLUGIN_GIT_REPO=git@github.com:YOUR_NAME/YOUR_REPOSITORY.gitexport PLUGIN_GIT_BRANCH=master

主机 ssh-agent 直通需要修改 Dockerfile。创建一个名为 Dockerfile.private 的新文件:

# syntax=docker/dockerfile:1.0.0-experimental# The above line is required to turn on experimental BuildKit features.# Dockerfile.private - Build Traefik and plugin from a private git repository.# Loads SSH keys from the host `ssh-agent` to allow git clone.FROM alpine:3# Clone your plugin git repositories:ARG PLUGIN_MODULE=github.com/traefik/plugindemo
ARG PLUGIN_GIT_REPO=git@github.com:traefik/plugindemo.git
ARG PLUGIN_GIT_BRANCH=master
RUN apk add --update git openssh && \
mkdir -m 700 /root/.ssh && \
touch -m 600 /root/.ssh/known_hosts && \
ssh-keyscan github.com > /root/.ssh/known_hosts
RUN --mount=type=ssh git clone \
--depth 1 --single-branch --branch ${PLUGIN_GIT_BRANCH} \ ${PLUGIN_GIT_REPO} /plugins-local/src/${PLUGIN_MODULE}
FROM traefik:v2.5
COPY --from=0 /plugins-local /plugins-local

使用额外的 --ssh default 选项构建镜像。这将通过连接到运行 ssh-agent 的主机连接到构建过程,以便您可以在构建过程中使用 SSH 密钥,并克隆私有 git 存储库:

docker build -f Dockerfile.private \
--ssh default --tag ${DOCKER_IMAGE} \
--build-arg PLUGIN_MODULE \
--build-arg PLUGIN_GIT_REPO \
--build-arg PLUGIN_GIT_BRANCH .

注意:由于 docker-compose 中存在一个未解决的问题,您目前无法在 docker-compose 中使用 --ssh 参数(并且与 ssh-agent 的连接将失败),因此如果您想使用此修改后的 Dockerfile 以及 docker-compose,您必须首先使用上面列出的 docker build 命令手动构建容器映像。如果您首先以这种方式构建映像,则 docker-compose 可以依赖构建缓存或显式镜像名称,而无需再次构建它。

  • https://github.com/docker/compose/issues/7025

使用 docker-compose 作为插件开发环境

你可以使用 docker-compose 作为一个简单的插件开发环境。

  • https://docs.docker.com/compose/

将您的插件存储库克隆到您的工作站,然后将这些新文件创建到存储库的根目录中:

创建 Dockerfile:

FROM traefik:v2.5## Default module name (put your setting in .env to override)ARG PLUGIN_MODULE=github.com/traefik/plugindemo
ADD . /plugins-local/src/${PLUGIN_MODULE}

创建 .env 设置文件:

## Traefik Proxy local plugin .env file## Configure your plugin name:PLUGIN_NAME=demo## Configure your module namespace:PLUGIN_MODULE=github.com/traefik/plugindemo## Configure whoami domain name for route testing:WHOAMI_TRAEFIK_HOST=whoami.example.com## Configure Email address for Let's Encrypt:## Uncomment and configure this for production only:# ACME_CA_EMAIL=you@example.com

创建 docker-compose.yaml:

# docker-compose.yaml for Traefik Proxy local plugin developmentversion: "3.3" networks: traefik-proxy: volumes: traefik-proxy:

services:
traefik-proxy:
build:
context: .
args:
PLUGIN_MODULE: ${PLUGIN_MODULE}
restart: unless-stopped
networks:
- traefik-proxy
security_opt:
- no-new-privileges:true
command: #- "--log.level=DEBUG"
- "--providers.docker=true"
- "--providers.docker.exposedbydefault=false"
- "--providers.docker.network=traefik-proxy"
## Entrypoints:
- "--entrypoints.web.address=:80"
- "--entrypoints.websecure.address=:443"
- "--entrypoints.traefik.address=:9000"
## Automatically redirect HTTP to HTTPS
- "--entrypoints.web.http.redirections.entryPoint.to=websecure"
## ACME TLS config:
- "--certificatesresolvers.default.acme.storage=/data/acme.json"
## Uncomment for production TLS certificates (Let's Encrypt):
# - "--certificatesresolvers.default.acme.tlschallenge=true"
# - "--certificatesresolvers.default.acme.caserver=https://acme-v02.api.letsencrypt.org/directory"
# - "--certificatesresolvers.default.acme.email=${ACME_CA_EMAIL}"
## Enable Dashboard available only from the docker localhost:9000
- "--api.dashboard=true"
- "--api.insecure=true"
## Enable local plugins:
- "--experimental.localPlugins.${PLUGIN_NAME}.moduleName=${PLUGIN_MODULE}"
ports:
- "80:80"
- "443:443"
- "127.0.0.1:9000:9000"
volumes:
- "traefik-proxy:/data"
- "/var/run/docker.sock:/var/run/docker.sock:ro"

## The whoami container will run the demo plugin for testing purposes:
whoami:
image: traefik/whoami
networks:
- traefik-proxy
restart: unless-stopped
labels:
- "traefik.enable=true"
- "traefik.http.routers.whoami.rule=Host(`${WHOAMI_TRAEFIK_HOST}`)"
- "traefik.http.routers.whoami.entrypoints=websecure"
# Configure the plugin as a new middleware:
- "traefik.http.routers.whoami.middlewares=whoami-demo"
# Add a test header to all incoming requests:
# (the presense of this header in whoami response shows if the plugin works:)
- "traefik.http.middlewares.whoami-demo.plugin.${PLUGIN_NAME}.headers.DoesPluginWork=YES"
- "traefik.http.routers.whoami.tls.certresolver=default"

创建 .dockerignore 以从镜像构建中排除 .git 目录:

# .dockerignore file exludes files from the image:.git

构建镜像并启动测试实例:

docker-compose up

编辑您的 /etc/hosts 文件(或您的本地 DNS 服务器)并添加 whoami 路由域:

curl -k https://whoami.example.com

使用 curl 测试您的 DNS 是否正常工作,以及插件是否已生效(使用与您为 WHOAMI_TRAEFIK_HOST 和 /etc/hosts 配置的域名相同的域名):

curl -k https://whoami.example.com

您应该得到 whoami 响应,并在输出中显示此测试头:

Doespluginwork: YES

这是插件配置为注入请求的相同头和值,并从 whoami 回显。如果你看到它,你就知道你的插件配置成功了。

 为常规开发工作配置本地 DNS 服务

当你需要测试大量不同的子域和 Traefik Proxy Host 路由器规则时,一个更好的 DNS 解决方案,而不是不断编辑你的 /etc/hosts 文件,是在你的工作站上运行 dnsmasq 作为本地 DNS 服务器,它会响应到通配符 DNS A 记录查询,用于整个根域或子域名。

dnsmasq 的配置是可选的,是对 /etc/hosts 文件的补充。dnsmasq 的安装说明取决于您的操作系统,但可以从大多数包管理器中获得。 dnsmasq 将使您的开发工作更加顺畅,并且是清理 /etc/hosts 文件的好方法。这是一个示例 /etc/dnsmasq.conf 配置文件,用于设置具有通配符域的本地 DNS 服务。您还需要按照注释中的说明编辑您的 /etc/resolv.conf:

# /etc/dnsmasq.conf# Use this if you are tired of editing your /etc/hosts file.# This is a local DNS service bound only to the looback device on localhost.# To use this requires an /etc/resolv.conf file # with a single line (no leading space): nameserver 127.0.0.1# To prevent any changes to the host DNS config,# run: sudo chattr +i /etc/resolv.conf#      (now all of your DNS queries will go through dnsmasq)interface=lo
listen-address=::1,127.0.0.1
bind-interfaces
cache-size=1000# Use cloudflare upstream DNS servers:server=1.1.1.1
server=1.0.0.1# Example wildcard domain names# All *.example.com names point to a single docker server IP address:address=/example.com/127.0.0.1# dnsmasq also loads your /etc/hosts file, so those host names still work.

检查您的操作系统说明以启用 dnsmasq 服务,但通常使用 Systemd:

sudo systemctl enable --now dnsmasq.service

编辑 /etc/resolv.conf 以将 dnsmasq 服务器用于所有系统 DNS 查询:

domain your.domain.example.com
search domain.example.com
nameserver 127.0.0.1

有时其他服务(systemd-resolved)想要覆盖这个文件,你可以通过在文件上应用不可变标志来防止这种情况:

# This prevents editing the file, use -i to re-enable editing:chattr +i /etc/resolv.conf

您可以使用 dig、drill 或 nslookup 实用程序测试 DNS 服务器是否处于活动状态:

# dig or drill:dig test.example.com | grep -A1 "ANSWER SECTION"# or nslookup:nslookup test.example.com

任何这些工具的输出都应该报告您的 docker 主机的正确 IP 地址,现在您可以在 Traefik 代理路由中使用您想要的任何子域。

引用

  • https://traefik.io/blog/using-private-plugins-in-traefik-proxy-2-5/
责任编辑:武晓燕 来源: 黑客下午茶
相关推荐

2022-01-06 07:46:01

Traefik 开源Gateway API

2012-04-19 12:58:26

TitaniumJSS

2018-03-26 14:25:55

KubernetesSkaffold命令

2017-06-07 11:57:26

混合云OpenStack容器

2011-08-16 10:35:59

Objective_C私有方法

2009-07-16 14:22:02

Windows Emb

2023-12-22 09:11:45

AndroidNFC移动开发

2021-12-28 15:38:46

Traefik中间件插件

2012-02-13 14:22:22

MonoTouchiOS应用Visual Stud

2020-07-07 09:19:28

Android 协程开发

2020-04-08 09:06:34

Android 协程开发

2011-06-23 10:39:43

ibmdw虚拟化敏捷开发

2020-04-23 09:33:32

Android 协程开发

2023-04-12 15:25:09

Bytrace鸿蒙

2013-12-13 17:21:14

Lua脚本语言

2009-06-25 16:49:24

Hibernate

2023-11-17 12:04:39

GORM并发

2021-10-13 16:00:53

KubeProxyIptables

2010-10-18 13:16:24

GalleryAndroid

2020-04-08 13:05:03

TraefikKubernetes树莓派
点赞
收藏

51CTO技术栈公众号