<p>I have an ingress resource like the following:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: &quot;app-ingress&quot;
  labels:
    app: &quot;app&quot;
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/backend-protocol: &quot;GRPC&quot;
    cert-manager.io/cluster-issuer: letsencrypt
spec:
  tls:
    - hosts:
        - api.example.com
      secretName: tls-secret
  rules:
    - host: api.example.com
      http:
        paths:
          - path: /grpc
            pathType: ImplementationSpecific
            backend:
              service:
                name: &quot;app-server&quot;
                port:
                  number: 50051
          - path: /callback
            pathType: ImplementationSpecific
            backend:
              service:
                name: &quot;app-server&quot;
                port:
                  number: 80
</code></pre>
<p><code>app-server</code> runs two services (on the same pod), a <code>grpc</code> web server and a <code>http</code> web server. To start, a client makes a request to <code>api.example.com/grpc</code>. To fulfill the request, the server makes a request to a third party service, waits for a callback on <code>api.example.com/callback</code>, and then completes the client's request (<code>api.example.com/callback</code> is only accessed by the third party service, not the client).</p>
<p>The question is, how can I specifically route <code>api.example.com/callback</code> back to the same pod that made the initial request? Obviously if <code>pod1</code> were to make a request to the third party service and <code>pod2</code> were to receive the callback, <code>pod1</code> would never receive the callback and the client request would have to timeout. I can attach information to the callback I receive from the third party service, so is there a way I could attach some sort of pod id so that kubernetes will route the callback back to the pod that initiated it?</p>
<p>I've looked at maybe using <code>StatefulSets</code>, but I'm not sure exactly how this would work. Or would it be better to have sort of &quot;notification pod&quot; that receives callbacks and routes them to the correct pod?</p>

<p>One way would be to user <a href="https://kubernetes.github.io/ingress-nginx/examples/affinity/cookie/" rel="nofollow noreferrer">sticky sessons</a>.<br />
If you want to make sure that connections from a particular client are passed to the same Pod each time, you can select the session affinity based on the client's IP addresses by setting <code>service.spec.sessionAffinity</code> to &quot;ClientIP&quot; (the default is &quot;None&quot;). You can also set the maximum session sticky time by setting <code>service.spec.sessionAffinityConfig.clientIP.timeoutSeconds</code> appropriately (the default value is 10800, which works out to be 3 hours).</p>
<p>Another way is to design the Application in away that it doesn't matter which pod receives callback request, the proper pod with the pending client connection will be notified (and provided with data), and send a response back to the client. To communicate requests data you can use some sort of messaging service or distributed in-memory data stores like Redis.</p>
