Introduction
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.
Start the Minikube Cluster
Before creating resources, you need a running Kubernetes cluster. Minikube is a lightweight Kubernetes environment that runs on your local machine.
Navigate to your working directory:
Open the terminal and navigate to the default project folder:
cd /home/labex/projectStart Minikube:
Start Minikube to initialize a Kubernetes cluster:
minikube start- This command sets up a single-node Kubernetes cluster on your local machine.
- Minikube may take a few minutes to start depending on your system's performance.
Verify Minikube is running:
Check the status of the Minikube cluster:
minikube status- Look for components like
kubeletandapiserverlisted asRunning. - If the cluster is not running, rerun
minikube start.
- Look for components like
If you encounter issues starting Minikube. Use minikube delete to reset the environment if needed.
Run the remaining steps in this lab from /home/labex/project so the YAML files stay in the same working directory.
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.yamlwith the following contents in the/home/labex/projectdirectory:
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.yamlwith the following contents in the/home/labex/projectdirectory:
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=weblabel:
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.yamlwith the following contents in the/home/labex/projectdirectory:
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=dblabel:
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.yamlwith the following contents in the/home/labex/projectdirectory:
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=weblabel:
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.yamlwith the following contents in the/home/labex/projectdirectory:
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=webanddisktype=ssdlabels:
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.


