Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Session Affinity on requests to Kubernetes service?

I could not find a documentation that specifies how Kubernetes service behaves when the affiliated deployment is scaled with multiple replicas.

I'm assuming there's some sort of load balancing. Is it related to the service type?

Also, I would want to have some affinity in the request forwarded by the service (i.e all requests with a certain suffix should always be mapped to the same pod if possible, etc). Is that achievable? Closes I've seen is Ambassador, but that is affinity in the service level, and not pod level.

like image 577
Mugen Avatar asked Nov 20 '19 13:11

Mugen


People also ask

How session affinity works Kubernetes?

Sticky sessions or session affinity, is a feature that allows you to keep a session alive for a certain period of time. In a Kubernetes cluster, all the traffic from a client to an application, even if you scale from 1 to 3 or more replicas, will be redirected to the same pod.

Does Kubernetes service use round robin?

Services in Kubernetes use the virtual IPs which the kube-proxy feature manages. The former default kube-proxy mode was userspace, which allocates the next available Kubernetes pod using round-robin load distribution on an IP list, and then rotates or otherwise permutes the list.

What is client IP in session affinity?

This means that all traffic from a client to a pod will be directed to the same pod. If you want to make sure that connections from a particular client are passed to the same Pod each time, you can select the session affinity based on the client's IP addresses by setting service.spec.sessionAffinity to "ClientIP"

Can two services run on the same port Kubernetes?

With this configuration you can't have multiple services listening on port 80. Kubernetes 0.5. x introduced a new networking model, which map an separate IP for each services. So once GKE upgrade you will be able to have multiple services exposed on different IP/ports.


1 Answers

Deployment: Stateless workload

I could not find a documentation that specifies how Kubernetes service behaves when the affiliated deployment is scaled with multi replicas.

Pods deployed with Deployment is supposed to be stateless.

Ingress to Service routing

When using Ingress, L7-proxy, the routing can be based on http request content, but this depends on what implementation of an IngressController you are using. E.g. Ingress-nginx has some support for sticky sessions and other implementations may have what you are looking for. E.g. Istio has support similar settings.

Ambassador

Ambassador that you write about does also have some support for session affinity / sticky sessions.

Configuring sticky sessions makes Ambassador route requests to the same backend service in a given session. In other words, requests in a session are served by the same Kubernetes pod

Pod to Service routing

When a pod in your cluster does an http request to a Service within the cluster, the kube-proxy does routing in a round robin way by default.

By default, kube-proxy in userspace mode chooses a backend via a round-robin algorithm.

If you want session affinity on pod-to-service routing, you can set the SessionAffinity: ClientIP field on a Service object.

If you want to make sure that connections from a particular client are passed to the same Pod each time, you can select the session affinity based on client’s IP addresses by setting service.spec.sessionAffinity to “ClientIP” (the default is “None”).

like image 104
Jonas Avatar answered Oct 23 '22 18:10

Jonas