What I understood by the documentation is that kubectl apply = kubectl create + kubectl replace. Reference
My understanding is that if I want create new k8s resource in the cluster I should use kubectl create operation. Now If I want to update something in a live k8s resources I should use kubectl replace operation.
If I want to do both operations (create a new k8s resource as well as update the live k8s resources ) then I should use kubectl apply operation
My questions are Why are there three operations for doing the same task in a cluster? What are the use cases for these operations? How do they differ from each other under the hood?
At the moment I am using kubectl create operation for creating new resources in the cluster. Thanks
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: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).
Just to give a more straight forward answer, from my understanding:
apply
- makes incremental changescreate
- overwrites all changesTaking this from a DigitalOcean article which was linked by Kubernetes website:
Those are two different approaches.
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.kubectl apply
is part of the Declarative Management approach, where changes that you may have applied to a live object (i.e. throughscale
) are maintained even if youapply
other changes to the object.You can read more about imperative and declarative management in the Kubernetes Object Management documentation.
These are imperative commands :
kubectl run
=kubectl create deployment
Advantages:
Disadvantages:
These are imperative object config:
kubectl create -f your-object-config.yaml
kubectl delete -f your-object-config.yaml
kubectl replace -f your-object-config.yaml
Advantages compared to imperative commands:
Disadvantages compared to imperative commands:
Advantages compared to declarative object config:
Disadvantages compared to declarative object configuration:
These are declarative object config
kubectl diff -f configs/
kubectl apply -f configs/
Advantages compared to imperative object config:
Disadvantages compared to imperative object configuration:
kubectl create can work with one object configuration file at a time. This is also known as imperative management
kubectl create -f filename|url
kubectl apply works with directories and its sub directories containing object configuration yaml files. This is also known as declarative management. Multiple object configuration files from directories can be picked up. kubectl apply -f directory/
Details :
https://kubernetes.io/docs/tasks/manage-kubernetes-objects/declarative-config/ https://kubernetes.io/docs/tasks/manage-kubernetes-objects/imperative-config/
The explanation below from the official documentation helped me understand
kubectl apply
.kubectl create
on the other hand will create (should be non-existing) resources.