Add 'plugins/traefik/' from commit 'aef1f9e0dd205ea9cdea9e3ccf11900c5fe79b1f'

git-subtree-dir: plugins/traefik
git-subtree-mainline: 1a14070131
git-subtree-split: aef1f9e0dd
This commit is contained in:
Alexis Couvreur
2022-09-30 14:32:09 +00:00
86 changed files with 24035 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
# Docker classic
## Run the demo
1. `git clone git@github.com:acouvreur/traefik-ondemand-plugin.git`
2. `cd traefik-ondemand-plugin/examples/docker_classic`
3. `export TRAEFIK_PILOT_TOKEN=...`
4. `docker-compose up`
The log: `level=error msg="middleware \"ondemand@docker\" does not exist" entryPointName=http routerName=whoami@file` is expected because the file provider is parsed before the docker containers. However this should appear only once and not cause any issue.
5. `docker stop docker_classic_whoami_1`
6. Load `http://localhost/whoami`
7. Wait 1 minute
8. Container is stopped
## Limitations
### Cannot use service labels
Cannot use labels because as soon as the container is stopped, the labels are not treated by Traefik.
The route doesn't exist anymore, so we use dynamic-config.yml file instead.

View File

@@ -0,0 +1,46 @@
version: "3.9"
services:
traefik:
image: traefik
command:
- --api=true
- --api.insecure=true
- --pilot.token=$TRAEFIK_PILOT_TOKEN
- --experimental.plugins.traefik-ondemand-plugin.moduleName=github.com/acouvreur/traefik-ondemand-plugin
- --experimental.plugins.traefik-ondemand-plugin.version=v1.2.0
- --providers.docker=true
- --providers.file.filename=/etc/traefik/dynamic-config.yml
- --entrypoints.http.address=:80
- --entrypoints.https.address=:443
ports:
- "80:80"
- "443:443"
- "8080:8080"
volumes:
- '/var/run/docker.sock:/var/run/docker.sock'
- './dynamic-config.yml:/etc/traefik/dynamic-config.yml'
labels:
- traefik.enable=true
ondemand:
image: ghcr.io/acouvreur/traefik-ondemand-service:1
command:
- --swarmMode=false
volumes:
- '/var/run/docker.sock:/var/run/docker.sock'
labels:
- traefik.enable=true
- traefik.http.middlewares.ondemand.plugin.traefik-ondemand-plugin.name=docker_classic_whoami_1
- traefik.http.middlewares.ondemand.plugin.traefik-ondemand-plugin.serviceUrl=http://ondemand:10000
- traefik.http.middlewares.ondemand.plugin.traefik-ondemand-plugin.timeout=1m
- traefik.http.services.ondemand.loadbalancer.server.port=10000
whoami:
image: containous/whoami
# 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.rule=PathPrefix(`/whoami`)
# - traefik.http.routers.whoami.middlewares=ondemand

View File

@@ -0,0 +1,16 @@
http:
services:
whoami:
loadBalancer:
servers:
- url: "http://whoami:80"
routers:
whoami:
rule: PathPrefix(`/whoami`)
entryPoints:
- "http"
middlewares:
- ondemand@docker
service: "whoami"

View File

@@ -0,0 +1,12 @@
# Docker swarm
## Run the demo
1. `git clone git@github.com:acouvreur/traefik-ondemand-plugin.git`
2. `cd traefik-ondemand-plugin/examples/docker_swarm`
3. `docker swarm init`
4. `export TRAEFIK_PILOT_TOKEN=...`
5. `docker stack deploy -c docker-stack.yml DOCKER_SWARM`
6. Load `http://localhost/nginx`
7. Wait 1 minute
8. Service is scaled to 0/0

View File

@@ -0,0 +1,51 @@
version: "3.9"
services:
traefik:
image: traefik
command:
- --api=true
- --api.insecure=true
- --pilot.token=$TRAEFIK_PILOT_TOKEN
- --experimental.plugins.traefik-ondemand-plugin.moduleName=github.com/acouvreur/traefik-ondemand-plugin
- --experimental.plugins.traefik-ondemand-plugin.version=v1.2.0
- --providers.docker=true
- --providers.docker.swarmmode=true
- --providers.file.filename=/etc/traefik/dynamic-config.yml
- --entrypoints.http.address=:80
- --entrypoints.https.address=:443
ports:
- "80:80"
- "443:443"
- "8080:8080"
volumes:
- '/var/run/docker.sock:/var/run/docker.sock'
ondemand:
image: ghcr.io/acouvreur/traefik-ondemand-service:1
command:
- --swarmMode=true
volumes:
- '/var/run/docker.sock:/var/run/docker.sock'
deploy:
labels:
- traefik.enable=true
- traefik.http.middlewares.ondemand.plugin.traefik-ondemand-plugin.name=DOCKER_SWARM_nginx
- traefik.http.middlewares.ondemand.plugin.traefik-ondemand-plugin.serviceUrl=http://ondemand:10000
- traefik.http.middlewares.ondemand.plugin.traefik-ondemand-plugin.timeout=1m
- traefik.http.services.ondemand.loadbalancer.server.port=10000
nginx:
image: nginx
deploy:
replicas: 0
labels:
- traefik.enable=true
# If you do not use the swarm load balancer, traefik will evict the service from its pool
# as soon as the service is 0/0. If you do not set that, fallback to dynamic-config.yml file usage.
- traefik.docker.lbswarm=true
- traefik.http.routers.nginx.middlewares=ondemand@docker
- traefik.http.routers.nginx.rule=PathPrefix(`/nginx`)
- traefik.http.services.nginx.loadbalancer.server.port=80

