<p>Here is my problem: I have 3 services defined in a kubernetes yaml file:</p>

<ul>
<li>one front-end (website)</li>
<li>one back-end : stateful, for user sessions</li>
<li>one back-end : stateless</li>
</ul>

<p>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.</p>

<pre><code>mydomain/stateful  ===&gt; Front-End Service (3 pods) ===&gt; Stateful Service (3 pods, need session affinity)
mydomain/stateless ===&gt; Front-End Service (3 pods) ===&gt; Stateless Service (3 pods, do not need session affinity)
</code></pre>

<p>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.</p>

<p>Here is my poc.yaml so far:</p>

<pre><code>####################################################################
######################### 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
</code></pre>

<p>Do you know how to solve my problem?</p>

<p>Thanks!</p>

<p>Natively Kubernetes itself does not provide <code>session affinity</code> on service [concept] level. </p>

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

<blockquote>
  <p><code>DestinationRule</code> 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.</p>
</blockquote>

<p><a href="https://dev.to/peterj/what-are-sticky-sessions-and-how-to-configure-them-with-istio-1e1a" rel="nofollow noreferrer">This document</a> shows how to to configure <code>sticky session</code> with istio. </p>
