traefik

package module
v0.0.0-...-0335dd1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2026 License: Apache-2.0 Imports: 24 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

coredns traefik plugin

Name

traefik - returns CNAMEs for hosts registered in Traefik instances

Description

Extracts FQDN's from the Host() and HostSNI() values in traefik's http router rules (Traefik's /api/http/routers endpoint). Returns a CNAME result with the traefik instance's domain.

Why?

Homelab + free time. Prior to using traefik, I had a few nginx containers running in different vlans in my homelab. I would create config files by hand, setup static DNS entries, generate certs, and reverse proxy to docker containers by hand.

Traefik took care of a lot of that for me, but I still needed to setup the static DNS entries. Basically I would have a single IP for traefik and a bunch of CNAMEs to it for the various containerized services. Rather than go into pfSense to do this every time, I figured I'd find something more automatic. So I spun up CoreDNS and threw this plugin together to poll the Traefik API periodically and figure out what host names I have http routers referring to. For each of those, I can respond with a CNAME to the Traefik server on-the-fly.

When you pair this with Traefik's ability to configure routes and services via Docker labels, this completely automates the DNS, TLS, and reverse proxy pain when spinning up new containers. It's a nice stopgap until I get around to making the switch to kubernetes & should play nicely during the transition period.

Compilation

This package will always be compiled as part of CoreDNS and not in a standalone way. It will require you to use go get or as a dependency on plugin.cfg.

The manual will have more information about how to configure and extend the server with external plugins.

A simple way to consume this plugin, is by adding the following on plugin.cfg, and recompile it as detailed on coredns.io.

traefik:github.com/scottt732/coredns-traefik

Put this early in the plugin list, so that traefik is executed before any of the other plugins.

After this you can compile coredns by:

go generate
go build

Or you can instead use make:

make

Syntax

. {
  traefik_webhook_listener 127.0.0.1:9154

  traefik https://your-traefik.homelab.net/api {
    bearerToken {$TRAEFIK_API_TOKEN}
    cname your-traefik.homelab.net
    refreshInterval 30
    triggerDebounce 1s
    trigger docker endpoint unix:///var/run/docker.sock delay 3s
    trigger webhook path /hooks/traefik secret change-me
    ttl 30
  }
}
  • traefik_webhook_listener configures a single process-wide listener for webhook triggers. It only needs to be declared once.
  • https://your-traefik.homelab.net/api refers to the base Traefik API endpoint (without trailing slash). Given the base URL, this will hit https://your-traefik.homelab.net/api/http/routers endpoint.
  • bearerToken sets the Authorization: Bearer ... header used when polling the Traefik API.
  • cname is the fully qualified domain name (with or without trailing .) that matching requests will have returned. Usually this will be the host name of the API endpoint above.
  • refreshInterval specifies how frequently that api/http/routers endpoint is polled for changes (in seconds).
  • triggerDebounce specifies how long trigger signals are coalesced before a refresh runs.
  • trigger docker [endpoint URI] [delay DURATION] watches Docker lifecycle, network, and health events and schedules an early refresh after a settle delay. Docker events are only hints; the plugin still re-reads Traefik's API before publishing changes.
  • trigger webhook path /PATH [secret VALUE] registers a webhook endpoint on the shared listener. Webhooks only trigger an early refresh; they do not change the source of truth.
  • ttl determines that time-to-live for successful responses (in seconds).

Trigger types are singleton per traefik block. Multiple different trigger types may be added over time, but a given block can only declare one trigger webhook ... entry.

Webhook requests must use POST. If secret is configured, callers must supply it in the X-Traefik-Trigger-Secret header.

Example

Imagine you have services in your homelab which you'd like to expose internally/externally as homelab.net subdomains.

  • Traefik is listening on 80/443 on a host whose IP is 10.10.10.2
  • The primary gateway/DNS is on 10.10.10.1
  • You want traefik.homelab.net to route to Traefik's dashboard/API.
  • You want a container named gitea to be resolvable as mytestservice.homelab.net with Traefik taking care of TLS/acme and reverse proxying.

Given this CoreDNS corefile fragment:

homelab.net:53 {
    hosts {
        10.10.10.2 traefik.homelab.net
        fallthrough
    }
    traefik https://traefik.homelab.net/api {
        cname traefik.homelab.net
        refreshInterval 30
        ttl 5
    }
    forward . dns://10.10.10.1
}

This will cause requests for traefik.homelab.net to return the A record 10.10.0.2.

Then we can use a compose.yml like this.

