kubectl apply vs kubectl create?

ghz 1years ago ⋅ 8166 views

Question

What I understood by the documentation is that:

  • kubectl create
    

Creates a new k8s resource in the cluster

  • kubectl replace
    

Updates a resource in the live cluster

  • kubectl apply
    

If I want to do create + replace ( Reference )

My questions are

  1. Why are there three operations for doing the same task in a cluster?
  2. What are the use cases for these operations?
  3. How do they differ from each other under the hood?

Answer

Those are two different approaches:

Imperative Management

kubectl create is what we call [Imperative Management](https://kubernetes.io/docs/tutorials/object-management- kubectl/imperative-object-management-configuration/). On this approach you tell the Kubernetes API what you want to create, replace or delete, not how you want your K8s cluster world to look like.

Declarative Management

kubectl apply is part of the [Declarative Management](https://kubernetes.io/docs/tutorials/object-management- kubectl/declarative-object-management-configuration/) approach, where changes that you may have applied to a live object (i.e. through scale) are " maintained " even if you apply other changes to the object.

You can read more about imperative and declarative management in the [Kubernetes Object Management](https://kubernetes.io/docs/concepts/overview/working-with- objects/object-management/) documentation.

In laymans They do different things. If the resource exists, `kubectl

createwill error out andkubectl apply` will not error out.