View File

@@ -0,0 +1,30 @@
# Kubernetes
## Run the demo
# you need docker-compose, kubectl and helm (v3) installed
1. `git clone git@github.com:acouvreur/traefik-ondemand-plugin.git`
2. `cd traefik-ondemand-plugin/examples/kubernetes`
3. `docker-compose up`
4. Wait 1 minute
5. `export KUBECONFIG=./kubeconfig.yaml`
5. `helm repo add traefik https://helm.traefik.io/traefik`
6. `helm repo update`
7. Edit values.yaml and add your traefik pilot.token
8. `helm install traefik traefik/traefik -f values.yaml --namespace kube-system `
9. `kubectl apply -f deploy-whoami.yml`
10. `kubectl apply -f manifests.yml`
11. `kubectl scale deploy whoami --replicas=0`
12. Browse to http://localhost/
13. `kubectl get deployments -o wide`
```
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
whoami 1/1 1 1 16m whoami containous/whoami app=whoami
```
13. After 1 minute: `kubectl get deployments -o wide`
```
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
whoami 0/0 0 0 17m whoami containous/whoami app=whoami`
```
14. Browse to http://localhost/

View File

@@ -0,0 +1,49 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: whoami
spec:
replicas: 1
selector:
matchLabels:
app: whoami
template:
metadata:
labels:
app: whoami
spec:
containers:
- name: whoami
image: containous/whoami
---
apiVersion: v1
kind: Service
metadata:
name: whoami-service
spec:
ports:
- name: http
targetPort: 80
port: 80
selector:
app: whoami
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: whoami-ingress
annotations:
kubernetes.io/ingress.class: traefik
traefik.ingress.kubernetes.io/router.middlewares: default-ondemand-whoami@kubernetescrd
spec:
rules:
- host: localhost
http:
paths:
- path: /
pathType: ImplementationSpecific
backend:
service:
name: whoami-service
port:
number: 80

View File

@@ -0,0 +1,29 @@
version: '3'
services:
server:
image: "rancher/k3s:${K3S_VERSION:-latest}"
command: server --no-deploy traefik
tmpfs:
- /run
- /var/run
ulimits:
nproc: 65535
nofile:
soft: 65535
hard: 65535
privileged: true
restart: always
environment:
- K3S_KUBECONFIG_OUTPUT=/output/kubeconfig.yaml
- K3S_KUBECONFIG_MODE=666
volumes:
- k3s-server:/var/lib/rancher/k3s
# This is just so that we get the kubeconfig file out
- .:/output
ports:
- 6443:6443 # Kubernetes API Server
- 80:80 # Ingress controller port 80
- 443:443 # Ingress controller port 443
volumes:
k3s-server: {}

View File

@@ -0,0 +1,86 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: traefik-ondemand-service
namespace: kube-system
labels:
app: traefik-ondemand-service
spec:
replicas: 1
selector:
matchLabels:
app: traefik-ondemand-service
template:
metadata:
labels:
app: traefik-ondemand-service
spec:
serviceAccountName: traefik-ondemand-service
serviceAccount: traefik-ondemand-service
containers:
- name: traefik-ondemand-service
image: ghcr.io/acouvreur/traefik-ondemand-service:1
args: ["--swarmMode=false", "--kubernetesMode=true"]
ports:
- containerPort: 10000
---
apiVersion: v1
kind: Service
metadata:
name: traefik-ondemand-service
namespace: kube-system
spec:
selector:
app: traefik-ondemand-service
ports:
- protocol: TCP
port: 10000
targetPort: 10000
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: traefik-ondemand-service
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: traefik-ondemand-service
namespace: kube-system
rules:
- apiGroups:
- apps
resources:
- deployments
- deployments/scale
verbs:
- patch
- get
- update
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: traefik-ondemand-service
namespace: kube-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: traefik-ondemand-service
subjects:
- kind: ServiceAccount
name: traefik-ondemand-service
namespace: kube-system
---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: ondemand-whoami
namespace: default
spec:
plugin:
traefik-ondemand-plugin:
name: deployment_default_whoami_1
serviceUrl: 'http://traefik-ondemand-service:10000'
timeout: 1m

View File

@@ -0,0 +1,10 @@
# traefik helm values
additionalArguments:
- "--pilot.token=XXXXX_YOURTOKEN_XXXXXXXXXXXXXXXX"
- "--experimental.plugins.traefik-ondemand-plugin.modulename=github.com/acouvreur/traefik-ondemand-plugin"
- "--experimental.plugins.traefik-ondemand-plugin.version=v1.2.0"
- "--providers.kubernetesingress.allowEmptyServices=true"
experimental:
plugins:
enabled: true

View File

@@ -0,0 +1,24 @@
# Docker swarm
## Run the demo
1. `git clone git@github.com:acouvreur/traefik-ondemand-plugin.git`
2. `cd traefik-ondemand-plugin/examples/multiple_containers`
3. `docker swarm init`
4. `export TRAEFIK_PILOT_TOKEN=...`
5. `docker stack deploy -c docker-stack.yml DOCKER_SWARM`
6. Load `http://localhost/nginx`
7. Load `http://localhost/whoami`
8. After 1 minute whoami is scaled to 0/0
9. After 5 minutes nginx is scaled to 0/0
10. `docker stack rm DOCKER_SWARM`
## Limitations
### Define a middleware per service/container
Due to Traefik plugin, the interface is to provide a config and a `ServeHTTP` request.
This function has no access to the Traefik configuration, thus no way to determine the container/service associated to the request.
See https://github.com/acouvreur/traefik-ondemand-plugin/issues/8#issuecomment-931940533.

