K8s: Editing vs Patching vs Updating

10/15/2019

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?

-- Doomro
kubectl
kubernetes

2 Answers

10/15/2019

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.

-- mario
Source: StackOverflow

10/15/2019

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.

-- night-gold
Source: StackOverflow