Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

K8S pods with two replicas on different nodes

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

like image 923
Jenny M Avatar asked Nov 15 '20 09:11

Jenny M


People also ask

Can a Kubernetes pod run on multiple nodes?

A Node can have multiple pods, and the Kubernetes control plane automatically handles scheduling the pods across the Nodes in the cluster. The control plane's automatic scheduling takes into account the available resources on each Node.

Can we run containers of same pod on different nodes?

In a pre-container world, they would have run on the same physical or virtual machine. Pods are tied to the Node where they are deployed and remain there until termination (according to restart policy) or deletion. In case of a Node failure, new identical Pods will be deployed on other available Nodes.

Can pods in different nodes communicate?

Pods on a node can communicate with all pods on all nodes without NAT. Agents on a node (system daemons, kubelet) can communicate with all the pods on that specific node.

How many replicas do I need Kubernetes?

Number of replicas. You need at least two replicas for the application to be considered minimally available.


Video Answer


1 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.

like image 162
Henry Avatar answered Oct 28 '22 18:10

Henry