traefik

package module
v0.0.0-...-9be1880 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: AGPL-3.0 Imports: 10 Imported by: 0

README

Traefik Sablier Plugin

Installation

  1. Add this snippet in the Traefik Static configuration
experimental:
  plugins:
    sablier:
      moduleName: "github.com/acouvreur/sablier"
      version: "v1.6.1"
  1. Configure the plugin using the Dynamic Configuration. Example:
http:
  middlewares:
    my-sablier:
      plugin:
        sablier:
          sablierUrl: http://sablier:10000  # The sablier URL service, must be reachable from the Traefik instance
          names: whoami,nginx               # Comma separated names of containers/services/deployments etc.
          sessionDuration: 1m               # The session duration after which containers/services/deployments instances are shutdown
          # You can only use one strategy at a time
          # To do so, only declare `dynamic` or `blocking`

          # Dynamic strategy, provides the waiting webui
          dynamic:
            displayName: My Title       # (Optional) Defaults to the middleware name
            showDetails: true           # (Optional) Set to true or false to show details specifcally for this middleware, unset to use Sablier server defaults
            theme: hacker-terminal      # (Optional) The theme to use
            refreshFrequency: 5s        # (Optional) The loading page refresh frequency

          # Blocking strategy, waits until services are up and running
          # but will not wait more than `timeout`
          # blocking: 
          #   timeout: 1m

You can also checkout the End to End tests here: e2e.

Traefik with Docker classic

Container labels cannot be used

Traefik will evict the container from its pool if it's not running. Which means the middleware won't trigger on incoming request.

You must use the dynamic configuration to route to the container whatever the container state is.

Example

docker-compose.yml

version: "3.9"

services:
  traefik:
    image: traefik:2.9.1
    command:
      - --entryPoints.http.address=:80
      - --providers.docker=true
      - --providers.file.filename=/etc/traefik/dynamic-config.yml
      - --experimental.plugins.sablier.moduleName=github.com/acouvreur/sablier/plugins/traefik
      - --experimental.plugins.sablier.version=v1.6.1
    ports:
      - "8080:80"
    volumes:
      - '/var/run/docker.sock:/var/run/docker.sock'
      - './dynamic-config.yml:/etc/traefik/dynamic-config.yml'

  sablier:
    image: acouvreur/sablier:1.6.1
    command:
      - start
      - --provider.name=docker
    volumes:
      - '/var/run/docker.sock:/var/run/docker.sock'
    labels:
      - traefik.enable=true
      # Dynamic Middleware
      - traefik.http.middlewares.dynamic.plugin.sablier.names=sablier-whoami-1
      - traefik.http.middlewares.dynamic.plugin.sablier.sablierUrl=http://sablier:10000
      - traefik.http.middlewares.dynamic.plugin.sablier.dynamic.sessionDuration=1m

  whoami:
    image: containous/whoami:v1.5.0
    # Cannot use labels because as soon as the container is stopped, the labels are not treated by Traefik
    # The route doesn't exist anymore. Use dynamic-config.yml file instead.
    # labels:
    #  - traefik.enable
    #  - traefik.http.routers.whoami-dynamic.rule=PathPrefix(`/dynamic/whoami`)
    #  - traefik.http.routers.whoami-dynamic.middlewares=dynamic@docker

dynamic-config.yaml

http:
  services:
    whoami:
      loadBalancer:
        servers:
        - url: "http://whoami:80"

  routers:
    whoami-dynamic:
      rule: PathPrefix(`/dynamic/whoami`)
      entryPoints:
        - "http"
      middlewares:
        - dynamic@docker
      service: "whoami"

Traefik with Docker Swarm

⚠️ Limitations

  • Traefik will evict the service from its pool as soon as the service is 0/0. You must add the traefik.docker.lbswarm label.
    services:
      whoami:
        image: containous/whoami:v1.5.0
        deploy:
          replicas: 0
          labels:
            - traefik.docker.lbswarm=true
    
  • We cannot use allowEmptyServices because if you use the blocking strategy you will receive a 503.

Traefik with Kubernetes

  • The format of the names section is <KIND>_<NAMESPACE>_<NAME>_<REPLICACOUNT> where _ is the delimiter.
    • Thus no _ are allowed in <NAME>
  • KIND can be either deployment or statefulset

⚠️ Limitations

  • Traefik will evict the service from its pool as soon as there is no endpoint available. You must use allowEmptyServices
  • Blocking Strategy is not yet supported because of how Traefik handles the pod ip.

See Kubernetes E2E Traefik Test script to see how it is reproduced

Plugin

The plugin is available in the Traefik Plugin Catalog

Development

You can use this to load the plugin.

version: "3.7"

services:
  traefik:
    image: traefik:2.9.1
    command:
      - --experimental.localPlugins.sablier.moduleName=github.com/acouvreur/sablier
      - --entryPoints.http.address=:80
      - --providers.docker=true
    ports:
      - "8080:80"
    volumes:
      - '/var/run/docker.sock:/var/run/docker.sock'
      - '../../..:/plugins-local/src/github.com/acouvreur/sablier'
      - './dynamic-config.yml:/etc/traefik/dynamic-config.yml'

But I recommend you to use the e2e folder.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error)

New function creates the configuration

Types

type BlockingConfiguration

type BlockingConfiguration struct {
	Timeout string `yaml:"timeout"`
}

type Config

type Config struct {
	SablierURL      string `yaml:"sablierUrl"`
	Names           string `yaml:"names"`
	Group           string `yaml:"group"`
	SessionDuration string `yaml:"sessionDuration"`

	Dynamic  *DynamicConfiguration  `yaml:"dynamic"`
	Blocking *BlockingConfiguration `yaml:"blocking"`
	// contains filtered or unexported fields
}

func CreateConfig

func CreateConfig() *Config

func (*Config) BuildRequest

func (c *Config) BuildRequest(middlewareName string) (*http.Request, error)

type DynamicConfiguration

type DynamicConfiguration struct {
	DisplayName      string `yaml:"displayname"`
	ShowDetails      *bool  `yaml:"showDetails"`
	Theme            string `yaml:"theme"`
	RefreshFrequency string `yaml:"refreshFrequency"`
}

type SablierMiddleware

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

func (*SablierMiddleware) ServeHTTP

func (sm *SablierMiddleware) ServeHTTP(rw http.ResponseWriter, req *http.Request)

Jump to

Keyboard shortcuts

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