Kubernetes Ingress后端子路径

qfe3c7zg  于 2023-04-11  发布在  Kubernetes
关注(0)|答案(1)|浏览(175)

是否可以通过任何方式将主机路径重定向到后端的子路径?类似于子路径对卷的工作方式。
入口看起来像这样:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: jupyter-notebook-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
    - host: jptrntbk.MYDOMAIN.com
      http:
        paths:
          - path: /
            backend:
              serviceName: jupyter-notebook-service
              servicePort: 8888
              subPath: /lab

导航到jptrntbk.MYDOMAIN.com将重定向到后端的/lab,并且所有其他父路径都不可用。

deikduxw

deikduxw1#

使用app-root annotation创建Ingress规则:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/app-root: /app1
  name: approot
  namespace: default
spec:
  rules:
  - host: approot.bar.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /

检查重写是否有效

$ curl -I -k http://approot.bar.com/
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.11.10
Date: Mon, 13 Mar 2017 14:57:15 GMT
Content-Type: text/html
Content-Length: 162
Location: http://approot.bar.com/app1
Connection: keep-alive

或者你可以创建一个带有rewrite annotation的Ingress规则:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /something(/|$)(.*)

在这个入口定义中,(.*)捕获的任何字符都将被分配给占位符$2,然后将其用作rewrite-target注解中的参数。
例如,上面的入口定义将导致以下重写:- rewrite.bar.com/something重写为rewrite.bar.com/-rewrite.bar.com/something/重写为rewrite.bar.com/-rewrite.bar.com/something/new重写为rewrite.bar.com/new
来源:https://kubernetes.github.io/ingress-nginx/examples/rewrite/

相关问题