Docker Compose Traefik 本地插件示例

问题描述 投票:0回答:1

我正在尝试在本地模式下创建 Traefik 插件的简化示例(https://plugins.traefik.io/install),但遇到了

field not found, node: myplugin
错误。这是我的
docker-compose.yml
:

services:
  traefik:
    image: traefik:latest
    ports:
      - "80:80"
      - "8080:8080"
    command:
      - --api.insecure=true
      - --providers.docker
      - --entrypoints.web.address=:80
      - --experimental.localplugins.myplugin.modulename=github.com/khpeek/my-plugin
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - ./my-plugin:/plugins-local/src/github.com/khpeek/my-plugin

  whoami:
    image: traefik/whoami:latest
    labels:
      - traefik.enable=true
      - traefik.http.routers.whoami.rule=Host(`whoami.localhost`)
      - traefik.http.routers.whoami.entrypoints=web
      - traefik.http.middlewares.my-plugin.myplugin
      - traefik.http.routers.whoami.middlewares=my-plugin@docker
      - traefik.http.middlewares.whoami-auth.forwardauth.address=http://host.docker.internal:4181
      - traefik.http.routers.whoami.middlewares=whoami-auth

其中目录结构如下:

.
├── docker-compose.yml
└── my-plugin
    ├── go.mod
    └── myplugin.go

and

myplugin.go
是一个简单的插件,添加了一个
foo
标头,其值为
bar
:

package myplugin

import (
    "context"
    "net/http"
)

type Config struct{}

func CreateConfig() *Config {
    return &Config{}
}

type MyPlugin struct {
    next http.Handler
    name string
}

func New(_ context.Context, next http.Handler, _ *Config, name string) (http.Handler, error) {
    return &MyPlugin{
        next: next,
        name: name,
    }, nil
}

func (m *MyPlugin) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    r.Header.Add("foo", "bar")
    m.next.ServeHTTP(w, r)
}

go.mod
中的模块名称与Traefik配置中的
modulename
声明相同,
github.com/khpeek/myplugin
:

module github.com/khpeek/my-plugin

go 1.23.0

当我尝试使用

docker compose up
运行此程序时,我收到
field not found
错误:

> docker compose up
[+] Running 2/0
 ✔ Container traefik-docker-compose-example-traefik-1  Created                                                                                  0.0s 
 ✔ Container traefik-docker-compose-example-whoami-1   Created                                                                                  0.0s 
Attaching to traefik-1, whoami-1
whoami-1   | 2024/09/03 15:51:39 Starting up on port 80
traefik-1  | 2024-09-03T15:51:39Z ERR error="field not found, node: myplugin" container=whoami-traefik-docker-compose-example-5d092b6fcc269917c46b28d319fec17966aeac7f3d51bb370c5593d47a64e3c9 providerName=docker

根据此自述文件

当动态或静态配置中遇到未知属性时,会出现“未找到字段”错误。

检查配置文件是否格式正确的一种方法是使用以下命令进行验证:

从静态配置的 JSON 模式中,我并不清楚此配置有什么问题,它看起来与 docs 中的配置类似:

enter image description here

(即使这些文档在示例的描述中似乎有不同的模块名称)。有什么想法可以让这项工作成功吗?

docker-compose traefik traefik-middleware traefik-plugins
1个回答
0
投票

我不确定您的配置中还会存在哪些其他问题,但作为第一步,我会尝试将您的插件目录映射到您链接到的文档中指定的位置(https://plugins.traefik.io/安装)。从“本地模式”部分:

插件必须放置在 ./plugins-local 目录中,该目录应该位于运行 Traefik 二进制文件的进程的工作目录中。

由于 docker 容器使用其

root
用户来运行 traefik,请尝试将 my-plugin 目录映射更改为:

    volumes:
      - ./my-plugin:/root/.plugins-local/src/github.com/khpeek/my-plugin

不要忘记

.plugins-local
中的前导点。

© www.soinside.com 2019 - 2024. All rights reserved.