How to Edit a Kubernetes Deployment
Looking to edit a Kubernetes deployment? Use kubectl edit for quick changes, kubectl patch for controlled modifications, or kubectl set for specific updates.
β LHB Community
![Warp Terminal](/assets/images/warp-terminal.webp)
Kubernetes deployments are essential components that manage the state of your application by ensuring that a specified number of pod replicas are running at any given time.
Editing these deployments is a common task, especially when you need to adjust configurations such as environment variables, image versions, or resource limits.
While you can modify the YAML file directly, Kubernetes provides a more dynamic approach to editing deployments without the need to modify the file manually.
In this guide, weβll explore how to accomplish this using kubectl, the Kubernetes command-line tool.
Prerequisites
Before we begin, ensure that you have the following:
- A Kubernetes cluster running with kubectl configured to interact with it.
- A deployment already running in your cluster. If you don't have one, you can create a simple NGINX deployment:
kubectl create deployment nginx-deployment --image=nginx
Method 1: Editing a deployment with kubectl edit
The kubectl edit
command allows you to open the deployment's configuration in a text editor, make changes, and apply them directly to the running deployment.
This method is quick and convenient when you need to make small adjustments.
Run the kubectl edit command.
kubectl edit deployment nginx-deployment
This command opens the deployment configuration in your default text editor (usually vim or nano). You can modify any aspect of the deployment, such as changing the Nginx image version.
spec:
containers:
- image: nginx:1.21.6
Once you save and exit the editor, Kubernetes automatically applies the changes to the deployment. The new configuration will take effect, and Kubernetes will update the pods accordingly.
Using kubectl patch
For more controlled and scriptable edits, kubectl patch
allows you to make partial changes to a deployment without editing the entire YAML file. This is particularly useful for automation or when integrating with CI/CD pipelines.
You can update the image of the deployment by using a strategic merge patch:
kubectl patch deployment nginx-deployment --patch '{"spec": {"template": {"spec": {"containers": [{"name": "nginx", "image": "nginx:1.21.6"}]}}}}'
You can also use the kubectl patch
command to change the number of replicas:
kubectl patch deployment nginx-deployment --patch '{"spec": {"replicas": 5}}'
This command updates the nginx-deployment
in Kubernetes to scale its replicas to 5
.
Using kubectl set commands
Kubernetes provides the kubectl set
family of commands, which are designed for specific tasks, such as updating image versions or modifying environment variables.
To update the container image, run the kubectl set image
command and specify your desired Nginx container image.
kubectl set image deployment/nginx-deployment nginx=nginx:1.21.6
To update environment variables in a container, use the kubectl set env
command:
kubectl set env deployment/nginx-deployment MY_ENV=production
To modify the resource requests and limits of a container, use the kubectl set resources
command:
kubectl set resources deployment/nginx-deployment --limits=cpu=500m,memory=512Mi
This command sets the resource limits for the nginx-deployment in Kubernetes to 500m
CPU and 512Mi
memory.
Using kubectl scale to adjust replicas
The kubectl scale
command is specifically used to adjust the number of replicas in a deployment:
kubectl scale deployment nginx-deployment --replicas=3
This command adjusts the deployment to the desired number of replicas without modifying the YAML file directly.
Viewing the deployment status
After making changes, itβs essential to verify that the deployment is running as expected. Use the following command to view the status of your deployment:
kubectl get deployment nginx-deployment
The output shows that the deployment is up and running with the desired number of replicas.
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 5m
Conclusion
Editing a Kubernetes deployment without manually modifying the YAML file is efficient and flexible. This approach is especially useful when managing multiple deployments or automating tasks.
You can use kubectl edit for quick changes, kubectl patch for controlled modifications, or kubectl set for specific updates. These tools help you maintain your deployments effectively.
Always verify the status of your deployment after making changes to ensure everything functions as expected.
LHB Community is made of readers like you who like to contribute to the portal by writing helpful Linux tutorials.