Whenever I need to update a YAML file, the first thing that comes to mind is to either use
or sed
or awk
etc., But there's an in-house perl
kubectl patch
option that simplifies the experience.
Lab Setup
You can start the lab setup by clicking on the Lab Setup button on the right side of the screen. Please note that there are app-specific URLs exposed specifically for the hands-on lab purpose.
Our lab has been set up with all necessary tools like base OS (Ubuntu), and developer tools like Git, Vim, wget, and others.
Lab With Kubectl Patch
As we triggered the lab through the LAB SETUP button, a terminal, and an IDE comes for us which already have a Kubernetes cluster running in them. This can be checked by running the kubectl get nodes
command.
Creating Kubernetes Deployment
- First, let’s create the
nginx-deployment
with 2 replicas and with container image as nginx with alpine tag.
Apply the above nginx-deployment.yaml
file and then check deployments and pods
kubectl apply -f nginx-deployment.yaml
kubectl get deployments
kubectl get pods
Now, let’s update the number of replicas in the
and also the nginx container image version. Remember, it's multiline, and using any other option can be a bit clumsy. spec
Updating Kubernetes Deployment
- Locally, let’s create a file called
patch.yaml
with the below content.
Then patch the nginx-
deployment
with the below command
kubectl patch deployment/nginx-deployment --patch "$(cat patch.yaml)"
Once you see this message
. Run the below command to check the deploymentsdeployment.apps/nginx-deployment patched
kubectl get deployments
kubectl get pods
kubectl get deployment nginx-deployment -o yaml
and look for to see the previous configuration that is replaced
What Next?
As we have seen an example about how to patch an existing deployment with a kubectl patch, more information on this can be found out here.
Conclusion
In this hands-on lab, we learned how to update existing Kubernetes resources with the kubectl patch command.