kubernetes技术

k8s ingress controllerr重写路径访问

2019-12-13  本文已影响0人  炳爷

示例

部署ingress 的时候可能会配置path属性
例如:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/service-weight: ''
  creationTimestamp: '2019-12-12T08:50:44Z'
  generation: 13
  name: ad-log-collect-ingress
  namespace: adserver
  resourceVersion: '12145449'
  selfLink: >-
    /apis/extensions/v1beta1/namespaces/adserver/ingresses/ad-log-collect-ingress
  uid: 7c1658a8-1cbc-11ea-92f8-00163e123e64
spec:
  rules:
    - http:
        paths:
          - backend:
              serviceName: ad-log-collect-svc
              servicePort: 8090
            path: /
status:
  loadBalancer:
    ingress:
      - ip: 106.15.219.101

对于这个配置,我们通过
http://ingressIp/ 就能访问到我们的应用,但是这会存在一个问题,如果部署了其他应用,请求都会被这个应用过滤
这样的话就没办法访问,我们的目标很简单,每个项目都有子路径,例如有A B项目,那么访问A,B项目的路径前缀都是这样的

http://ingressIp/A
http://ingressIp/B

如果要想达到这个目的,需要增加下面的配置:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/service-weight: ''
  creationTimestamp: '2019-12-12T08:50:44Z'
  generation: 13
  name: ad-log-collect-ingress
  namespace: adserver
  resourceVersion: '12145449'
  selfLink: >-
    /apis/extensions/v1beta1/namespaces/adserver/ingresses/ad-log-collect-ingress
  uid: 7c1658a8-1cbc-11ea-92f8-00163e123e64
spec:
  rules:
    - http:
        paths:
          - backend:
              serviceName: ad-log-collect-svc
              servicePort: 8090
            path: /adlog-collect(/|$)(.*)
status:
  loadBalancer:
    ingress:
      - ip: 106.15.219.101
    nginx.ingress.kubernetes.io/rewrite-target: /$2
            path: /adlog-collect(/|$)(.*)

就是我们的配置变动项目

上一篇下一篇

猜你喜欢

热点阅读