Update Multiple Lines in a YAML file with Kubectl

Update multiple lines in a YAML file with kubectl

Whenever I need to update a YAML file, the first thing that comes to mind is to either use sed or awk or perl etc., But there’s an in-house kubectl patch option that simplifies the experience.

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.
# nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: teamcloudyuga/nginx:alpine
        ports:
        - containerPort: 80
# File Source: https://kubernetes.io/docs/tasks/run-application/run-stateless-application-deployment/        

Apply the above nginx-deployment.yaml file and then check deployments and pods

kubectl apply -f nginx-deployment.yaml
Figure 1: Using the kubectl apply to create a deployment
Figure 1: Using the kubectl apply to create a deployment
kubectl get deployments
kubectl get pods

Now, let’s update the number of replicas in the spec  and also the nginx container image version. Remember, it’s multiline, and using any other option can be a bit clumsy.    

Updating Kubernetes Deployment

  • Locally, let’s create a file called patch.yaml with the below content.
# patch.yaml
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3 # Update the number replicas from 2 to 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: teamcloudyuga/nginx:latest # Update the version of nginx from alpine to latest
        ports:
        - containerPort: 80

Then patch the nginx-deployment with the below command

kubectl patch deployment/nginx-deployment --patch "$(cat patch.yaml)"
Figure 2: Using the kubectl patch to update the existing deployment
Figure 2: Using the kubectl patch to update the existing deployment

Once you see this message deployment.apps/nginx-deployment patched. Run the below command to check the deployments

kubectl get deployments
kubectl get pods
kubectl get deployment nginx-deployment -o yaml

and look for to see the previous configuration that is replaced 

Copy Codekubectl.kubernetes.io/last-applied-configuration: | {“apiVersion”:”apps/v1″,”kind”:”Deployment”,”metadata”:{“annotations”:{},”name”:”nginx-deployment”,”namespace”:”default”},”spec”:{“replicas”:2,”selector”:{“matchLabels”:{“app”:”nginx”}},”template”:{“metadata”:{“labels”:{“app”:”nginx”}},”spec”:{“containers”:[{“image”:”teamcloudyuga/nginx:alpine”,”name”:”nginx”,”ports”:[{“containerPort”:80}]}]}}}}

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 blog, we learned how to update existing Kubernetes resources with the kubectl patch command.

Join Our Newsletter

Share this article:

Table of Contents