Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
Node Affinity in Kubernetes provides a powerful way to influence pod scheduling based on node attributes. In this hands-on guide, we’ll explore Node Affinity through a series of practical exercises. Let’s dive in!
1. Creating a Pod with Node Affinity#
First, let’s create a pod with a Node Affinity rule that requires nodes with disktype=ssd:
1apiVersion: v12kind: Pod3metadata:4 name: nginx-ssd5spec:6 containers:7 - name: nginx8 image: nginx9 affinity:10 nodeAffinity:11 requiredDuringSchedulingIgnoredDuringExecution:12 nodeSelectorTerms:13 - matchExpressions:14 - key: disktype15 operator: In16 values:17 - ssdSave this as nginx-ssd.yaml and apply it:
1kubectl apply -f nginx-ssd.yaml2. Checking Pod Status#
Let’s check the status of our pod:
1kubectl get podsYou’ll notice that the pod is in a “Pending” state. Let’s investigate why:
1kubectl describe pod nginx-ssdIn the events section, you should see messages indicating that no nodes match the affinity requirements.
3. Adding Label to Worker Node#
Now, let’s add the required label to our worker01 node:
1kubectl label nodes worker01 disktype=ssdCheck the pod status again:
1kubectl get pods -o wideYou should now see that the nginx-ssd pod is scheduled on worker01.
4. Creating a Pod with Existence-based Node Affinity#
Let’s create another pod, this time with a Node Affinity rule that only checks for the existence of the disktype label, without specifying a value:
1apiVersion: v12kind: Pod3metadata:4 name: redis-disktype5spec:6 containers:7 - name: redis8 image: redis9 affinity:10 nodeAffinity:11 requiredDuringSchedulingIgnoredDuringExecution:12 nodeSelectorTerms:13 - matchExpressions:14 - key: disktype15 operator: ExistsSave this as redis-disktype.yaml and apply it:
1kubectl apply -f redis-disktype.yaml5. Adding Label to Second Worker Node#
Now, let’s add the disktype label to worker02, but without specifying a value:
1kubectl label nodes worker02 disktype=6. Verifying Pod Scheduling#
Check the status of both pods:
1kubectl get pods -o wideYou should see that:
- The
nginx-ssdpod is scheduled on worker01 (which hasdisktype=ssd) - The
redis-disktypepod is scheduled on worker02 (which has thedisktypelabel without a value)
Key Takeaways#
- Precise Control: Node Affinity allows you to define specific rules for pod placement based on node labels.
- Flexibility: You can require exact label matches (
Inoperator) or just the existence of a label (Existsoperator). - Scheduling vs. Execution: The
requiredDuringSchedulingIgnoredDuringExecutionproperty ensures the rule is enforced during scheduling but doesn’t affect running pods if node labels change. - Label Management: Properly labeling your nodes is crucial for effective use of Node Affinity.
Conclusion#
Node Affinity provides a powerful tool for controlling pod placement in your Kubernetes cluster. By using Node Affinity, you can ensure that pods are scheduled on nodes with the right characteristics for their workloads, whether that’s specific hardware, geographical location, or any other attribute you can represent with labels.
As you continue to work with Kubernetes, experiment with different Node Affinity rules and combinations of node labels. This will help you optimize your cluster resource utilization and ensure that your applications are running on the most suitable nodes.
Remember, while Node Affinity gives you fine-grained control over pod scheduling, it’s important to use it judiciously. Overly complex affinity rules can make your cluster harder to manage and potentially lead to scheduling conflicts. Always strive for a balance between control and simplicity in your Kubernetes configurations.
Happy Kuberneting!