kubernetes 如何使用Ingress Nginx通过url查询字符串路由请求?

v440hwme  于 2022-12-03  发布在  Kubernetes
关注(0)|答案(1)|浏览(189)

我们希望使用注解根据url参数将请求重定向到不同的后端服务(query)
示例:
https://example.com/foo?differentQueryString=0-〉服务a
https://example.com/foo/bar?queryString=0-服务b

  • 注意:路径无关紧要,可以是/foo/bar、/foo或/bar/foo

我们跟进了这件事
Kubernetes NGINX Ingress controller - different route if query string exists
且这
Kubernetes ingress routes with url parameter
但是,我们不希望仅为此设置ConfigMap,也不希望复制对入口的请求,而是重写
这是我们尝试过的

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: test-ingress
 annotations:
    nginx.ingress.kubernetes.io/configuration-snippet: |
      if ($args ~ queryString=0){
            backend.service.name = service-b
          }
spec:
  ingressClassName: nginx
  rules:
    - host: example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: service-a
                port:
                  number: 80
          - path: /
            pathType: Prefix
            backend:
              service:
                name: service-b
                port:
                  number: 80

我们期待得到回应,但我们得到了502从入口Nginx

3htmauhk

3htmauhk1#

我们设法找到了一个不错的解决方案,而无需重写和配置Map
工作很好,还包括Nginx入口指标,因此我们可以相应地进行HPA

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: test-ingress
 annotations:
    nginx.ingress.kubernetes.io/configuration-snippet: |
      if ($args ~ queryString=0){
            set $proxy_upstream_name "default-service-b-80";
            set $proxy_host $proxy_upstream_name;
            set $service_name "service-b"; 
          }
spec:
  ingressClassName: nginx
  rules:
    - host: example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: service-a
                port:
                  number: 80
          - path: /
            pathType: Prefix
            backend:
              service:
                name: service-b
                port:
                  number: 80

$proxy_upstream_name约定为名称空间-服务名称-端口

相关问题