API
As with all features of Traefik, this handler can be enabled with the static configuration.
Enabling the API in production is not recommended, because it will expose all configuration elements,including sensitive data.
In production, it should be at least secured by authentication and authorizations.
A good sane default (non exhaustive) set of recommendationswould be to apply the following protection mechanisms:
- At the transport level:NOT publicly exposing the API's port,keeping it restricted to internal networks(as in the , applied to networks).
If you enable the API, a new special named [email protected]
is created and can then be referenced in a router.
To enable the API handler, use the following option on the:
# Static Configuration
api: {}
--api=true
And then define a routing configuration on Traefik itself with thedynamic configuration:
# Dynamic Configuration
labels:
- "traefik.http.routers.api.rule=Host(`traefik.domain.com`)"
- ""
- "traefik.http.routers.api.middlewares=auth"
- "traefik.http.middlewares.auth.basicauth.users=test:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/,test2:$$apr1$$d9hr9HBB$$4HxwgUir3HP4EsggP/QNo0"
# Dynamic Configuration
deploy:
labels:
- "traefik.http.routers.api.rule=Host(`traefik.domain.com`)"
- "[email protected]"
- "traefik.http.routers.api.middlewares=auth"
- "traefik.http.middlewares.auth.basicauth.users=test:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/,test2:$$apr1$$d9hr9HBB$$4HxwgUir3HP4EsggP/QNo0"
# Dummy service for Swarm port detection. The port can be any valid integer value.
- "traefik.http.services.dummy-svc.loadbalancer.server.port=9999"
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
name: traefik-dashboard
spec:
routes:
- match: Host(`traefik.domain.com`)
services:
- name:
kind: TraefikService
---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
name: auth
spec:
basicAuth:
secret: secretName # Kubernetes secret named "secretName"
# Dynamic Configuration
- "traefik.http.routers.api.rule=Host(`traefik.domain.com`)"
- "[email protected]"
- "traefik.http.routers.api.middlewares=auth"
# Dynamic Configuration
labels:
- "traefik.http.routers.api.rule=Host(`traefik.domain.com`)"
- "traefik.http"
- "traefik.http.routers.api.middlewares=auth"
- "traefik.http.middlewares.auth.basicauth.users=test:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/,test2:$$apr1$$d9hr9HBB$$4HxwgUir3HP4EsggP/QNo0"
# Dynamic Configuration
[http.routers.my-api]
rule = "Host(`traefik.domain.com`)"
service = "[email protected]"
middlewares = ["auth"]
[http.middlewares.auth.basicAuth]
users = [
"test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/",
"test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
# Dynamic Configuration
http:
routers:
api:
rule: Host(`traefik.domain.com`)
service:
middlewares:
- auth
middlewares:
auth:
basicAuth:
users:
- "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"
- "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"
The router's rule must catch requests for the URI path /api
Using an "Host" rule is recommended, by catching all the incoming traffic on this host domain to the API.However, you can also use "path prefix" rule or any combination or rules.
# Matches http://traefik.domain.com, http://traefik.domain.com/api
# or http://traefik.domain.com/hello
rule = "Host(`traefik.domain.com`)"
# Matches http://api.traefik.domain.com/api or http://domain.com/api
# but does not match http://api.traefik.domain.com/hello
rule = "PathPrefix(`/api`)"
# Matches http://traefik.domain.com/api or http://traefik.domain.com/dashboard
# but does not match http://traefik.domain.com/hello
rule = "Host(`traefik.domain.com`) && (PathPrefix(`/api`) || PathPrefix(`/dashboard`))"
Enable the API in insecure
mode, which means that the API will be available directly on the entryPoint named .
Info
If the entryPoint named traefik
is not configured, it will be automatically created on port 8080.
api:
insecure: true
--api.insecure=true
Optional, Default=true
Enable the dashboard. More about the dashboard features .
[api]
dashboard = true
api:
dashboard: true
--api.dashboard=true
With Dashboard enabled, the router rule must catch requests for both /api
and /dashboard
Please check the to learn more about this and to get examples.
Optional, Default=false
Enable additional endpoints for debugging and profiling, served under /debug/
.
[api]
--api.debug=true