<p>What is the logic algorithm that a kubernets service uses to assign requests to pods that it exposes? Can this algorithm be customized?</p>
<p>Thanks.</p>

Mazen Ezzeddine
<p>You can use a component <a href="https://kubernetes.io/docs/concepts/overview/components/#kube-proxy" rel="nofollow noreferrer"><code>kube-proxy</code></a>. What is it?</p>
<blockquote>
<p>kube-proxy is a network proxy that runs on each  <a href="https://kubernetes.io/docs/concepts/architecture/nodes/" rel="nofollow noreferrer">node</a>  in your cluster, implementing part of the Kubernetes  <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">Service</a>  concept.
<a href="https://kubernetes.io/docs/reference/command-line-tools-reference/kube-proxy/" rel="nofollow noreferrer">kube-proxy</a>  maintains network rules on nodes. These network rules allow network communication to your Pods from network sessions inside or outside of your cluster.
kube-proxy uses the operating system packet filtering layer if there is one and it's available. Otherwise, kube-proxy forwards the traffic itself.</p>
</blockquote>
<p>But why use a proxy when there is a <a href="https://kubernetes.io/docs/concepts/services-networking/service/#why-not-use-round-robin-dns" rel="nofollow noreferrer">round-robin DNS algorithm</a>? There are a few reasons for using proxying for Services:</p>
<blockquote>
<ul>
<li>There is a long history of DNS implementations not respecting record TTLs, and caching the results of name lookups after they should have expired.</li>
<li>Some apps do DNS lookups only once and cache the results indefinitely.</li>
<li>Even if apps and libraries did proper re-resolution, the low or zero TTLs on the DNS records could impose a high load on DNS that then becomes difficult to manage.</li>
</ul>
</blockquote>
<p><code>kube-proxy</code> has many modes:</p>
<ul>
<li><a href="https://kubernetes.io/docs/concepts/services-networking/service/#proxy-mode-userspace" rel="nofollow noreferrer"><strong>User space proxy mode</strong></a> - In the userspace mode, the iptables rule forwards to a local port where a go binary (kube-proxy) is listening for connections. The binary (running in userspace) terminates the connection, establishes a new connection to a backend for the service, and then forwards requests to the backend and responses back to the local process. An advantage of the userspace mode is that because the connections are created from an application, if the connection is refused, the application can retry to a different backend</li>
<li><a href="https://kubernetes.io/docs/concepts/services-networking/service/#proxy-mode-iptables" rel="nofollow noreferrer"><strong>Iptables proxy mode</strong></a> - In iptables mode, the iptables rules are installed to directly forward packets that are destined for a service to a backend for the service. This is more efficient than moving the packets from the kernel to kube-proxy and then back to the kernel so it results in higher throughput and better tail latency. The main downside is that it is more difficult to debug, because instead of a local binary that writes a log to <code>/var/log/kube-proxy</code> you have to inspect logs from the kernel processing iptables rules.</li>
<li><a href="https://kubernetes.io/docs/concepts/services-networking/service/#proxy-mode-ipvs" rel="nofollow noreferrer"><strong>IPVS proxy mode</strong></a> - IPVS is a Linux kernel feature that is specifically designed for load balancing. In IPVS mode, kube-proxy programs the IPVS load balancer instead of using iptables. This works, it also uses a mature kernel feature and IPVS is <em>designed</em> for load balancing lots of services; it has an optimized API and an optimized look-up routine rather than a list of sequential rules.</li>
</ul>
<p>You can read more <a href="https://stackoverflow.com/questions/36088224/what-does-userspace-mode-means-in-kube-proxys-proxy-mode">here</a> - good question about proxy mode on StackOverflow, <a href="https://www.tigera.io/blog/comparing-kube-proxy-modes-iptables-or-ipvs/" rel="nofollow noreferrer">here</a> - comparing proxy modes and <a href="https://www.stackrox.com/post/2020/01/kubernetes-networking-demystified/" rel="nofollow noreferrer">here</a> - good article about proxy modes.</p>
<hr />
<p>Like <a href="https://stackoverflow.com/users/4551228/rohatgisanat">rohatgisanat</a> mentioned in his answer you can also use <a href="https://www.redhat.com/en/topics/microservices/what-is-a-service-mesh" rel="nofollow noreferrer">service mesh</a>. Here is also good article about Kubernetes service mesh <a href="https://www.toptal.com/kubernetes/service-mesh-comparison" rel="nofollow noreferrer">comparsion</a>.</p>
