Kubernetes Ingress——暴露不同主机下的两条路径

Kubernetes Ingress - expose two paths under different hosts

我正在努力处理以下案例。我有一个用 .NET Core 编写的带有某种网关的服务,因为下面两个 GraphQL 端点位于:

https://my-local-cluster.svc/api/abc/graphql
https://my-local-cluster.svc/api/xyz/graphql

现在我正在向我的 Kubernetes 集群添加一个 Ingress,我有这个服务,我想要实现的是:

https://abc.ingresswebsite.com/graphql <- points to https://mywebsite.com/api/abc/graphql
https://xyz.ingresswebsite.com/graphql <- points to https://mywebsite.com/api/xyz/graphql

这可能吗?我正在尝试执行以下操作:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-app-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    appgw.ingress.kubernetes.io/backend-path-prefix: "/api/abc/"
    cert-manager.io/cluster-issuer: letsencrypt
spec:
  tls:
  - hosts:
    - abc.ingresswebsite.com
    secretName: tls-secret
  rules:
  - host: abc.ingresswebsite.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: my-abc-service
            port: 
              number: 80

如您所见,我使用了 appgw.ingress.kubernetes.io/backend-path-prefix: "/api/abc/" 注释,但不幸的是没有任何结果。也许您有关于如何实现的建议,或者是否有可能实现?

谢谢!

您是否尝试在您的 my-abc-service 服务上请求 https://abc.ingresswebsite.com/graphql 转到 /api/abc/graphql

如果是这样,那么 nginx-ingress' rewrite 功能 (https://kubernetes.github.io/ingress-nginx/examples/rewrite/) 可能适合您。

这是文档中的示例:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
  name: rewrite
  namespace: default
spec:
  ingressClassName: nginx
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - path: /something(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: http-svc
            port: 
              number: 80

下面是文档描述的重写结果:

rewrite.bar.com/something rewrites to rewrite.bar.com/

rewrite.bar.com/something/ rewrites to rewrite.bar.com/

rewrite.bar.com/something/new rewrites to rewrite.bar.com/new

所以改编那个例子,你的可能看起来像这样:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-app-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /api/abc/
    cert-manager.io/cluster-issuer: letsencrypt
spec:
  tls:
  - hosts:
    - abc.ingresswebsite.com
    secretName: tls-secret
  rules:
  - host: abc.ingresswebsite.com
    http:
      paths:
      - path: /(.*)
        pathType: Prefix
        backend:
          service:
            name: my-abc-service
            port: 
              number: 80

(注意:我还没有尝试过)