kubernetessession-cookieskubernetes-ingresssticky-sessionsession-affinity

How to set session affinity between inner servicess inside a Kubernetes cluster


Here is my problem: I have 3 services defined in a kubernetes yaml file:

I need session affinity on the stateful service, but not on the stateless nor front-end service. I need the session affinity to be cookie-based, not clientIP based.

mydomain/stateful  ===> Front-End Service (3 pods) ===> Stateful Service (3 pods, need session affinity)
mydomain/stateless ===> Front-End Service (3 pods) ===> Stateless Service (3 pods, do not need session affinity)

I tried to use Ingress service, but I fail to see how I can use it as a proxy in-between 2 services inside the Kubernetes Cluster. All the examples I see show how to use Ingress as a router for request coming from outside the Cluster.

Here is my poc.yaml so far:

####################################################################
######################### STATEFUL BACKEND #########################
# Deployment for pocbackend containers, listening on port 3000
apiVersion: apps/v1
kind: Deployment
metadata:
  name: stateful-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: stateful-backend
      tier: backend
  template:
    metadata:
      labels:
        app: stateful-backend
        tier: backend
    spec:
      containers:
      - name: pocbackend
        image: pocbackend:2.0
        ports:
           - name: http
             containerPort: 3000
---
# Service for Stateful containers, listening on port 3000
apiVersion: v1
kind: Service
metadata:
  name: api-stateful
spec:
  selector:
    app: stateful-backend
    tier: backend
  ports:
  - protocol: TCP
    port: 3002
    targetPort: http
  #sessionAffinity: ClientIP
---
#####################################################################
######################### STATELESS BACKEND #########################
# Deployment for pocbackend containers, listening on port 3000
apiVersion: apps/v1
kind: Deployment
metadata:
  name: stateless-backend
spec:
  replicas: 3
  selector:
    matchLabels:
      app: stateless-backend
      tier: backend
  template:
    metadata:
      labels:
        app: stateless-backend
        tier: backend
    spec:
      containers:
      - name: pocbackend
        image: pocbackend:2.0
        ports:
           - name: http
             containerPort: 3000
---
# Service for Stateless containers, listening on port 3000
apiVersion: v1
kind: Service
metadata:
  name: api-stateless
spec:
  selector:
    app: stateless-backend
    tier: backend
  ports:
  - protocol: TCP
    port: 3001
    targetPort: http
---
#############################################################
######################### FRONT END #########################
# deployment of the container pocfrontend listening to port 3500
apiVersion: apps/v1
kind: Deployment
metadata:
  name: front-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: frontend
      tier: frontend
  template:
    metadata:
      labels:
        app: frontend
        tier: frontend
    spec:
      containers:
      - name: pocfrontend
        image: pocfrontend:2.0       
        ports:
           - name: http
             containerPort: 3500      
---
# Service exposing frontend on node port 85
apiVersion: v1
kind: Service
metadata:
  name: frontend-service
spec:
  type: LoadBalancer
  selector:
    app: frontend
    tier: frontend
  ports:
  - protocol: TCP
    port: 85
    targetPort: http

Do you know how to solve my problem?

Thanks!


Solution

  • Natively Kubernetes itself does not provide session affinity on service [concept] level.

    The only way that comes to my mind is to use Istio and it's Destination Rules. Taken from the istio manual:

    DestinationRule defines policies that apply to traffic intended for a service after routing has occurred. These rules specify configuration for load balancing, connection pool size from the sidecar, and outlier detection settings to detect and evict unhealthy hosts from the load balancing pool.

    This document shows how to to configure sticky session with istio.