Kubectl apply vs create
If you are a Kubernetes user, you must be familiar with two terms, namely, Kubectl apply and Kubectl create. If not, no worries, we are presenting you with a detailed explanation!
But first, let’s get a quick brief of what is Kubernetes?
What is Kubernetes?
Kubernetes is also known as K8s or Kube. It is an open-source system to manage containerized applications across several hosts. It was originally developed by Google before it became an open-source system. Borg is known as a container orchestration platform used by Google. Furthermore, Kubernetes descends from Borg.
It assists you to provide mechanisms that are beneficial to deploy, maintain and scale the applications.
This project is hosted by CNCF, the Cloud Native Computing Foundation. Additionally, if your company or business wishes to bring changes in the technologies that are microservices oriented or container packaged, to name a few, you can rely on CNCF.
Furthermore, Kubernetes today is progressing to become a general-purpose computing platform or an ecosystem that gives tough competition to virtual machines. Virtual machines, which make up the basic foundation of cloud-based applications and infrastructure, are highly influential on them.
This ecosystem helps the businesses to attain Platform-as-a-service, or, PaaS. This in turn helps to manage several infrastructure-related operations and tasks related to the cloud services.
Now, Kubernetes allows you to create cluster environments. There are several approaches to create such resources, for instance, kubectl apply and kubectl create.
Let us talk about them, one by one!
But first, what is Kubectl?
Let us dig this area a little deeper!
Kubectl basically is the controller of the Kubernetes cluster manager. Each of the two kubectl applications apply and behave differently when the objects are modified by multiple writers.
The first step is to configure Kubectl so that it automatically accesses the Kubernetes cluster’s Control panel after the minikube start command is executed.
If by chance, kubectl is not locally installed on your system, minikube already has it. To access it, use the below-given command:
minikube kubectl -- <kubectl commands>
Or, there is another option for you. You can use:
alias kubectl="minikube kubectl --"
Additionally, you can also create a symbolic link in the below-given manner:
ln -s $(which minikube) /usr/local/bin/kubectl
In order to get pods, type in the following:
minikube kubectl -- get pods
Moving on, to create a deployment inside the cluster you can use:
minikube kubectl -- create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.4
In order to expose the deployment with a NodePort service, type in the following:
minikube kubectl -- expose deployment hello-minikube --type=NodePort --port=8080
Now, let us understand the two categories in detail.
When we talk about kubectl create, it comes under the category of imperative management.
Now, what does this mean?
Well, this approach tells the Kubernetes API about what you want to create, delete or replace. In a more simplified manner, it means that you can create a whole new object from scratch. Or, it makes some changes to any existing object by defining the requirements.
On the other hand, it is quite a declarative management approach. It means that all the changes that you make to a live object will stay intact even if you apply more changes to the same object.
In simple words, apply means making more changes to an already existing object by declaring what exactly you need.
For more clarity, here is an example for you to understand the difference between kubectl create and kubectl apply.
Take a look!
We have created the Kubernetes pod below by using the YAML file as shown below.
root@kmaster-rj:~/pod-create# cat mypod.yml apiVersion: v1 kind: Pod metadata: name: create-vs-apply-demo labels: app: front-end rel: dev spec: containers: - name: httpd image: docker.io/httpd imagePullPolicy: IfNotPresent ports: - containerPort: 80
Now, let us create the pod by using kubectl create command:
root@kmaster-rj:~/pod-create# kubectl create -f mypod.yml pod/create-vs-apply-demo created
You have to enlist the pod status with the labels as shown below:
root@kmaster-rj:~/pod-create# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS create-vs-apply-demo 1/1 Running 0 8s app=front-end,rel=dev
To add an extra-label in the same YAML file, all you need to do is, follow the below-given steps:
root@kmaster-rj:~/pod-create# cat mypod.yml apiVersion: v1 kind: Pod metadata: name: create-vs-apply-demo labels: app: front-end rel: dev demo: applyVscreate spec: containers: - name: httpd image: docker.io/httpd imagePullPolicy: IfNotPresent ports: - containerPort: 80
Moving on, you have to apply these changes using the imperative approach.
root@kmaster-rj:~/pod-create# kubectl create -f mypod.yml Error from server (AlreadyExists): error when creating "mypod.yml": pods "create-vs-apply-demo" already exists
As you can see, there is an error message that states that the resource already exists in your file.
Here, we have applied the imperative approach. Now, we will use the declarative approach by using the kubectl apply command. Take a look.
root@kmaster-rj:~/pod-create# kubectl apply -f mypod.yml pod/create-vs-apply-demo configured
Now, the configuration of the resources is done. The Next step is to verify the changes.
root@kmaster-rj:~/pod-create# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS create-vs-apply-demo 1/1 Running 0 3m19s app=front-end,demo=applyVscreate,rel=dev
As you can clearly see that a new level is applied to the pod.
Moving on to the next segment, you may wonder which one of these two is the best pick? Well, here is a brief for you.
Kubectl create or Kubectl apply?
One thing that you need to understand is that these two are different methodologies and have different approaches for the work to be done.
Besides, if you wish to version control the Kubernetes objects, then you can go for the declarative approach, which basically assists you to analyse the accuracy of data.
On the other hand, if you wish to create resources for the purpose of troubleshooting or interactive experimentation you can select the imperative approach.
In a nutshell, you have two different approaches to create resources in the Kubernetes cluster.
The first step is to understand your requirements and then land on a wise pick depending upon your business needs. Select either kubectl apply or kubectl create.
The purpose of this article is to help you understand Kubernetes, kubectl create and kubectl apply.
We sincerely hope that your ambiguities are resolved after grasping the information stated above.
Keep learning, keep evolving!