Doomro
Doomro

Reputation: 141

K8s: Editing vs Patching vs Updating

In the kubectl Cheat Sheet (https://kubernetes.io/docs/reference/kubectl/cheatsheet/), there are 3 ways to modify resources. You can either update, patch or edit.

What are the actual differences between them and when should I use each of them?

Upvotes: 14

Views: 16511

Answers (3)

Satish
Satish

Reputation: 21

There are actually 4 main methods to modify Kubernetes resources, the fourth one being replace.

  • kubectl edit allows you to directly edit any resource live on the server. The edit function performs a get, opens an editor for you to make changes, then finally does an apply. While this can be handy for making quick changes, it can also lead to accidental alterations. This is because kubectl edit opens up the entire configuration for editing, and it’s easy to unintentionally modify something else in the process.

  • kubectl apply manages applications through files defining Kubernetes resources. Kubectl apply modifies the object by applying property values from a full YAML or JSON file. If the object specified in the YAML/JSON doesn’t exist yet, it’s created. The file needs to contain the full definition of the resource (it can’t include only the fields you want to update, as is the case with kubectl patch). This approach is great for using with Infrastructure as Code where we need to track changes to configurations over time.

  • kubectl patch strikes a balance between the above two. It allows for precise, quick modifications without exposing the entire configuration or needing a new configuration file. It works by “patching” changes onto the current resource configuration. The yaml file used needs to contain only the resources that need to be patched instead of all the resources needed in Kubectl apply. There are three types of patching: strategic merge, JSON merge patch, and JSON patch. Kubernetes documentation has a detailed description on how to use each of these three. As Mario mentioned this is the batch method of making changes and is easier to use within scripts.

  • kubectl replace replaces the object with a new one from a YAML/JSON file. Like in the apply command the file needs to contain the full definition of the resource. However, in contrast to the apply command, this command requires the object to exist; otherwise it prints an error.

There is also a fifth method of modifying Kubernetes resources, kubectl set Image, but it can be used just to modify the container images.


Upvotes: 1

mario
mario

Reputation: 11128

I would like to add a few things to night-gold's answer. I would say that there are no better and worse ways of modifying your resources. Everything depends on particular situation and your needs.

It's worth to emphasize the main difference between editing and patching namely the first one is an interactive method and the second one we can call batch method which unlike the first one may be easily used in scripts. Just imagine that you need to make change in dozens or even a few hundreds of different kubernetes resources/objects and it is much easier to write a simple script in which you can patch all those resources in an automated way. Opening each of them for editing wouldn't be very convenient and effective. Just a short example:

kubectl patch resource-type resource-name --type json -p '[{"op": "remove", "path": "/spec/someSection/someKey"}]'

Although at first it may look unnecessary complicated and not very convenient to use in comparison with interactive editing and manually removing specific line from specific section, in fact it is a very quick and effective method which may be easily implemented in scripts and can save you a lot of work and time when you work with many objects.

As to apply command, you can read in the documentation:

apply manages applications through files defining Kubernetes resources. It creates and updates resources in a cluster through running kubectl apply. This is the recommended way of managing Kubernetes applications on production.

It also gives you possibility of modifying your running configuration by re-applying it from updated yaml manifest e.g. pulled from git repository.

If by update you mean rollout ( formerly known as rolling-update ), as you can see in documentation it has quite different function. It is mostly used for updating deployments. You don't use it for making changes in arbitrary type of resource.

Upvotes: 6

night-gold
night-gold

Reputation: 2431

I don't think I have the answer to this but I hope this will help.

All three methods do the same thing, they modify some resources configuration but the command and way to it is not the same.

As describe in the documentation:

  • Editing is when you open the yaml configuration file that is in the kubernetes cluster and edit it (with vim or other) to get direct modification on your cluster. I would not recommand this outside of testing purpose, reapplying conf from orignal yaml file will delete modificaitons.
  • Patching seems the same to me, but without opening the file and targetting specific parts of the resources.
  • Updating in the documentation it seems that's it's all other method to update a resource without using patch or edit. Some of those can be used for debug/testing, for example forcing a resource replace, or update an image version. Others are used to update them with new configurations.

From experience, I only used editing and some command of update for testing, most of time I reapply the configurations.

Upvotes: 0

Related Questions