我们有一个用例,我们必须根据 URL 中传递的 ID 为不同的 svc 路由 api,即如果 这是用例但不起作用
如果请求到达 hello.domain.com/hello-service/v1/organizations/4999c9f1/(.),则路由应该类似于 hello-setup-svc,如果请求到达 hello.domain.com,那么它应该路由到 hello-setup-svc /hello-service/v1/organizations/4999c9f1/(.) 那么它应该路由到 hello-setup-svc
在 Kubernetes 集群中创建 Ingress 资源。此资源将充当指向您的服务的外部流量的入口点。
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: hello-ingress
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- host: hello.domain.com
http:
paths:
- path: /hello-service/v1/organisations/(?<id>\w+)/(.*)
pathType: Regex
backend:
service:
name: hello-setup-svc
port:
number: 80
- path: /hello-service/v1/organisations/(?<id>\w+)/(.*)
pathType: Regex
backend:
service:
name: hello-setup-svc
port:
number: 80
使用此 Ingress 配置,当请求到达
hello.domain.com/hello-service/v1/organisations/4999c9f1/some-path
时,Nginx Ingress Controller 将匹配第一个路径规则,捕获 id 值为 4999c9f1
,并将 URL 重写为 /some-path。然后,该请求将被转发至 hello-setup-svc
服务。
同样,当请求来到
hello.domain.com/hello-service/v1/organisations/4999c9f1/some-other-path
时,Nginx Ingress Controller会匹配第二条路径规则,捕获id值为4999c9f1
,并将URL重写为/some-other-path。然后,该请求将被转发至 hello-setup-svc
服务。
只需确保
hello-setup-svc
服务已正确配置为处理请求,并且 Nginx Ingress Controller 已在 Kubernetes 集群中正确部署和配置。