Start your free 14-day ContainIQ trial

Using Kubectl Edit | Tutorial and Example

In this guide, we highlight the kubectl edit command and provide an actionable example alongside helpful context.

July 13, 2022
Kumar Harsh
Software Developer

Kubernetes has quickly become the leading container orchestration system with more than 2.8 million contributions and a growing majority of companies, according to CNCF, using it in production. Kubernetes enables engineers to host clusters of containers and scale them as needed depending on the requirements of your apps.

Kubectl is the Kubernetes command line tool and it makes running commands against a Kubernetes cluster easy. In this guide, you will learn how to use the <terminal inline>kubectl edit<terminal inline> command to update your K8s resources on the fly.

What is kubectl edit Used For?

As is clear by the name, the <terminal inline>kubectl edit<terminal inline> command is used to edit a deployed resource in your Kubernetes cluster. The syntax is simple:

kubectl edit <resource type> <resource name>

The <terminal inline><resource type><terminal inline> could be anything from a deployment to a ReplicaSet. It is specified in the key kind in the resource file. The <terminal inline><resource name><terminal inline> is defined under the key metadata.

You could also edit a resource file directly by using the following syntax:

kubectl edit -f <file path>

Let’s consider the following deployment file as an example.

apiVersion: apps/v1
kind: Deployment
  name: nginx-deployment
      app: nginx
  replicas: 2
        app: nginx
      - name: nginx
        image: nginx:1.14.2
        - containerPort: 80

Once this file is applied, it will create a sample nginx server deployment with two replicas. You can now use the following command to edit this resource using the default editor (vi for Linux and notepad for Windows):

kubectl edit deployment nginx-deployment

Alternatively, you could also run the following command, assuming that the resource file is stored at the root and named nginx.yaml:

kubectl edit -f nginx.yaml

Once you save the file, it will be updated in your K8s cluster.

Monitor Kubernetes Events in Real-Time
Monitor the health of your cluster and troubleshoot issues faster with pre-built dashboards that just work.
Learn More Book a Demo
event dashboard

Playing Around With kubectl edit

There are a couple of important things you can do with this command.

Updating the default editor

When running normally, the <terminal inline>kubectl edit<terminal inline> command would open the resource for editing in either vi for Linux or notepad on Windows. However, you can change this by updating the value of the <terminal inline>KUBE_EDITOR<terminal inline> variable. To use the nano editor to edit your resources, you can run a similar command:

KUBE_EDITOR=”nano” kubectl edit -f nginx.yaml

Changing the output version & type

The command will output the changes in the default API version by default. You can use the <terminal inline>--output-version<terminal inline> command to change it. Also, the default file format for the command is YAML. You can use the <terminal inline>-o<terminal inline> option to change. If you were to edit a JSON resource on your K8s server, here’s how you could do that:

kubectl edit -f nginx.yaml –ouptut-version=v1 -o json

Things to Keep In Mind While Using kubectl edit

Here are a few things you should be aware of when using the <terminal inline>kubectl edit<terminal inline> command:

  • Concurrent Edits: One of the most common errors users face when working with <terminal inline>kubectl edit<terminal inline> is when another editor changes the file on the server while the user is editing it locally. In that case, you need to fetch the latest changes to the resource before adding your own. Your changes will be saved in a temporary file, and you will need to update the resource version before saving your changes.
  • Kubectl edit vs. apply: It is often suggested not to use <terminal inline>kubectl edit<terminal inline> if you have an option to use the <terminal inline>kubectl apply<terminal inline> command. This is so because with <terminal inline>kubectl apply<terminal inline>, you get to maintain a local copy of your resource file and check it in a version control system for persistence and version update management. However, if you need to make a very small change on the fly, <terminal inline>kubectl edit<terminal inline> can come in quite handy.

Final Thoughts

Kubernetes is a very powerful and customizable container orchestration tool. The <terminal inline>kubectl edit<terminal inline> command makes it very easy for users to edit Kubernetes resources on the fly. In this guide, we showed you how to use the command as well as shared some tips to keep in mind when using it.

Start your free 14-day ContainIQ trial
Start Free TrialBook a Demo
No card required
Kumar Harsh
Software Developer

Kumar is a software developer and technical author. He has written for a number of software companies including LogRocket and Career Karma. Kumar previously worked for Goldman Sachs as a Summer Analyst. He is currently pursuing a Bachelor of Technology in Computer Science at the National Institute of Technology, Patna.