nginx - 如何根据 URL id 在 Istio 上设置哈希?

标签 nginx kubernetes kubernetes-ingress istio istio-gateway

在改进 Kubernetes 上的 API 的上下文中,我正在考虑使用分布式哈希表。我的 API 总是收到对具有此方案的 URL 的请求:

www.myapi.com/id

看了Istio的文档,似乎很直接,很容易得到我想要的东西。事实上,Istio 处理称为 ConsistentHashLB 的负载平衡方案。在这样的方案中,服务目的地是根据从几个可能的字段计算的哈希值来选择的:HTTP header 名称、cookie、源 IP 和 HTTP 查询参数名称。

在我的例子中,我需要根据与请求关联的 id 计算哈希值。

我的问题是双重的和有条件的:

  1. 是否可以将 id 读取为 HTTP 参数名称?
  2. 如果是,我应该如何在 list 中指定规则? (我看过的文档在这方面不够清楚)。

如果是否定的,有什么想法吗?一些技巧?例如,我正在考虑使用 `Nginx 添加 id 作为 HTTP header ,但这会增加一个额外的步骤。

最佳答案

正如我在评论中提到的,如果我理解正确,您正在寻找 ConsistenHashLB 路径,那么有 documentation关于那个。

还有github issue关于那个。


至于 http header 问题,你应该可以添加它:

  1. Virtual Service

Headers istio 文档的一部分显示了如何通过示例添加/删除 header 。

Message headers can be manipulated when Envoy forwards requests to, or responses from, a destination service. Header manipulation rules can be specified for a specific route destination or for all destinations. The following VirtualService adds a test header with the value true to requests that are routed to any reviews service destination. It also removes the foo response header, but only from responses coming from the v1 subset (version) of the reviews service.

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews-route
spec:
  hosts:
  - reviews.prod.svc.cluster.local
  http:
  - headers:
      request:
        set:
          test: "true"
    route:
    - destination:
        host: reviews.prod.svc.cluster.local
        subset: v2
      weight: 25
    - destination:
        host: reviews.prod.svc.cluster.local
        subset: v1
      headers:
        response:
          remove:
          - foo
      weight: 75
  1. Envoy Filter

EnvoyFilter provides a mechanism to customize the Envoy configuration generated by Istio Pilot. Use EnvoyFilter to modify values for certain fields, add specific filters, or even add entirely new listeners, clusters, etc.

在 envoy 过滤器下方,将名为 customer-id 的请求 header 添加到所有通过 istio 入口网关的请求中。我还评论了响应 header 的代码。

apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: lua-filter
  namespace: istio-system
spec:
  workloadSelector:
    labels:
      istio: ingressgateway
  configPatches:
  - applyTo: HTTP_FILTER
    match:
      context: GATEWAY
      listener:
        filterChain:
          filter:
            name: "envoy.http_connection_manager"
            subFilter:
              name: "envoy.router"
    patch:
      operation: INSERT_BEFORE
      value:
       name: envoy.lua
       typed_config:
         "@type": "type.googleapis.com/envoy.config.filter.http.lua.v2.Lua"
         inlineCode: |
            function envoy_on_request(request_handle)
                request_handle:headers():add("customer-id", "alice")
            end
           # function envoy_on_response(response_handle)
           #     response_handle:headers():add("customer-id", "alice")
           # end

关于nginx - 如何根据 URL id 在 Istio 上设置哈希?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66957315/

相关文章:

angular - 使用Docker在nginx内部署时,Angular找不到资源

nginx - 减少健康检查的日志记录

docker 错误 : for nginx Cannot start service nginx: driver failed programming external connectivity on

jenkins kubernetes 插件在循环中运行 Pod,并且永远不会构建作业

kubernetes - GCE Kubernetes session 持久化

kubernetes - 从Ingress按其标签选择服务/ pods

regex - fail2ban 定期在 nginx 中查找 403 请求

https - Kubernetes https服务暴露

kubernetes - 如何使用 kubectl 正确访问多个 kubernetes 集群

nginx - 配置路径字段后,Kubernetes nginx入口显示 “403”