Traefik Proxy

Cloud Native Application Proxy

Simplify and automate the discovery, routing, and load balancing of microservices.

URL Rewrite : Middleware

Traekfik's chainable Middleware applies only to objects declaring it. Use K8s Ingress with annotations for simple configurations, else Traefik's IngressRoute for full Traefik configuration control.

1.a. /old-path --> /new-path

Middleware (ReplacePath) / Ingress (annotations)

---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
## ReplacePath
metadata:
  name: replace-path
  namespace: default
spec:
  replacePath:
    path: "/new-path"
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  namespace: default
  annotations:
    ## Having no "middlewares" key, Kubernetes Ingress' 
    ## are unaffected by any middlewares unless declared 
    ## at traefik annotation key using this pattern : <namespace>-<middleware-name>@kubernetescrd
    traefik.ingress.kubernetes.io/router.middlewares: "default-replace-path@kubernetescrd"
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /old-path
        pathType: Prefix
        backend:
          service:
            name: svc-x
            port:
              number: 80

1.b. /old-path --> /new-path

Middleware (ReplacePath) / IngressRoute

---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
## ReplacePath
metadata:
  name: replace-path
  namespace: default
spec:
  replacePath:
    path: "/new-path"
---
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: example-ingressroute
  namespace: default
spec:
  entryPoints:
    - web
  routes:
    - middlewares:
        - name: replace-path
      kind: Rule # The only routes.kind 
      match: "Host(`example.com`) && Path(`/old-path`)"
      services:
        - name: svc-x
          port: 80

2. /api/v1/(.*) --> /v2/$1

Middleware (ReplacePathRegex) / IngressRoute

---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
## ReplacePathRegex
metadata:
  name: replace-path-regex
  namespace: default
spec:
  replacePathRegex:
    regex: "^/api/v1/(.*)"
    replacement: "/v2/$1"
---
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: example-ingressroute-regex
  namespace: default
spec:
  entryPoints:
    - web
  routes:
    - match: "Host(`example.com`) && PathPrefix(`/api/v1`)"
      kind: Rule
      services:
        - name: svc-x
          port: 80
      middlewares:
        - name: replace-path-regex

3. /app --> /

Middleware (AddPrefix) / IngressRoute

---
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  name: add-prefix
  namespace: default
spec:
  addPrefix:
    prefix: "/app"
---
apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  name: example-ingressroute-prefix
  namespace: default
spec:
  entryPoints:
    - web
  routes:
    - match: "Host(`example.com`)"
      kind: Rule
      services:
        - name: svc-x
          port: 80
      middlewares:
        - name: add-prefix

Lab

app.yaml

Hub API Gateway

Traefik Hub API Gateway is a drop-in replacement for Traefik Proxy. It can do everything Traefik Proxy does, with additional capabilities and support out of the box.