This content originally appeared on DEV Community and was authored by Labby
Introduction
This article covers the following tech skills:
In Kubernetes, node affinity is used to schedule pods on nodes that match certain conditions. This can be used to ensure that pods are scheduled on specific types of nodes, or to balance the workload across nodes. In this lab, we will learn how to use node affinity to schedule pods on specific nodes.
Creating a Node with Labels
In this step, we will create a node with a label that will be used to schedule pods.
- Create a file named
node-with-label.yaml
with the following contents in the/home/labex
directory:
apiVersion: v1
kind: Node
metadata:
name: minikube
labels:
type: web
- Apply the changes:
kubectl apply -f node-with-label.yaml
- Verify that the node has been created and labeled:
kubectl get nodes --show-labels
Creating a Pod with Node Affinity
In this step, we will create a pod with a node affinity rule that will ensure it is scheduled on a node with a specific label.
- Create a file named
pod-with-node-affinity.yaml
with the following contents in the/home/labex
directory:
apiVersion: v1
kind: Pod
metadata:
name: pod-with-node-affinity
spec:
containers:
- name: nginx
image: nginx:latest
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: type
operator: In
values:
- web
- Apply the changes:
kubectl apply -f pod-with-node-affinity.yaml
- Verify that the pod is scheduled on the node with the
type=web
label:
kubectl get pod pod-with-node-affinity -o wide
Creating a Pod with Node Anti-Affinity
In this step, we will create a pod with a node anti-affinity rule that will ensure it is not scheduled on a node with a specific label.
- Create a file named
pod-with-node-anti-affinity.yaml
with the following contents in the/home/labex
directory:
apiVersion: v1
kind: Pod
metadata:
name: pod-with-node-anti-affinity
spec:
containers:
- name: nginx
image: nginx:latest
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: type
operator: NotIn
values:
- web
- Apply the changes:
kubectl apply -f pod-with-node-anti-affinity.yaml
- Verify that the pod is not scheduled on the node with the
type=db
label:
kubectl get pod pod-with-node-anti-affinity -o wide
Creating a Pod with Node Affinity and Node Selector
In this step, we will create a pod with both a node affinity rule and a node selector that will ensure it is scheduled on a node with a specific label.
- Create a file named
pod-with-node-affinity-and-selector.yaml
with the following contents in the/home/labex
directory:
apiVersion: v1
kind: Pod
metadata:
name: pod-with-node-affinity-and-selector
spec:
containers:
- name: nginx
image: nginx:latest
nodeSelector:
type: web
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: type
operator: In
values:
- db
- Apply the changes:
kubectl apply -f pod-with-node-affinity-and-selector.yaml
- Verify that the pod is not scheduled on the node with the
type=web
label:
kubectl get pod pod-with-node-affinity-and-selector -o wide
Creating a Pod with Multiple Node Affinity Rules
In this step, we will create a pod with multiple node affinity rules that will ensure it is scheduled on a node with labels that match all of the rules.
- Create a file named
pod-with-multiple-node-affinity.yaml
with the following contents in the/home/labex
directory:
apiVersion: v1
kind: Pod
metadata:
name: pod-with-multiple-node-affinity
spec:
containers:
- name: nginx
image: nginx:latest
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: type
operator: In
values:
- web
- matchExpressions:
- key: disktype
operator: In
values:
- ssd
- Apply the changes:
kubectl apply -f pod-with-multiple-node-affinity.yaml
- Verify that the pod is scheduled on the node with the
type=web
anddisktype=ssd
labels:
kubectl get pod pod-with-multiple-node-affinity -o wide
Summary
In this lab, we learned how to use node affinity to schedule pods on specific nodes. We created a node with a label, and then created pods with node affinity rules that ensured they were scheduled on nodes with specific labels. We also created a pod with a node anti-affinity rule that ensured it was not scheduled on a node with a specific label. Finally, we created a pod with multiple node affinity rules that ensured it was scheduled on a node with labels that matched all of the rules.
🚀 Practice Now: Scheduing with Node Affinity
Want to Learn More?
- 🌳 Learn the latest Kubernetes Skill Trees
- đź“– Read More Kubernetes Tutorials
- đź’¬ Join our Discord or tweet us @WeAreLabEx
This content originally appeared on DEV Community and was authored by Labby
Labby | Sciencx (2024-07-18T03:26:43+00:00) Scheduling Pods with Node Affinity in Kubernetes. Retrieved from https://www.scien.cx/2024/07/18/scheduling-pods-with-node-affinity-in-kubernetes/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.