<p>I have one kubernetes cluster with 4 nodes and one master. I am trying to run 5 nginx pod in all nodes. Currently sometimes the scheduler runs all the pods in one machine and sometimes in different machine.</p>

<p>What happens if my node goes down and all my pods were running in same node? We need to avoid this.</p>

<p>How to enforce scheduler to run pods on the nodes in round-robin fashion, so that if any node goes down then at at least one node should have NGINX pod in running mode.</p>

<p>Is this possible or not? If possible, how can we achieve this scenario?</p>

<h1>Use podAntiAfinity</h1>

<p><strong>Reference: <a href="https://www.manning.com/books/kubernetes-in-action" rel="noreferrer">Kubernetes in Action Chapter 16. Advanced scheduling</a></strong></p>

<p>The podAntiAfinity with <strong>requiredDuringSchedulingIgnoredDuringExecution</strong> can be used to prevent the same pod from being scheduled to the same hostname. If prefer more relaxed constraint, use <strong>preferredDuringSchedulingIgnoredDuringExecution</strong>.</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 5
  template:
    metadata:
      labels:                                            
        app: nginx                                   
    spec:
      affinity:
        podAntiAffinity:                                 
          requiredDuringSchedulingIgnoredDuringExecution:   &lt;---- hard requirement not to schedule "nginx" pod if already one scheduled.
          - topologyKey: kubernetes.io/hostname     &lt;---- Anti affinity scope is host     
            labelSelector:                               
              matchLabels:                               
                app: nginx        
      container:
        image: nginx:latest
</code></pre>

<h1>Kubelet <a href="https://kubernetes.io/docs/reference/generated/kubelet/" rel="noreferrer">--max-pods</a></h1>

<p>You can specify the max number of pods for a node in kubelet configuration so that in the scenario of node(s) down, it will prevent K8S from saturating another nodes with pods from the failed node.</p>
