<p>In my headless service, I configure sessionAffinity so that <strong>connections from a particular client are passed to the same Pod each time</strong> <a href="https://kubernetes.io/docs/concepts/services-networking/service/#virtual-ips-and-service-proxies" rel="nofollow noreferrer">as described here</a></p>
<p>Here is the manifest :</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: service1
spec:
  clusterIP: None
  selector:
    app: nginx
  sessionAffinity: ClientIP
  sessionAffinityConfig:
    clientIP:
      timeoutSeconds: 30
</code></pre>
<p>I run some nginx pods to test :</p>
<pre><code>$ kubectl create deployment nginx --image=stenote/nginx-hostname
</code></pre>
<p>The problem is that when I curl my service, I am redirected to different pods and sessionAffinity seems to be ignored.</p>
<pre><code>$ kubectl run --generator=run-pod/v1 --rm utils -it --image arunvelsriram/utils bash
root@utils:/# for i in $(seq 1 10) ; do curl service1; done
nginx-78d58889-b7fm2
nginx-78d58889-b7fm2
nginx-78d58889-b7fm2
nginx-78d58889-b7fm2
nginx-78d58889-b7fm2
nginx-78d58889-8rpxd
nginx-78d58889-b7fm2
nginx-78d58889-62jlw
nginx-78d58889-8rpxd
nginx-78d58889-62jlw
</code></pre>
<p>NB. When I check with</p>
<pre><code>$ kubectl describe  svc service1
Name:              service1
Namespace:         abdelghani
Labels:            &lt;none&gt;
Annotations:       &lt;none&gt;
Selector:          app=nginx
Type:              ClusterIP
IP Families:       &lt;none&gt;
IP:                None
IPs:               &lt;none&gt;
Session Affinity:  ClientIP
Events:            &lt;none&gt;
</code></pre>
<p><code>SessionAffinity</code> configuration is present.</p>
<p>Note that my service is headless i.e. <code>clusterIP: None</code>. SessionAffinity seems to work fine with non-headless services. But, I cant find a clear explanation in the documentation. Is this related to the platform not doing any proxying?</p>
<p>Abdelghani</p>

Abdelghani
<p>When using headless service (clusterIP: None) you don't use proxy.</p>
<p>From <a href="https://kubernetes.io/docs/concepts/services-networking/service/#headless-services" rel="nofollow noreferrer">k8s docs</a>:</p>
<blockquote>
<p>For headless Services, a cluster IP is not allocated, kube-proxy does
not handle these Services, and there is no load balancing or proxying
done by the platform for them. How DNS is automatically configured
depends on whether the Service has selectors defined</p>
</blockquote>
<p>So when using headless service, dns responds with randomized list of ips of all pods associated with given service.</p>
<pre><code>/app # dig service1 +search +short
172.17.0.8
172.17.0.10
172.17.0.9
/app # dig service1 +search +short
172.17.0.9
172.17.0.10
172.17.0.8
/app # dig service1 +search +short
172.17.0.8
172.17.0.10
172.17.0.9
/app # dig service1 +search +short
172.17.0.10
172.17.0.9
172.17.0.8
/app # dig service1 +search +short
172.17.0.9
172.17.0.8
172.17.0.10
</code></pre>
<p>and curl just gets one and goes with it.</p>
<p>Since this hapens every request, every time you get different ip from dns, you connect to different pod.</p>
