Home > Back-end >  Kubernetes how to maintain session affinity during hpa scale down event
Kubernetes how to maintain session affinity during hpa scale down event

Time:12-21

I have deploy my application in kubernetes using deployment.

  1. whenever user gets login to application pod will generate session for that user.
  2. To maintain session stickiness i have set session cookie using Nginx ingress annotations.
  3. When hpa scale down pods application user is phasing a logout problem when pod is get terminated. if ingress has generated a session using this pod. it needs to log in again.
  4. What i want is some sort of graceful termination of the connection. when pod is in a terminating state it should serve existing sessions until grace period.

CodePudding user response:

What i want is some sort of graceful termination of connection. when pod is in terminating state it should serve existing sessions until grace period.

You can use the key in POD spec : terminationGracePeriodSeconds

this will wait for the mentioned second and after that POD will get terminated.

You can read more at : https://cloud.google.com/blog/products/containers-kubernetes/kubernetes-best-practices-terminating-with-grace

CodePudding user response:

The answer Harsh Manvar is great, However, I want to expand it a bit :)

You can of course use terminationGracePeriodSeconds in the POD spec. Look at the example yaml:

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: my-image
  terminationGracePeriodSeconds: 60

At this point, Kubernetes waits for a specified time called the termination grace period. By default, this is 30 seconds. It’s important to note that this happens in parallel to the preStop hook and the SIGTERM signal. Kubernetes does not wait for the preStop hook to finish.

If your app finishes shutting down and exits before the terminationGracePeriod is done, Kubernetes moves to the next step immediately.

If your pod usually takes longer than 30 seconds to shut down, make sure you increase the grace period. You can do that by setting the terminationGracePeriodSeconds option in the Pod YAML. For example, to change it to 60 seconds.

For more look here.

If you want to know how exactly looks like pod lifecycle see this link to the official documentation. The part about the termination of pods should be most interesting. You will also have it described how exactly the termination takes place.

  • Related