Kubernetes how to spread pods to nodes but with preferredDuringSchedulingIgnoredDuringExecution

Viewed 43

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.

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.

Related