Search code examples
load-balancingenvoyproxy

How to set Hash Key value in envoy proxy for RING_HASH load balancing


I am trying to set up RING_HASH load balancing on the envoy proxy based on some request header. From documentation it looks like I have to set hash key in filter_metadata

filter_metadata:
    envoy.lb:
      hash_key: "YOUR HASH KEY"

But, I am not able to find out what are the possible values/expressions of hash_key. I tried to configure a fixed value for the hash key, but still requests does not go to the same upstream server.

My 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
              

Solution

  • I guess, envoy.lb metadata is used to find hash of upstream host not to configure key for request.

    There is another configuration, hash_policy which should be used.

    My final working envoy configuration looks like this. The sticky session is based on header sticky-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