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