Rewriting paths with Traefik

5/24/2018

Can someone give a simple example on how to route the following URLs:

http://monitor.app.com/service-one
http://monitor.app.com/service-two
http://monitor.app.com/service-three
http://monitor.app.com/service-four

To the following backend services?

http://service-one/monitor
http://service-two/monitor
http://service-three/monitor
http://service-four/monitor

Preferably using the [file] syntax of Traefik, although any is fine.

-- AndrewMcLagan
docker
kubernetes
nginx
reverse-proxy
traefik

1 Answer

5/25/2018

Here is a configuration for your example. Adjust it according to your real cluster configuration:

apiVersion: v1
kind: Service
metadata:
  name: service-one
spec:
  selector:
    k8s-app: service-one-app
  ports:
  - port: 80
    targetPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: service-two
spec:
  selector:
    k8s-app: service-two-app
  ports:
  - port: 80
    targetPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: service-three
spec:
  selector:
    k8s-app: service-three-app
  ports:
  - port: 80
    targetPort: 8080
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: monitor.app
  annotations:
    kubernetes.io/ingress.class: traefik
    traefik.ingress.kubernetes.io/rewrite-target: /monitor # set path to result request
spec:
  rules:
  - host: monitor.app.com
    http:
      paths:
      - path /service-one  # path for routing, it will be removed because of PathPrefixStrip settings
        backend:
          serviceName: service-one
          servicePort: 80
      - path /service-two  # path for routing, it will be removed because of PathPrefixStrip settings
        backend:
          serviceName: service-two
          servicePort: 80
      - path /service-three  # path for routing, it will be removed because of PathPrefixStrip settings
        backend:
          serviceName: service-three
          servicePort: 80

Additional information could be found here:

-- VAS
Source: StackOverflow