使用Traefik重写路径

时间:2018-05-24 08:46:48

标签: docker nginx kubernetes reverse-proxy traefik

有人可以简单介绍如何路由以下网址:

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

以下后端服务?

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

最好使用Traefik的[file]语法,尽管一切都很好。

1 个答案:

答案 0 :(得分:0)

以下是您的示例的配置。根据您的真实群集配置进行调整:

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

可在此处找到更多信息: