Introduction
In this lab, we will start by creating a simple deployment and then assign Node Selectors to it. We will then move on to more complex scenarios where we will use different selectors 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.
Creating a Simple Deployment
In this step, we will create a simple deployment with a single pod.
- Create a file named
simple-deployment.yamlwith the following contents:
apiVersion: apps/v1
kind: Deployment
metadata:
name: simple-deployment
spec:
replicas: 1
selector:
matchLabels:
app: simple-app
template:
metadata:
labels:
app: simple-app
spec:
containers:
- name: simple-container
image: nginx:latest
- Use
kubectlto create the deployment:
kubectl apply -f simple-deployment.yaml
- Verify that the deployment has been created:
kubectl get deployments
Assigning Node Selectors to a Deployment
In this step, we will assign a Node Selector to the deployment we created in Step 1.
- Create the nodes with a label:
kubectl label nodes minikube disk=ssd
- Edit the
node-selector-deployment.yamlfile and add thenodeSelectorfield under thespec.template.specsection:
apiVersion: apps/v1
kind: Deployment
metadata:
name: selector-deployment
spec:
replicas: 1
selector:
matchLabels:
app: selector-app
template:
metadata:
labels:
app: selector-app
spec:
nodeSelector:
disk: ssd
containers:
- name: selector-container
image: nginx:latest
- Use
kubectlto apply the changes:
kubectl apply -f node-selector-deployment.yaml
- Verify that the pod has been scheduled on a node with the label
disk=ssd:
kubectl get pods -o wide | grep selector-deployment
Using Different Node Selectors
In this step, we will use different Node Selectors to schedule pods on specific nodes based on the labels assigned to those nodes.
- Create three nodes with different labels:
kubectl label nodes minikube resigon=labex
kubectl label nodes minikube gpu=true
- Create a file named
multi-selector-deployment.yamlwith the following contents:
apiVersion: apps/v1
kind: Deployment
metadata:
name: multi-selector-deployment
spec:
replicas: 3
selector:
matchLabels:
app: multi-selector-app
template:
metadata:
labels:
app: multi-selector-app
spec:
containers:
- name: multi-selector-container
image: nginx:latest
nodeSelector:
resigon: labex
gpu: "true"
- Apply the changes:
kubectl apply -f multi-selector-deployment.yaml
- Verify that the pods have been scheduled on nodes with the appropriate labels:
kubectl get pods -o wide | grep multi-selector-deployment
Summary
In this lab, we learned how to schedule pods on specific nodes using Node Selectors and Node Affinity. We started with a simple deployment and then moved on to more complex scenarios where we used different selectors and affinity rules to schedule pods on specific nodes based on the labels assigned to those nodes.
Congratulations! You have successfully completed this lab.


