Assigning Pods to Nodes Utilizing Affinity Guidelines – DZone – Uplaza

This text describes find out how to configure your Pods to run in particular nodes primarily based on affinity and anti-affinity guidelines. Affinity and anti-affinity permit you to inform the Kubernetes Scheduler whether or not to assign or not assign your Pods, which can assist optimize efficiency, reliability, and compliance.

There are two forms of affinity and anti-affinity, as per the Kubernetes documentation:

  • requiredDuringSchedulingIgnoredDuringExecution: The scheduler cannot schedule the Pod until the rule is met. This capabilities like nodeSelector, however with a extra expressive syntax.
  • preferredDuringSchedulingIgnoredDuringExecution: The scheduler tries to discover a node that meets the rule. If an identical node will not be obtainable, the scheduler nonetheless schedules the Pod.

Let’s examine a few situations the place you should utilize this configuration.

Situation 1: Kafka Cluster With a Pod in a Totally different K8s Employee Node

On this situation, I am working a Kafka cluster with 3 nodes (Pods). For resilience and excessive availability, I need to have every Kafka node working in a special employee node. 

On this case, Kafka is deployed as a StatefulSet, so the affinity is configured within the .template.spec.affinity discipline:

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

Within the configuration above, I am utilizing podAntiAffinity as a result of it permits us to create guidelines primarily based on labels on Pods and never solely within the node itself. Along with that, I am setting the requiredDuringSchedulingIgnoredDuringExecution as a result of I do not need two Kafka Pods working in the identical cluster at any time. The labelSelector discipline search for the label app=kafka within the Pod and topologyKey is the label node. Pod anti-affinity requires nodes to be constantly labeled, in different phrases, each node within the cluster will need to have an acceptable label matching topologyKey.

Within the occasion of a failure in one of many employees (contemplating that we solely have three employees), the Kafka Pod will probably be in a Pending standing as a result of the opposite two nodes have already got a Kafka node working. 

If we alter the sort from requiredDuringSchedulingIgnoredDuringExecution to preferredDuringSchedulingIgnoredDuringExecution, then kafka-2 could be assigned to a different employee.

Situation 2: Knowledge Science Purposes That Should Run on Particular Nodes

Conceptually, node affinity is just like nodeSelector the place you outline the place a Pod will run. Nonetheless, affinity provides us extra flexibility. To illustrate that in our cluster we have now two employee nodes with GPU processors and a few of our purposes should run in considered one of these nodes.

Within the Pod configuration we configure the .spec.affinity discipline:

spec:
  affinity:
    nodeAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        nodeSelectorTerms:
        - matchExpressions:
          - key: processor
            operator: In
            values:
            - gpu-east1
            - gpu-west1

On this instance, the next guidelines apply:

On this situation, the Knowledge Science purposes will probably be assigned to employees 1 and a pair of. Employee 3 won’t ever host a Knowledge Science software.

Abstract

Affinity and anti-affinity guidelines present us flexibility and management on the place to run our purposes in Kubernetes. It is an vital characteristic to create a extremely obtainable and resilient platform. There are extra options, like weight, that you may examine within the official Kubernetes documentation.

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Exit mobile version