Pin requests to an endpoint by header
Use the x-user header for endpoint affinity with RingHash. Requests with the same value stay on the same endpoint while the eligible endpoint set and configuration remain stable; scaling or health changes can move them.
Configuration
apiVersion: kuma.io/v1alpha1
kind: MeshLoadBalancingStrategy
metadata:
name: backend-hash-on-user
namespace: kong-mesh-demo
labels:
kuma.io/mesh: default
spec:
targetRef:
kind: Dataplane
labels:
app: frontend
to:
- targetRef:
kind: MeshService
labels:
kuma.io/display-name: backend
default:
loadBalancer:
type: RingHash
hashPolicies:
- type: Header
header:
name: x-userCopied!
type: MeshLoadBalancingStrategy
name: backend-hash-on-user
mesh: default
spec:
targetRef:
kind: Dataplane
labels:
app: frontend
to:
- targetRef:
kind: MeshService
labels:
kuma.io/display-name: backend
default:
loadBalancer:
type: RingHash
hashPolicies:
- type: Header
header:
name: x-userCopied!
Adjust konnect_mesh_control_plane.my_meshcontrolplane.id and konnect_mesh.my_mesh.name according to your current configuration.
resource "konnect_mesh_load_balancing_strategy" "backend_hash_on_user" {
provider = konnect-beta
type = "MeshLoadBalancingStrategy"
name = "backend-hash-on-user"
spec = {
target_ref = {
kind = "Dataplane"
labels = {
app = "frontend"
}
}
to = [
{
target_ref = {
kind = "MeshService"
labels = {
"kuma.io/display-name" = "backend"
}
}
default = {
load_balancer = {
type = "RingHash"
}
hash_policies = [
{
type = "Header"
header = {
name = "x-user"
}
}
]
}
}
]
}
labels = {
"kuma.io/mesh" = konnect_mesh.my_mesh.name
}
cp_id = konnect_mesh_control_plane.my_meshcontrolplane.id
mesh = konnect_mesh.my_mesh.name
}
Copied!