<p>Let's say I have a service that maps to pod that has 2 containers, 1 expose port 8080, the other one expose port 8081. The service expose both ports. The ingress uses nginx-ingress, and has the cookie based session affinity annotations. It has 2 paths, 1 is <code>/</code> mapping to port 8080, the other one is <code>/static</code> mapping to port 8081 on the same service. Will the session affinity work in such way where all the requests from the same client will be sent to the same pod no matter if the path is <code>/</code> or <code>/static</code>?</p>
<p>Below are full configs:</p>
<p>Ingress</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    kubernetes.io/ingress.class: &quot;nginx&quot;
    nginx.ingress.kubernetes.io/affinity: &quot;cookie&quot;
    nginx.ingress.kubernetes.io/affinity-mode: &quot;persistent&quot;
    nginx.ingress.kubernetes.io/session-cookie-name: &quot;route&quot;
    nginx.ingress.kubernetes.io/session-cookie-expires: &quot;172800&quot;
    nginx.ingress.kubernetes.io/session-cookie-max-age: &quot;172800&quot;
spec:
  rules:
    - host: test.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: test-service
                port:
                  number: 8080
          - path: /static
            pathType: Prefix
            backend:
              service:
                name: test-service
                port:
                  number: 8081
</code></pre>
<p>Service</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: test-service
spec:
  type: ClusterIP
  selector:
    app: test-pod
  ports:
    - name: container1
      port: 8080
      targetPort: 8080
    - name: container2
      port: 8081
      targetPort: 8081
</code></pre>
<p>Deployment</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
...
spec:
  ...
  template:
    metadata:
      labels:
        app: test-pod
    spec:
      containers:
        - name: container1
          image: ...
          ports:
            - containerPort: 8080
        - name: container2
          image: ...
          ports:
            - containerPort: 8081
</code></pre>

user3908406
<p>I managed to test your configuration.</p>
<p>Actually this affinity annotation will work only for <code>/</code> path - this is how <code>nginx ingress</code> <a href="https://stackoverflow.com/questions/59272484/sticky-sessions-on-kubernetes-cluster/59360370#59360370">works</a> - to make affinity annotation work for both paths you need to create two ingress definitions:</p>
<p>Ingress for path <code>/</code>:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: test-ingress-one
  annotations:
    kubernetes.io/ingress.class: &quot;nginx&quot;
    nginx.ingress.kubernetes.io/affinity: &quot;cookie&quot;
    nginx.ingress.kubernetes.io/affinity-mode: &quot;balanced&quot;
    nginx.ingress.kubernetes.io/session-cookie-name: &quot;route-one&quot;
    nginx.ingress.kubernetes.io/session-cookie-expires: &quot;172800&quot;
    nginx.ingress.kubernetes.io/session-cookie-max-age: &quot;172800&quot;
spec:
  rules:
    - host: &lt;your-domain&gt;
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: test-service
                port:
                  number: 8080
</code></pre>
<p>Ingress for path <code>/static</code>:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: test-ingress-two
  annotations:
    kubernetes.io/ingress.class: &quot;nginx&quot;
    nginx.ingress.kubernetes.io/affinity: &quot;cookie&quot;
    nginx.ingress.kubernetes.io/affinity-mode: &quot;balanced&quot;
    nginx.ingress.kubernetes.io/session-cookie-name: &quot;route-two&quot;
    nginx.ingress.kubernetes.io/session-cookie-expires: &quot;172800&quot;
    nginx.ingress.kubernetes.io/session-cookie-max-age: &quot;172800&quot;
spec:
  rules:
    - host: &lt;your-domain&gt;
      http:
        paths:
          - path: /static
            pathType: Prefix
            backend:
              service:
                name: test-service
                port:
                  number: 8081
</code></pre>
<p>Back to your main question - as we are creating two different ingresses, with two different cookies, they are independent of each other.  Each of them will choose his &quot;pod&quot; to &quot;stick&quot; regardless of what the other has chosen. I did research I couldn't find any information about setting it a way to make it work you want.
Briefly answering your question:</p>
<blockquote>
<p>Will the session affinity work in such way where all the requests from the same client will be sent to the same pod no matter if the path is <code>/</code> or <code>/static</code>?</p>
</blockquote>
<p>No.</p>
