Pod affinity ensures colocated pods are deployed onto same node. It does not guarantee traffic will be sent to pods on the same node.
You need to use service topology. Kubernetes version 1.17 or later is needed for this feature and you need to enable this feature via feature flag.
Service Topology enables a service to route traffic based upon the
Node topology of the cluster. For example, a service can specify that
traffic be preferentially routed to endpoints that are on the same
Node as the client, or in the same availability zone
Consider a cluster with Nodes that are labeled with their hostname
, zone name
, and region name
. Then you can set the topologyKeys
values of a service to direct traffic as follows.
Only to endpoints
on the same node, failing if no endpoint exists on the node: ["kubernetes.io/hostname"]
.
Preferentially to endpoints
on the same node, falling back to endpoints
in the same zone, followed by the same region, and failing otherwise: ["kubernetes.io/hostname"
, "topology.kubernetes.io/zone"
, "topology.kubernetes.io/region"]
. This may be useful, for example, in cases where data locality is critical.
Preferentially to the same zone
, but fallback on any available endpoint
if none are available within this zone
: ["topology.kubernetes.io/zone", "*"]
.