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.
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.
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/project
Start Minikube:
Start Minikube to initialize a Kubernetes cluster:
minikube start
Verify Minikube is running:
Check the status of the Minikube cluster:
minikube status
kubelet
and apiserver
listed as Running
.minikube start
.If you encounter issues starting Minikube. Use minikube delete
to reset the environment if needed.
In this step, we will create a simple deployment with a single pod.
simple-deployment.yaml
with 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
kubectl
to create the deployment:kubectl apply -f simple-deployment.yaml
kubectl get deployments
In this step, we will assign a Node Selector to the deployment we created in Step 1.
kubectl label nodes minikube disk=ssd
node-selector-deployment.yaml
file and add the nodeSelector
field under the spec.template.spec
section: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
kubectl
to apply the changes:kubectl apply -f node-selector-deployment.yaml
disk=ssd
:kubectl get pods -o wide | grep selector-deployment
In this step, we will use different Node Selectors to schedule pods on specific nodes based on the labels assigned to those nodes.
kubectl label nodes minikube resigon=labex
kubectl label nodes minikube gpu=true
multi-selector-deployment.yaml
with 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"
kubectl apply -f multi-selector-deployment.yaml
kubectl get pods -o wide | grep multi-selector-deployment
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.