简体   繁体   English

在 Kube.netes 中使用 ingress-nginx 时,是否可以使用路径中捕获的组配置后端?

[英]When using ingress-nginx in Kubernetes, is is possible to configure backends with captured groups from the path?

Is it possible to configure an nginx class ingress in Kube.netes with a backend reference to a captured group from a path regex?是否可以在 Kube.netes 中配置一个nginx class 入口,后端引用来自路径正则表达式的捕获组? There is no indication of this in the ingress-nginx docs https://kube.netes.github.io/ingress-nginx/user-guide/ingress-path-matching/在 ingress-nginx 文档https://kube.netes.github.io/ingress-nginx/user-guide/ingress-path-matching/中没有任何指示

I am trying to reduce the complexity of ingress path / backend definitions, eg from this:我正在尝试降低入口路径/后端定义的复杂性,例如:

...
spec:
  rules:
    - http:
      paths:
        - path: /some/backend1/*
          backend: 
            serviceName: backend1
        - path: /some/otherbackend/*
          backend:
            serviceName: otherbackend

to something like this:像这样:


spec:
  rules:
    - http:
      paths:
        - path: /some/([^/]+)/(.+) 
          backend: 
            serviceName: $1  # or \1 - possible to ref a capture group here?

In the above example, the match regex for path matches "any character that isn't a '/' repeated" - ie it will match the next URI part until '/'.在上面的示例中, path的匹配正则表达式匹配“任何不是'/'重复的字符” - 即它将匹配下一个 URI 部分直到'/'。 Am then looking to pass into serviceName .然后我想传递给serviceName

Is this possible?这可能吗?

edit编辑

I have found a way to shoe horn this into working.我找到了一种方法来解决这个问题。 This is not an ideal way of doing it, because I am injecting an override directly into nginx configs.这不是一种理想的实现方式,因为我将覆盖直接注入到 nginx 配置中。 Would sort of functionality similar to this be worth requesting as an nginx controller feature?与此类似的功能是否值得作为 nginx controller 功能请求?

I have posted a solution to get it working below, even though there are concerns with it - see the disclaimer / concerns below.我已经发布了一个解决方案来让它在下面工作,即使它有问题 - 请参阅下面的免责声明/问题。

disclaimer: Before reproducing this, it is useful to know some of the concerns around working with nginx config directly - see this -免责声明:在重现之前,了解直接使用 nginx 配置的一些问题是很有用的 - 请看这个-

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: dynamic-routing-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/configuration-snippet: |
      set $proxy_upstream_name "default-$BackendService-8088";
spec:
  rules:
  - http:
    paths:
    - path: /dynamci/(?<BackendService>[^/]+)(.+)?
      pathType: ImplementationSpecific
      backend:
        service:
          name: backendservice
          port:
            number: 8088

Now requests to /dynamic/x-backend will get routed to x-backend service, if it exists.现在,对/dynamic/x-backend请求将被路由到x-backend服务(如果存在)。 Heavily caveated - assuming you have consistent spec for the service definitions (eg host / port / namespace / etc).严重警告 - 假设您对服务定义有一致的规范(例如主机/端口/名称空间等)。

The link above that discusses lack of robustness is that the nginx controller translates to nginx config in a correlated way, and now you are taking that + sanity checks (eg by the admission verifier), as well as understanding internal controller naming conventions into the ingress definition.上面讨论缺乏健壮性的链接是 nginx controller 以相关方式转换为 nginx 配置,现在您正在进行 + 健全性检查(例如,通过准入验证程序),以及了解进入入口的内部 controller 命名约定定义。 Without the configuration-snippet annotation, the nginx location block would set proxy_upstream_name to something like default-backendservice-8088 , but then the config snippet injects another set $proxy_upstream_name parameter later, that matches the name of the captured group from the regex - so you have to know how the back references are created and manually maintain this, while ensuring that the override proxy parameter is consistent with how all your services are defined (eg if you have different backend services running on different ports, this won't solve for it and it starts becoming much more complex pretty quick).如果没有configuration-snippet注释, nginx 位置块会将proxy_upstream_name设置为类似default-backendservice-8088东西,但是配置片段稍后会注入另一set $proxy_upstream_name参数,该参数与正则表达式中捕获的组的名称相匹配 - 所以你必须知道如何创建反向引用并手动维护它,同时确保覆盖代理参数与所有服务的定义方式一致(例如,如果您在不同端口上运行不同的后端服务,这将无法解决它它很快就会变得更加复杂)。

While it does get the desired functionality, am not sure if I'll take this forward because of reliability / robustness concerns....虽然它确实获得了所需的功能,但由于可靠性/稳健性问题,我不确定我是否会继续推进它......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM