Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes how to spread pods to nodes but with preferredDuringSchedulingIgnoredDuringExecution

Tags:

kubernetes

I want that my api deployment pods will be spread to the whole cluster's nodes. So I came up with this:

spec:
  affinity:
    podAntiAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: app
            operator: In
            values:
            - api
        topologyKey: "kubernetes.io/hostname"

But this allows exactly one pod in each node, and no more. My problem is when I want to rollout an update, kubernetes remains the new creating pod under "pending" state.

How can I change the requiredDuringSchedulingIgnoredDuringExecution to preferredDuringSchedulingIgnoredDuringExecution?

I have tried, but I got many errors since the preferredDuringSchedulingIgnoredDuringExecution probably requires different configurations from the requiredDuringSchedulingIgnoredDuringExecution.

like image 425
Raz Buchnik Avatar asked Oct 23 '25 00:10

Raz Buchnik


1 Answers

This is the right implementation:

  affinity:
    podAntiAffinity:
      preferredDuringSchedulingIgnoredDuringExecution: 
      - weight: 100  
        podAffinityTerm:
          labelSelector:
            matchExpressions:
            - key: app 
              operator: In 
              values:
              - api
          topologyKey: kubernetes.io/hostname

This will spread the pods evenly in the nodes and will allow more than one in each node. So basically you can deploy 6 replicas to cluster of 3 nodes without a problem. Also, you can rollout an update even though it creates a new extra pod before shutting down the old one.

like image 100
Raz Buchnik Avatar answered Oct 24 '25 15:10

Raz Buchnik