Introduction
The set command in Kubernetes is a versatile tool that allows you to modify the configuration of existing Kubernetes resources. This command enables adjustments such as updating container images and configuring resource requests and limits, all without directly editing YAML files. It simplifies resource management and enhances operational efficiency.
By the end of this lab, you will understand how to:
- Start a Kubernetes cluster.
- Update the container image of a deployment.
- Configure resource requests and limits for a deployment.
- Modify labels and annotations on a deployment using relevant commands.
This lab is designed for beginners and assumes you are working on an Ubuntu Linux system with Kubernetes tools installed. No prior experience with Kubernetes is required.
Start a Kubernetes Cluster
Before interacting with Kubernetes resources, ensure that a Kubernetes cluster is running. For this lab, we will use Minikube to set up a single-node Kubernetes cluster.
Open a terminal and start Minikube:
minikube startThis initializes a local Kubernetes cluster. Minikube automatically assigns appropriate resources, but you can customize them using flags like
--cpusand--memoryif necessary.Verify that Minikube is running:
kubectl cluster-infoEnsure the output confirms the cluster is operational.
Create a Deployment
Once the cluster is running, create a simple Kubernetes deployment named hello-world using the nginx image.
Run the following command to create the deployment:
kubectl create deployment hello-world --image=nginxThis command creates a deployment named
hello-worldwith one replica of thenginxcontainer.Verify that the deployment was successfully created:
kubectl get deploymentsCheck the output to ensure
hello-worldappears in the list of deployments.
Explore the kubectl set Command
The kubectl set command provides multiple subcommands to configure and modify application resources. It helps manage specific aspects like environment variables, container images, and resource settings.
Run the following command to view the available
kubectl setsubcommands:kubectl set -hYou will see the following output:
Configure application resources. These commands help you make changes to existing application resources. Available Commands: env Update environment variables on a pod template image Update the image of a pod template resources Update resource requests/limits on objects with pod templates selector Set the selector on a resource serviceaccount Update the service account of a resource subject Update the user, group, or service account in a role binding or cluster role binding Usage: kubectl set SUBCOMMAND [options] Use "kubectl --help" for more information about a given command. Use "kubectl options" for a list of global command-line options (applies to all commands).Review the available subcommands and their descriptions to understand how
kubectl setcan be used.Use
kubectl set --helpto explore additional details about each subcommand as needed.
Update the Container Image
Next, update the container image in the hello-world deployment to a specific version.
Use the
kubectl setcommand to update the container image tonginx:1.19.10:kubectl set image deployment/hello-world nginx=nginx:1.19.10This command updates the
nginxcontainer in thehello-worlddeployment.Verify the image update by querying the container image:
kubectl get deployment hello-world -o jsonpath='{.spec.template.spec.containers[0].image}'Ensure the output shows
nginx:1.19.10.
Configure Resource Requests and Limits
Resource management is essential for Kubernetes deployments. Set resource requests and limits for the hello-world deployment.
Configure CPU and memory requests and limits:
kubectl set resources deployment/hello-world --limits=cpu=1,memory=512Mi --requests=cpu=500m,memory=256MiThis command sets resource requests to
500mCPU and256Mimemory and limits to1CPU and512Mimemory.Verify the resource settings by describing the deployment:
kubectl describe deployment hello-worldCheck the
LimitsandRequestssections in the output to confirm the configuration.
Modify Labels on the Deployment
Labels help categorize and organize Kubernetes resources. Use the kubectl label command to add or modify labels on the deployment.
Add a label
environment=developmentto thehello-worlddeployment:kubectl label deployment hello-world environment=developmentThis command adds a new label to the deployment.
Verify that the label has been applied:
kubectl get deployment hello-world --show-labelsCheck the
LABELScolumn for theenvironment=developmentlabel.
Update Annotations on the Deployment
Annotations provide metadata to Kubernetes resources. Use the kubectl annotate command to add or update annotations on the deployment.
Add an annotation
owner=team-alphato thehello-worlddeployment:kubectl annotate deployment hello-world owner=team-alphaThis command adds an annotation to the deployment.
Verify that the annotation has been applied:
kubectl describe deployment hello-worldCheck the
Annotationssection forowner=team-alpha.
Summary
In this lab, you learned how to use the Kubernetes set command to manage deployments effectively. You:
- Started a Kubernetes cluster.
- Created a deployment.
- Updated the container image.
- Configured resource requests and limits.
- Modified labels and annotations using the appropriate commands.
These skills are essential for managing Kubernetes applications efficiently.


