<p>With <code>node_selector</code> we can schedule a particular deployment replicas to a certain node pool. But how to make sure that at least one pod is running in every node (say size of node pool is more than 1)</p>

<p>I need this to ensure my pods are spread across the node pool, so that if a particular node face an issue (say disconnected from cluster) my application would still run.</p>

Jawahar
<p>With <code>nodeSelector</code> you can directly tie a Pod to a node, but it doesn't provide any means for spreading the Pods of a Deployment across the nodes.</p>

<p>To spread Pods across the nodes, you can use <a href="https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#inter-pod-affinity-and-anti-affinity" rel="nofollow noreferrer">Pod anti-affinity</a>.</p>

<p>For example:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  selector:
    matchLabels:
      app: my-app
  replicas: 3
  template:
    metadata:
      labels:
        app: my-app
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - my-app
            topologyKey: "kubernetes.io/hostname"
      containers:
      - name: my-app
        image: my-app:1.0.0
</code></pre>

<p>This schedules the Pods so that no two Pods of the Deployment are located on the same node, if possible.</p>

<p>For example, if you have 5 nodes and 3 replicas in the Deployment, then each Pod should be scheduled to a different node. If you have 5 nodes and 6 replicas, then the first 5 Pods should be scheduled to a different node each and the 6th Pod is scheduled to a node which already already has Pod (because there's no other possibility).</p>

<p>See more examples in the <a href="https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#always-co-located-in-the-same-node" rel="nofollow noreferrer">Kubernetes documentation</a>.</p>
