kubectl apply vs create: Which One to Use for Creating Resources in Kubernetes Cluster Environment?
Two different approaches for creating resources in Kubernetes cluster. What's the difference?

kubectl apply
and kubectl create
both are two different approaches to create resources in Kubernetes cluster environment.
They both create resources from either a file or from STDIN.
kubectl apply and create: Two approaches for creating resources
Now let's go in some detailing and understand how kubectl apply and create differ from each other while implementing.
kubectl create: 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.
In simpler words, create
creates a whole new object (previously non-existing or deleted).
kubectl apply: 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
) will be "maintained" even if you apply
other changes to the object.
In simpler words, apply
- makes incremental changes to an existing object by defining what we need.
Understanding the difference between kubectl create and apply with example
I will use the below YAML file to create a Kubernetes pod.
Let's create the Pod using imperative way, i.e., using kubectl create
command:
List the pod status along with labels:
Now I will edit the YAML file and add an extra label (demo: applyVscreate) to it.
Now lets again use the imperative approach to apply the changes.
It throws an error and says the resource already exists.
Now let's do the same operation using declarative approach, i.e. kubectl apply
command.
So, the resource got configured this time. Verify the changes made.
You can see the new label has been applied to the pod.
I believe now you should have a clear understanding of the two approaches.

Kubectl create or apply? Which one to use?
It depends on use case how you want to use these concepts or methodology. It's not about which is good or which is bad.
If you want to version control the k8s object then it's better to use declarative way (kubectl apply) which helps to determine the accuracy of data in k8s objects.
And if you want to just create some resource for troubleshooting, learning or interactive experimentation purpose go with imperative approach (kubectl create).
Still confused? Do leave a comment and I'll try to answer your doubts.
Seasoned DevOps professional with 8+ years of real-world experience with tools like Docker, Kubernetes and more.