Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kubectl apply vs kubectl create?

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?
like image 730
Suresh Vishnoi Avatar asked Nov 18 '17 18:11

Suresh Vishnoi


People also ask

What is kubectl create?

Resource Creation kubectl apply and kubectl create are two different approaches in creating resources or updating resource configurations in the Kubernetes cluster environment. Both these commands accept configuration changes either from the file or stdin, and both accept JSON and YAML file formats.

What is the opposite of kubectl apply?

It's really important to remember that technically there isn't an inverse to kubectl apply . This is because of how k8s works, it converges desired state against current state. By running apply , you are telling the cluster to "make it look like this".

What does kubectl create command do?

The kubectl create service command creates the configuration for the Service and saves it to /tmp/srv. yaml . The kubectl create --edit command opens the configuration file for editing before it creates the object.

Does kubectl apply restart pod?

Because of this, there is no way to restart a pod, instead, it should be replaced. There is no kubectl restart [podname] command for use with K8S (with Docker you can use docker restart [container_id] ), so there are a few different ways to achieve a pod 'restart' with kubectl .


2 Answers

Those are two different approaches:

Imperative Management

kubectl create is what we call Imperative Management. 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 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 documentation.

In laymans They do different things. If the resource exists, kubectl create will error out and kubectl apply will not error out.

like image 98
Ara Pulido Avatar answered Sep 19 '22 12:09

Ara Pulido


When running in a CI script, you will have trouble with imperative commands as create raises an error if the resource already exists.

What you can do is applying (declarative pattern) the output of your imperative command, by using --dry-run=true and -o yaml options:

kubectl create whatever --dry-run=true -o yaml | kubectl apply -f - 

The command above will not raise an error if the resource already exists (and will update the resource if needed).

This is very useful in some cases where you cannot use the declarative pattern (for instance when creating a docker-registry secret).

like image 23
Sébastien Dan Avatar answered Sep 17 '22 12:09

Sébastien Dan