Kubernetes 重写/重定向路径

Kubernetes 重写/重定向路径

我有 2 个服务使用相同的图像,但这些服务的配置不同。我希望每当我请求 /get_traits_similarity 时,它都会重定向到该服务内的 /get_similarity。有办法吗?以下是我尝试过的最新配置。

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: "/$1"
    nginx.ingress.kubernetes.io/ssl-redirect: 'false'
    nginx.ingress.kubernetes.io/use-regex: 'true'
    nginx.ingress.kubernetes.io/app-root: /get_similarity
  labels:
    app.kubernetes.io/component: hashingservice
    app.kubernetes.io/name: hashing-ingress
    app.kubernetes.io/part-of: hashingapi
  name: hashing-ingress
  namespace: default
spec:
  rules:
    - http:
        paths:
        - backend:
            serviceName: mediasimilarity
            servicePort: 5000
          path: "/(get_similarity)"
        - backend:
            serviceName: mediasimilarity
            servicePort: 5000
          path: "/(create_token)(/get_similarity){1}"
        - backend:
            serviceName: mediasimilarity
            servicePort: 5000
          path: "/(load_index)"
        - backend:
            serviceName: traits-mediasimilarity
            servicePort: 5000
          path: "/(get_traits_similarity)(/|$)(.*)"
        - backend:
            serviceName: traits-mediasimilarity
            servicePort: 5000
          path: "/(create_token)/(get_traits_similarity)(/|$)(.*){1}"
        - backend:
            serviceName: traits-mediasimilarity
            servicePort: 5000
          path: "/(load_index)"

答案1

我发现您的配置存在三个潜在问题:

  1. 注释中的拼写错误:nginx.ingress.kubernetes.io/user-regex:而不是nginx.ingress.kubernetes.io/use-regex:(已修复)

  2. 从 Kubernetes 1.16 版开始,API 版本中的 NetworkPolicyextensions/v1beta1不再提供服务。

  3. 关于改写本身:

    • 您需要正确设定目标:nginx.ingress.kubernetes.io/rewrite-target: /get_similarity/$2

    • 您需要在以下spec:部分下指定您的主机

    • 最后你需要设置你想要重写的路径:path: /get_traits_similarity(/|$)(.*)

以下是一个例子:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /get_similarity/$2
  name: hashing-ingress
  namespace: default
spec:
  rules:
  - host: <your_host>
    http:
      paths:
      - backend:
          serviceName: traits-mediasimilarity
          servicePort: 5000
        path: /get_traits_similarity(/|$)(.*)

如果需要,请参阅链接的文档,如果有帮助,请告诉我。

相关内容