K8S pods with two replicas on different nodes

Viewed 1403

I've pods with two replicas, does it make sense that k8s will reschedule both replicas in the same time? if yes is there a way to avoid it ?

I guess(according to the replies from @Henry) that I need to use https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#affinity-and-anti-affinity or

topology https://kubernetes.io/blog/2020/05/introducing-podtopologyspread/

But not sure how to configure following:

1 application with 2 replicas that for example

Replica A runs on nodeFoo and

Replica B run in NodeBar

2 Answers

To configure the replicas to run on different nodes podAntiAffinity can be used. For example in the deployment spec:

spec:    
  template:
    metadata:
      labels:
        name: my-app-label
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchLabels:
                  name: my-app-label
              topologyKey: kubernetes.io/hostname

This basically means, all pods matched by the label name=my-app-label should run on hosts where the node label kubernetes.io/hostname is different.

Assuming you mean a Deployment, yes the Pod objects are created and scheduled roughly simultaneously. If you want things to be created in order, use a StatefulSet.

Related