0
0
Kubernetesdevops~30 mins

Node affinity and anti-affinity in Kubernetes - Mini Project: Build & Apply

Choose your learning style9 modes available
Node affinity and anti-affinity
📖 Scenario: You are managing a Kubernetes cluster for a small company. You want to control where your application pods run by using node affinity and anti-affinity rules. This helps keep your app stable and balanced across the cluster.
🎯 Goal: Learn how to write a Kubernetes pod specification that uses node affinity and anti-affinity to schedule pods on specific nodes or avoid certain nodes.
📋 What You'll Learn
Create a pod manifest with a basic container
Add node affinity rules to prefer nodes with a specific label
Add node anti-affinity rules to avoid nodes with another label
Display the final pod manifest YAML
💡 Why This Matters
🌍 Real World
Node affinity and anti-affinity help control where pods run in a Kubernetes cluster. This is useful to improve performance, reliability, and resource use by placing pods on suitable nodes or avoiding problematic ones.
💼 Career
Understanding node affinity and anti-affinity is important for Kubernetes administrators and DevOps engineers to manage cluster workloads effectively and ensure application stability.
Progress0 / 4 steps
1
Create a basic pod manifest
Create a Kubernetes pod manifest named pod.yaml with a pod named myapp-pod that runs the container image nginx:latest. Include the apiVersion, kind, metadata, and spec sections with the container definition.
Kubernetes
Need a hint?

Start with the basic pod YAML structure. Use apiVersion: v1 and kind: Pod. Add metadata with the pod name and a spec with one container running nginx.

2
Add node affinity to prefer nodes with label
Add a nodeAffinity section under spec to prefer scheduling the pod on nodes with the label disktype: ssd. Use preferredDuringSchedulingIgnoredDuringExecution with a weight of 1 and a match expression for the label key disktype and value ssd.
Kubernetes
Need a hint?

Under spec, add affinity with nodeAffinity. Use preferredDuringSchedulingIgnoredDuringExecution with a weight and a match expression for the label disktype: ssd.

3
Add node anti-affinity to avoid nodes with label
Add a nodeAffinity requiredDuringSchedulingIgnoredDuringExecution rule to avoid scheduling the pod on nodes with the label zone: east. Use a matchExpressions with key zone, operator NotIn, and value east.
Kubernetes
Need a hint?

Under nodeAffinity, add requiredDuringSchedulingIgnoredDuringExecution with a nodeSelectorTerms list. Use a matchExpressions to exclude nodes labeled zone: east.

4
Display the final pod manifest
Print the complete pod manifest YAML to the console exactly as written in the previous steps.
Kubernetes
Need a hint?

Use a print statement to output the full pod manifest YAML exactly as defined.