Introduction
In this lab, you will learn how to scale and manage Pods with Deployments in Kubernetes. Deployments are a higher-level abstraction that allow you to declaratively manage and scale replica sets of Pods. By using Deployments, you can easily update your application to a new version, roll back to a previous version, and scale your application up or down to meet changing demand.
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.
Create a Deployment
- Create a file called
my-deployment.yamlin/home/labex/project/with the following content::
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: nginx:latest
ports:
- containerPort: 80
This YAML file defines a Deployment with 3 replicas, running an Nginx container. The selector field selects the Pods controlled by the Deployment based on the app label.
- Deploy the
my-deploymentDeployment:
kubectl apply -f my-deployment.yaml
This will create the my-deployment Deployment and its associated ReplicaSets and Pods.
- Verify that the Deployment has been created:
kubectl get deployments
This will show you the Deployments in your cluster, including the my-deployment Deployment.
Scale the Deployment
- Scale up the
my-deploymentDeployment to 5 replicas:
kubectl scale deployment my-deployment --replicas=5
This will increase the number of replicas in the my-deployment Deployment to 5.
- Verify that the Deployment has been scaled:
kubectl get deployments
This will show you the Deployments in your cluster, including the my-deployment Deployment with 5 replicas.
Update the Deployment
- Edit the
my-deploymentDeployment to use thenginx:1.19image:
kubectl edit deployment my-deployment
This will open the Deployment in your default text editor. Change the image field to nginx:1.19 and save the file.
- Verify that the Deployment has been updated:
kubectl rollout status deployment/my-deployment
This will show you the status of the latest rollout of the my-deployment Deployment.
Roll Back the Deployment
- Roll back the
my-deploymentDeployment to the previous version:
kubectl rollout undo deployment/my-deployment
This will roll back the my-deployment Deployment to the previous version.
- Verify that the Deployment has been rolled back:
kubectl rollout status deployment/my-deployment
This will show you the status of the latest rollout of the my-deployment Deployment.
Clean Up
- Delete the
my-deploymentDeployment:
kubectl delete deployment my-deployment
This will delete the my-deployment Deployment and its associated ReplicaSets and Pods.
Summary
In this lab, you learned how to create and manage Deployments in Kubernetes. You created a Deployment with 3 replicas, scaled the Deployment to 5 replicas, updated the Deployment to use a different image, rolled back the Deployment to the previous version,


