Introducing Autopilot, an AI coding assistant
Using kubectl edit

Using kubectl edit

May 17, 2022
3 min read

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 kubectl edit command to update your K8s resources on the fly.

What is kubectl edit used for?

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


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

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


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


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):


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


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

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 kubectl edit 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 KUBE_EDITOR variable. To use the nano editor to edit your resources, you can run a similar command:


Changing the output version & type

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


Things to keep in mind while using kubectl edit

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

  • Concurrent Edits: One of the most common errors users face when working with kubectl edit 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 kubectl edit if you have an option to use the kubectl apply command. This is so because with kubectl apply, 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, kubectl edit can come in quite handy.

Final thoughts

Kubernetes is a powerful and customizable container orchestration tool. The kubectl edit 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.

If you're looking to build internal tools that make it even easier to edit Kubernetes resources on the fly, check out Airplane. With Airplane, you get a powerful, maintenance-free platform that can run several tools you may run with Kubernetes. Users can transform Python scripts, shell scripts, SQL queries, REST APIs, and more into custom UIs and workflows.

The basic building blocks of Airplane are Tasks, which are single or multi-step functions. Airplane also offers Views, a powerful UI framework to build dashboards quickly.

If you're looking to build complex workflows and UIs using code within minutes, sign up for a free account or book a demo.

Share this article:
Kumar Harsh
Kumar is a software developer and technical author. He has written for a number of software companies including LogRocket and Career Karma.

Subscribe to new blog posts from Airplane.