<p>I'm trying to set up an ingress controller in Kubernetes that will give me strict alternation between two (or more) pods running in the same service.</p>
<p>My testing setup is a single Kubernetes node, with a deployment of two nginx pods.
The deployment is then exposed with a NodePort service.</p>
<p>I've then deployed an ingress contoller (I've tried both <a href="https://github.com/kubernetes/ingress-nginx" rel="nofollow noreferrer">Kubernetes Nginx Ingress Controller</a> and <a href="https://github.com/nginxinc/kubernetes-ingress" rel="nofollow noreferrer">Nginx Kubernetes Ingress Controller</a>, separately) and created an ingress rule for the NodePort service.</p>
<p>I edited index.html on each of the nginx pods, so that one shows &quot;SERVER A&quot; and the other &quot;SERVER B&quot;, and ran a script that then <code>curl</code>s the NodePort service 100 times. It <code>grep</code>s &quot;SERVER x&quot; each time, appends it to an output file, and then tallies the number of each at the end.</p>
<p>As expected, curling the NodePort service itself (which uses kube-proxy), I got completely random results-- anything from 50:50 to 80:20 splits between the pods.</p>
<p>Curling the ingress controller, I consistently get something between 50:50 and 49:51 splits, which is great-- the default round-robin distribution is working well.</p>
<p><strong>However</strong>, looking at the results, I can see that I've curled the same server up to 4 times in a row, but I need to enforce a strict alternation A-B-A-B. I've spent quite a researching this and trying out different options, but I can't find a setting that will do this. Does anyone have any advice, please?</p>
<p>I'd prefer to stick with one of the ingress controllers I've tried, but I'm open to trying a different one, if it will do what I need.</p>

<p>Nginx default behavior is like <strong>round-robin</strong> only. You can use it to perform most tests on Nginx ingress with different config <strong>tweaks</strong> if required.</p>
<p>There is also other options like you can use the <strong>Istio</strong> <strong>service mesh</strong>.</p>
<p>You can Load balance the traffic as you required by changing the config only</p>
<pre><code>apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: bookinfo-ratings
spec:
  host: ratings.prod.svc.cluster.local
  trafficPolicy:
    loadBalancer:
      simple: LEAST_CONN
  subsets:
  - name: testversion
    labels:
      version: v3
    trafficPolicy:
      loadBalancer:
        simple: ROUND_ROBIN
</code></pre>
<p>Read more at : <a href="https://istio.io/latest/docs/reference/config/networking/destination-rule/" rel="nofollow noreferrer">https://istio.io/latest/docs/reference/config/networking/destination-rule/</a></p>
<p>&amp; <a href="https://istio.io/latest/docs/reference/config/networking/destination-rule/#LoadBalancerSettings" rel="nofollow noreferrer">https://istio.io/latest/docs/reference/config/networking/destination-rule/#LoadBalancerSettings</a></p>
<p>however, i would suggest going with <code>service mesh</code> only when there is a large cluster implementing for 2-3 services better use the <strong>Nginx</strong> <strong>ingress</strong> or <strong>haproxy-ingress</strong> also good option.</p>
