如何在 envoy 代理中设置 Hash Key 值以实现 RING_HASH 负载平衡

dns*_*nsh 3 load-balancing envoyproxy

我正在尝试根据某些请求标头在特使代理上设置 RING_HASH 负载平衡。从文档看来我必须在中设置哈希键filter_metadata

filter_metadata:
    envoy.lb:
      hash_key: "YOUR HASH KEY"
Run Code Online (Sandbox Code Playgroud)

但是,我无法找出 hash_key 的可能值/表达式是什么。我尝试为哈希键配置固定值,但请求仍然不会发送到同一上游服务器。

我的envoy.yaml

admin:
  address:
    socket_address: { address: 0.0.0.0, port_value: 9901 }

static_resources:
  listeners:
  - name: listener_0
    address:
      socket_address: { address: 0.0.0.0, port_value: 10000 }
    filter_chains:
    - filters:
      - name: envoy.filters.network.http_connection_manager
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
          stat_prefix: ingress_http
          codec_type: AUTO
          route_config:
            name: local_route
            virtual_hosts:
            - name: local_service
              domains: ["*"]
              routes:
              - match: { prefix: "/" }
                route: { cluster: some_service }
          http_filters:
          - name: envoy.filters.http.router

  clusters:
  - name: some_service
    connect_timeout: 0.25s
    type: STATIC
    lb_policy: RING_HASH
    metadata:
      filter_metadata:
        envoy.lb:
          hash_key: "fixed_value"
    load_assignment:
      cluster_name: some_service
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: 172.19.0.2
                port_value: 8080
        - endpoint:
            address:
              socket_address:
                address: 172.19.0.3
                port_value: 8080
        - endpoint:
            address:
              socket_address:
                address: 172.19.0.4
                port_value: 8080
              
Run Code Online (Sandbox Code Playgroud)

dns*_*nsh 7

我猜,envoy.lb元数据用于查找上游主机的哈希值,而不是为请求配置密钥。

还有另一种配置,应该使用hash_policy 。

我的最终工作特使配置如下所示。粘性会话基于 headersticky-key

admin:
  address:
    socket_address: { address: 0.0.0.0, port_value: 9901 }

static_resources:
  listeners:
  - name: listener_0
    address:
      socket_address: { address: 0.0.0.0, port_value: 10000 }
    filter_chains:
    - filters:
      - name: envoy.filters.network.http_connection_manager
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
          stat_prefix: ingress_http
          codec_type: AUTO
          route_config:
            name: local_route
            virtual_hosts:
            - name: local_service
              domains: ["*"]
              routes:
              - match: { prefix: "/" }
                route: 
                  cluster: some_service
                  hash_policy:
                    - header:
                        header_name: sticky-key
          http_filters:
          - name: envoy.filters.http.router

  clusters:
  - name: some_service
    connect_timeout: 0.25s
    type: STATIC
    lb_policy: RING_HASH
    load_assignment:
      cluster_name: some_service
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: 172.19.0.2
                port_value: 8080
        - endpoint:
            address:
              socket_address:
                address: 172.19.0.3
                port_value: 8080
        - endpoint:
            address:
              socket_address:
                address: 172.19.0.4
                port_value: 8080
              
Run Code Online (Sandbox Code Playgroud)