View File

@@ -0,0 +1,65 @@
version: "3.9"
services:
traefik:
image: traefik
command:
- --api=true
- --api.insecure=true
- --pilot.token=$TRAEFIK_PILOT_TOKEN
- --experimental.plugins.traefik-ondemand-plugin.moduleName=github.com/acouvreur/traefik-ondemand-plugin
- --experimental.plugins.traefik-ondemand-plugin.version=v1.2.0
- --providers.docker=true
- --providers.docker.swarmmode=true
- --providers.file.filename=/etc/traefik/dynamic-config.yml
- --entrypoints.http.address=:80
- --entrypoints.https.address=:443
ports:
- "80:80"
- "443:443"
- "8080:8080"
volumes:
- '/var/run/docker.sock:/var/run/docker.sock'
ondemand:
image: ghcr.io/acouvreur/traefik-ondemand-service:1
command:
- --swarmMode=true
volumes:
- '/var/run/docker.sock:/var/run/docker.sock'
deploy:
labels:
- traefik.enable=true
- traefik.http.middlewares.ondemand-nginx.plugin.traefik-ondemand-plugin.name=DOCKER_SWARM_nginx
- traefik.http.middlewares.ondemand-nginx.plugin.traefik-ondemand-plugin.serviceUrl=http://ondemand:10000
- traefik.http.middlewares.ondemand-nginx.plugin.traefik-ondemand-plugin.timeout=5m
- traefik.http.middlewares.ondemand-whoami.plugin.traefik-ondemand-plugin.name=DOCKER_SWARM_whoami
- traefik.http.middlewares.ondemand-whoami.plugin.traefik-ondemand-plugin.serviceUrl=http://ondemand:10000
- traefik.http.middlewares.ondemand-whoami.plugin.traefik-ondemand-plugin.timeout=1m
- traefik.http.services.ondemand.loadbalancer.server.port=10000
nginx:
image: nginx
deploy:
replicas: 0
labels:
- traefik.enable=true
# If you do not use the swarm load balancer, traefik will evict the service from its pool
# as soon as the service is 0/0. If you do not set that, fallback to dynamic-config.yml file usage.
- traefik.docker.lbswarm=true
- traefik.http.routers.nginx.middlewares=ondemand-nginx@docker
- traefik.http.routers.nginx.rule=PathPrefix(`/nginx`)
- traefik.http.services.nginx.loadbalancer.server.port=80
whoami:
image: containous/whoami
deploy:
replicas: 0
labels:
- traefik.enable=true
- traefik.docker.lbswarm=true
- traefik.http.routers.whoami.middlewares=ondemand-whoami@docker
- traefik.http.routers.whoami.rule=PathPrefix(`/whoami`)
- traefik.http.services.whoami.loadbalancer.server.port=80