...
services:
  gitea:
    image: gitea/gitea:latest
    container_name: gitea
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.gitea-web.rule=Host(`gitea.homelab.net`)"
      - "traefik.http.routers.gitea-web.service=gitea-web-svc"
      - "traefik.http.routers.gitea-web.entrypoints=websecure"
      - "traefik.http.routers.gitea-web.tls=true"
      - "traefik.http.routers.gitea-web.tls.domains[0].main=homelab.net"
      - "traefik.http.routers.gitea-web.tls.domains[0].sans=*.homelab.net"
      - "traefik.http.services.gitea-web-svc.loadbalancer.server.scheme=http"
      - "traefik.http.services.gitea-web-svc.loadbalancer.server.port=3000"
    ...

The container labels cause Traefik's routers & services to get wired together, waiting for a match on gitea.homelab.net.

This plugin polls the Traefik API and discovers gitea.homelab.net. It dynamically returns a CNAME to traefik.homelab.net, which gets resolved via the A record to 10.10.10.2. Traefik receives the request and sees the host name matches its rule.

In my case, pfSense's DNS Resolver handles DNS for my network and I setup a conditional forwarder for the zone homelab.net to point just those requests to the CoreDNS instance. Alternatively, you can send all of your DNS requests through CoreDNS.

Metrics

If monitoring is enabled (via the prometheus directive) the following metric is exported:

  • coredns_traefik_request_count_total{server} - query count to the traefik plugin.

The server label indicated which server handled the request, see the metrics plugin for details.

Ready

This plugin reports readiness to the ready plugin. It will be ready after the first refresh of data

Also See

See the manual.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DockerTrigger

type DockerTrigger struct {
	// contains filtered or unexported fields
}

func NewDockerTrigger

func NewDockerTrigger(cfg *DockerTriggerConfig, target *Traefik) *DockerTrigger

func (*DockerTrigger) Start

func (d *DockerTrigger) Start() error

func (*DockerTrigger) Stop

func (d *DockerTrigger) Stop()

type DockerTriggerConfig

type DockerTriggerConfig struct {
	Endpoint string
	Delay    time.Duration
}

type HttpRouter

type HttpRouter struct {
	EntryPoints []string       `json:"entryPoints"`
	Service     string         `json:"service"`
	Rule        string         `json:"rule"`
	Priority    int            `json:"priority"`
	Tls         *HttpRouterTls `json:"tls,omitempty"`
	Status      string         `json:"status"`
	Using       []string       `json:"using"`
	Name        string         `json:"name"`
	Provider    string         `json:"provider"`
}

type HttpRouterTls

type HttpRouterTls struct {
	Options string                `json:"options"`
	Domains []HttpRouterTlsDomain `json:"domains"`
}

type HttpRouterTlsDomain

type HttpRouterTlsDomain struct {
	Main string   `json:"main"`
	Sans []string `json:"sans"`
}

type ITraefikClient

type ITraefikClient interface {
	GetHttpRouters() (*[]HttpRouter, error)
}

type Traefik

type Traefik struct {
	Next          plugin.Handler
	Config        *TraefikConfig
	TraefikClient *TraefikClient
	// contains filtered or unexported fields
}

func (*Traefik) Name

func (t *Traefik) Name() string

func (*Traefik) Ready

func (t *Traefik) Ready() bool

func (*Traefik) ServeDNS

func (t *Traefik) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error)

func (*Traefik) String

func (t *Traefik) String() string

type TraefikClient

type TraefikClient struct {
	ITraefikClient
	// contains filtered or unexported fields
}

func NewTraefikClient

func NewTraefikClient(cfg *TraefikConfig) (*TraefikClient, error)

func (*TraefikClient) GetHttpRouters

func (c *TraefikClient) GetHttpRouters() (*[]HttpRouter, error)

type TraefikConfig

type TraefikConfig struct {
	// contains filtered or unexported fields
}

type TraefikConfigEntry

type TraefikConfigEntry struct {
	// contains filtered or unexported fields
}

type TraefikConfigEntryMap

type TraefikConfigEntryMap map[string]*TraefikConfigEntry

type WebhookManager

type WebhookManager struct {
	// contains filtered or unexported fields
}

func NewWebhookManager

func NewWebhookManager() *WebhookManager

func (*WebhookManager) Configure

func (m *WebhookManager) Configure(addr string) error

func (*WebhookManager) OnFinalShutdown

func (m *WebhookManager) OnFinalShutdown() error

func (*WebhookManager) OnStartup

func (m *WebhookManager) OnStartup() error

func (*WebhookManager) Register

func (m *WebhookManager) Register(cfg *WebhookTriggerConfig, target *Traefik) error

func (*WebhookManager) ServeHTTP

func (m *WebhookManager) ServeHTTP(w http.ResponseWriter, r *http.Request)

type WebhookTriggerConfig

type WebhookTriggerConfig struct {
	Path   string
	Secret string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址