Kubernetes-基于ingress实现重写URL

1、基于ingress实现重写URL基础

官方文档:https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/annotations/

1.1、rewriting可以使用下面的anntations进行控制

1.2、常规的ingress

1、创建service和后端pod的yaml文件(myweb-service.yaml)

apiVersion: v1
kind: Service
metadata:
  name: myweb-service
spec:
  selector:
    app: ingress-nginx-pod
  ports:
  - port: 11180
    targetPort: 80
    protocol: TCP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: ingress-nginx-deployment
spec:
  selector:
    matchLabels:
      app: ingress-nginx-pod
  replicas: 1
  template:
    metadata:
      labels:
        app: ingress-nginx-pod
    spec:
      volumes:
      - name: html
        hostPath:
          path: /apps/html
      containers:
      - name: ingress-nginx-container
        image: nginx:latest
        imagePullPolicy: Never
        command: ["/usr/sbin/nginx", "-g", "daemon off;"]
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: html
View Code

2、创建index.html文件

]# mkdir -pv /apps/html/a1/b1/c1/
]# echo '/a1/b1/c1/index.html' > /apps/html/a1/b1/c1/index.html

]# mkdir -pv /apps/html/a2/b2/c2/
]# echo '/a2/b2/c2/index.html' > /apps/html/a2/b2/c2/index.html

3、创建ingress规则的yaml文件(my-ingress.yaml)

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"    #必须有,否则创建出的ingress可能不正常
spec:
  rules:
  - host: localhost
    http:
      paths:
      - path: /a1/b1/c1
        pathType: Prefix
        backend:
          service:
            name: myweb-service
            port:
              number: 11180
      - path: /a2/b2/c2
        pathType: Prefix
        backend:
          service:
            name: myweb-service
            port:
              number: 11180

4、创建service、pod和ingress

]# kubectl apply -f myweb-service.yaml
]# kubectl apply -f my-ingress.yaml

5、访问服务

]# curl -H "Host:localhost" 10.1.1.11:32080/a1/b1/c1/
/a1/b1/c1/index.html

]# curl -H "Host:localhost" 10.1.1.11:32080/a2/b2/c2/
/a2/b2/c2/index.html

2、基于ingress实现重写URL

  • /a2/b2/c2/重写成/page/b2/c2/

1、、创建index.html文件

]# mkdir -pv /apps/html/page/b2/c2/
]# echo '/page/b2/c2/index.html' > /apps/html/page/b2/c2/index.html

2、更新ingress规则的yaml文件(my-ingress.yaml)

  • 注意:重写是针对整个ingress的,因此这里将重写单独使用一个ingress(最好一个重写使用一个ingress)。
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"    #必须有,否则创建出的ingress可能不正常
spec:
  rules:
  - host: localhost
    http:
      paths:
      - path: /a1/b1/c1
        pathType: Prefix
        backend:
          service:
            name: myweb-service
            port:
              number: 11180
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress-rewrite
  annotations:
    kubernetes.io/ingress.class: "nginx"    #必须有,否则创建出的ingress可能不正常
    nginx.ingress.kubernetes.io/rewrite-target: /page/$3/$5/$7    #每一个括号是一个捕获组
spec:
  rules:
  - host: localhost
    http:
      paths:
      - path: /(a2)(/)(b2)(/)(c2)(/|$)(.*)                         #最后必须以".*"结尾
        pathType: Prefix
        backend:
          service:
            name: myweb-service
            port:
              number: 11180

2、更新ingress

]# kubectl apply -f my-ingress.yaml

3、访问服务

]# curl -H "Host:localhost" 10.1.1.11:32080/a1/b1/c1/
/a1/b1/c1/index.html

]# curl -H "Host:localhost" 10.1.1.11:32080/a2/b2/c2/
/page/b2/c2/index.html

1

#                                                                                                                       #
posted @ 2022-06-07 11:37  麦恒  阅读(542)  评论(0编辑  收藏  举报