kubernetesgoogle-cloud-platformgoogle-kubernetes-enginesession-affinity

gcloud command for session affinity


I am trying to enable session affinity for one of my backend services on GKE. The backend service exists currently with Session Affinity off, so it will be an update to an existing service. I know it is offered through the GKE documentation here https://cloud.google.com/load-balancing/docs/backend-service#session_affinity

I am seeing the gcloud command in the documentation:

By default, all Google Cloud load balancers select backends by using a five-tuple hash (--session-affinity=NONE)

I should be able to change it from NONE to CLIENT_IP or similar.

After exploring the gcloud commands for some time and googling around I am still unable to figure out where this setting is applied. From my understanding this is to configure the Google External Application Load Balancer before it hits my Gateway. There is no option under Load Balancing in the GCP Console.

Has anyone done this before and can help? Thanks in advance


Solution

  • As per this official document

    Allows "None" and "ClientIP" needed to keep session affinity Activate session affinity depending on client IP. ClientIP or None is required. Defaults to None.

    You can use a BackendConfig to set session affinity to client IP or generated cookie.

    Set affinityType to "CLIENT_IP" in the BackendConfig to set client IP affinity, as shown in the example below.

    apiVersion:cloud.google.com/v1
    kind:BackendConfig
    metadata:
         name:my-backendconfig
    spec:
     sessionAffinity:
       affinityType:"CLIENT_IP"
    

    To use a BackendConfig to set generated cookie affinity , set affinityType to GENERATED_COOKIE in your BackendConfig manifest. You can also use affinityCookieTtlSec to set the time period for the cookie to remain active.

    The following manifest sets the affinity type to generated cookie and gives the cookies a TTL of 50 seconds:

    apiVersion:cloud.google.com/v1
        kind:BackendConfig
        metadata:
             name:my-backendconfig
        spec:
         sessionAffinity:
           affinityType:GENERATED_COOKIE"
        affinityCookieTtlSec: 50
    

    NOTE:

    If you want to set up session affinity, use a cluster that is native to the VPC. Only services supported by network endpoint groups can benefit from session affinity, and these groups need a VPC-native cluster.

    For more information refer to this blog 1 and blog 2 by Medium