What I understood by the documentation is that:
My questions are
This question is related to
kubernetes
kubectl
One for the finest way to understand the difference for beginner.
kubectl run
= kubectl create deployment
kubectl create -f your-object-config.yaml
kubectl delete -f your-object-config.yaml
kubectl replace -f your-object-config.yaml
kubectl diff -f configs/
kubectl apply -f configs/
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/
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).
+----------------------------------------------------------+
¦ command ¦ object does not exist ¦ object already exists ¦
+---------+-----------------------+------------------------¦
¦ create ¦ create new object ¦ ERROR ¦
¦ ¦ ¦ ¦
¦ apply ¦ create new object ¦ configure object ¦
¦ ¦ (needs complete spec) ¦ (accepts partial spec) ¦
¦ ¦ ¦ ¦
¦ replace ¦ ERROR ¦ delete object ¦
¦ ¦ ¦ create new object ¦
+----------------------------------------------------------+
Just to give a more straight forward answer, from my understanding:
apply
- makes incremental changes to an existing object
create
- creates a whole new object (previously non-existing / deleted)
Taking this from a DigitalOcean article which was linked by Kubernetes website:
We use apply instead of create here so that in the future we can incrementally apply changes to the Ingress Controller objects instead of completely overwriting them.
We love Kubernetes is because once we give them what we want it goes on to figure out how to achieve it without our any involvement.
"create" is like playing GOD by taking things into our own hands. It is good for local debugging when you only want to work with the POD and not care abt Deployment/Replication Controller.
"apply" is playing by the rules. "apply" is like a master tool that helps you create and modify and requires nothing from you to manage the pods.
The explanation below from the official documentation helped me understand kubectl apply
.
This command will compare the version of the configuration that you’re pushing with the previous version and apply the changes you’ve made, without overwriting any automated changes to properties you haven’t specified.
kubectl create
on the other hand will create (should be non-existing) resources.
Source: Stackoverflow.com