<p>I have a question about how kubernetes decides the serving pod when there are several replicas of the pod.</p>

<p>For Instance, let's assume I have a web application running on a k8s cluster as multiple pod replicas and they are exposed by a service. </p>

<p>When a client sends a request it goes to service and kube-proxy. But where and when does kubernetes make a decision about which pod should serve the request?</p>

<p>I want to know the internals of kubernetes for this matter. Can we control this? Can we decide which pod should serve based on client requests and custom conditions?</p>

<blockquote>
  <p>can we decide which pod should serve based on client requests and custom conditions?</p>
</blockquote>

<p>As kube-proxy works on <strong>L4</strong> load balancing stuff thus you can control the session based on  <strong>Client IP</strong>.  it does not  read the header of client request. </p>

<p>you can control the session with the following field <strong>service.spec.sessionAffinityConfig</strong> in <strong>service obejct</strong> </p>

<p>following command provide the explanation 
<code>kubectl explain service.spec.sessionAffinityConfig</code></p>

<p>Following paragraph and link provide detail answer.</p>

<blockquote>
  <p>Client-IP based session affinity can be selected by setting service.spec.sessionAffinity to “ClientIP” (the default is “None”), and you can set the max session sticky time by setting the field service.spec.sessionAffinityConfig.clientIP.timeoutSeconds if you have already set service.spec.sessionAffinity to “ClientIP” (the default is “10800”)<a href="https://kubernetes.io/docs/concepts/services-networking/service/#virtual-ips-and-service-proxies" rel="nofollow noreferrer">-service-proxies</a></p>
</blockquote>

<p>Service object would be like this</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>
