<p>I have a setup Metallb as LB with Nginx Ingress installed on K8S cluster.
I have read about session affinity and its significance but so far I do not have a clear picture.</p>

<p>How can I create a single service exposing multiple pods of the same application?
After creating the single service entry point, how to map the specific client IP to Pod abstracted by the service?</p>

<p>Is there any blog explaining this concept in terms of how the mapping between Client IP and POD is done in kubernetes?</p>

<p>But I do not see Client's IP in the YAML. Then, How is this service going to map the traffic to respective clients to its endpoints? this is the question I have.</p>

<pre><code>kind: Service
apiVersion: v1
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: 80
  sessionAffinity: ClientIP
  sessionAffinityConfig:
    clientIP:
      timeoutSeconds: 10000
</code></pre>

Pert8S
<p>Main concept of Session Affinity is to redirect traffic from one client always to specific node. Please keep in mind that session affinity is a <strong>best-effort</strong> method and there are scenarios where it will fail due to pod restarts or network errors.
There are two main types of Session Affinity: </p>

<p><strong>1)</strong> <em>Based on Client IP</em></p>

<p>This option works well for scenario where there is only one client per IP. In this method you don't need Ingress/Proxy between K8s services and client. 
Client IP should be static, because each time when client will change IP he will be redirected to another pod.</p>

<p>To enable the session affinity in kubernetes, we can add the following to the service definition.</p>

<pre><code>service.spec.sessionAffinity: ClientIP
</code></pre>

<p>Because community provided proper manifest to use this method I will not duplicate.</p>

<p><strong>2)</strong> <em>Based on Cookies</em></p>

<p>It works when there are multiple clients from the same IP, because it´s stored at web browser level. This method require Ingress object. Steps to apply this method with more detailed information can be found <a href="https://medium.com/@diegomrtnzg/redirect-your-users-to-the-same-pod-by-using-session-affinity-on-kubernetes-baebf6a1733b" rel="noreferrer">here</a> under <strong>Session affinity based on Cookie</strong> section.</p>

<ul>
<li>Create NGINX controller deployment</li>
<li>Create NGINX service</li>
<li>Create Ingress</li>
<li>Redirect your public DNS name to the NGINX service public/external IP.</li>
</ul>

<blockquote>
  <p>About mapping ClientIP and POD, according to <a href="https://kubernetes.io/docs/concepts/services-networking/service/#virtual-ips-and-service-proxies" rel="noreferrer">Documentation</a>
  kube-proxy is responsible for SessionAffinity. One of Kube-Proxy job
  is writing to IPtables, more details <a href="https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/#is-the-kube-proxy-working" rel="noreferrer">here</a> so thats how it is
  mapped.</p>
</blockquote>

<p>Articles which might help with understanding Session Affinity:
<a href="https://sookocheff.com/post/kubernetes/building-stateful-services/" rel="noreferrer">https://sookocheff.com/post/kubernetes/building-stateful-services/</a>
<a href="https://medium.com/@diegomrtnzg/redirect-your-users-to-the-same-pod-by-using-session-affinity-on-kubernetes-baebf6a1733b" rel="noreferrer">https://medium.com/@diegomrtnzg/redirect-your-users-to-the-same-pod-by-using-session-affinity-on-kubernetes-baebf6a1733b</a></p>
