具有混合位置的 Kubernetes Ingress

发布于 2025-01-11 21:28:29 字数 1278 浏览 0 评论 0原文

我有一个 kubernetes 入口,它有一个利用 nginx.ingress.kubernetes.io/rewrite-target 的服务。我有另一条路径,service1/query,它需要重定向到 rewrite-target 指向之外的其他位置。假设它应该充当代理并将请求转发到 google.com 并返回结果。因为rewrite-target适用于定义的所有路径,所以我不确定应该如何继续区分service1/query的路径。

有什么想法吗?

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: myApp
  namespace: myApp
  annotations:
    nginx.ingress.kubernetes.io/enable-cors: "true"
    nginx.ingress.kubernetes.io/cors-allow-methods: "GET, POST, OPTIONS"
    nginx.ingress.kubernetes.io/cors-allow-origin: '$http_origin'
    nginx.ingress.kubernetes.io/cors-allow-credentials: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    cert-manager.io/cluster-issuer: letsencrypt-prod

spec:
  ingressClassName: nginx
  tls:
  - hosts:
      - myhost.org
    secretName: ingress-nginx-tls-cert
  rules:
  - host: myhost.org
    http:
      paths:
      - path: /service1(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: service1
            port:
              number: 80
      - path: /service1/query
        pathType: Prefix

I have a kubernetes ingress that has a service taking advantage of nginx.ingress.kubernetes.io/rewrite-target. I have another path, service1/query that needs to redirect somewhere other than what the rewrite-target is pointing to. Let's say it should act as a proxy and forward the request to google.com and return the result. Because the rewrite-target is for all paths defined, I'm not sure how I should proceed to differentiate the path for service1/query.

Any ideas?

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: myApp
  namespace: myApp
  annotations:
    nginx.ingress.kubernetes.io/enable-cors: "true"
    nginx.ingress.kubernetes.io/cors-allow-methods: "GET, POST, OPTIONS"
    nginx.ingress.kubernetes.io/cors-allow-origin: '$http_origin'
    nginx.ingress.kubernetes.io/cors-allow-credentials: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    cert-manager.io/cluster-issuer: letsencrypt-prod

spec:
  ingressClassName: nginx
  tls:
  - hosts:
      - myhost.org
    secretName: ingress-nginx-tls-cert
  rules:
  - host: myhost.org
    http:
      paths:
      - path: /service1(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: service1
            port:
              number: 80
      - path: /service1/query
        pathType: Prefix

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱皱眉﹌ 2025-01-18 21:28:29

你已经快到了,你只需要改变路径的顺序。 入口路径匹配

paths:
  - path: /service1/query
    pathType: Prefix
  - path: /service1(/|$)(.*)
    pathType: Prefix
    backend:
      service:
        name: service1
        port:
          number: 80

当您添加 时/service1(/|$)(.*) 到第一行,/service1/query 是一个死代码;因为它进入了第一行。

但是,如果您将 /service1/query 放在第一个; nginx 会首先查看它。

You are almost there, you just need to change the paths' order. Ingress Path Matching

paths:
  - path: /service1/query
    pathType: Prefix
  - path: /service1(/|$)(.*)
    pathType: Prefix
    backend:
      service:
        name: service1
        port:
          number: 80

When you add /service1(/|$)(.*) to the first line, the /service1/query is a dead code; because it enters the first line.

However, if you put /service1/query to the first; nginx will look at it first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文