Kubernetes

What is the difference between kubectl create and kubectl apply?

“Kubectl apply” modifies only the properties specified in the file in the existing stable source. It is possible that the file used in “apply” is an incomplete specification file. Only a few characteristics of the resources are affected by “Apply”. You may “apply” a file that merely modifies an annotation without specifying any additional resource characteristics. “Apply” provides more adaptability and is a preferred approach for updating production objects: it generates new objects from configuration files if they don’t already exist, upgrades existing objects from or several configuration files or folders, and helps sustain multiple configuration changes when made by various sources. The operation set “kubectl apply” is used to build or change Kubernetes services specified in a manifest file from a terminal’s command prompt. The resource’s state is defined in the manifest file, and then kubectl apply is used to put it into action. If we choose the apply option, Kubernetes will handle all we require. We don’t have to teach or deal with each phase individually. This is the Declarative technique, in which modifications made to a live object are “kept” even if other changes are made to the object.

Kubectl Create

“Kubectl create” erases the resources before creating them from the given file. “Create” uses an entire file. “Create” works based on each resource’s property. Because of the missing metadata, if you are using the file with the replace instruction, the instruction will abort. The imperative approach includes the choice of creation. This implies we will have to advise the Kubernetes client to conduct the necessary tasks. As a result, it’s similar to a user-guided approach. We refer to kubectl create as essential management. You inform the Kubernetes API of whatever you want to build using this method. kubectl create is the instruction that you are using to create a Kubernetes resource from the command prompt. This is a necessary application. You will, though, receive an exception if the resource previously exists. Let’s look into the specifics of both kubectl commands. Let’s start with kubectl apply.

Practical Implementation of kubectl apply

We will use the Ubuntu 20.04 Linux system for the practical implementation of the Kubectl apply command. We have installed minikube to get the services of Kubernetes in the Ubuntu 20.04 Linux system. So firstly, we have to start minikube in our Ubuntu 20.04 Linux system.

$ minikube start

The process will take some time. As quickly as it gets started, you can proceed further. To understand the concept of the kubectl applies command, we are going to create the deployment. Deployments are a collection of many identical Pods with no distinguishing characteristics. Deployments assist in ensuring that one or even more copies of your application are accessible to serve user’s requests in this manner. To create a file having. yaml extension, execute the affixed command.

$ touch deployment.yaml

The file will be created in the home directory; its name may vary as per user preference. A manifest file for a Kubernetes deployment with three copies of an Nginx container image is shown. We have named it” mydeployment” as you can check in line 4 of the attached image.

If you execute the command beneath, the information of this manifest file will be used to establish a deployment.

$ kubectl apply –f deployment.yaml

The output depicts that the deployment has been formed effectively. You will get the following output if you execute the following listed instruction:

 $ kubectl get deployment

You can check the newly created deployment in the above-attached image. We have labeled this deployment “mydeployment”, and it is now operating three pods.

Practical Implementation of kubectl Create

For the practical implementation of the kubectl create command, let’s start to create a deployment with kubectl create. You will receive the following appended result if you run the command shown in the attached image.

From the above-displayed output, understand that you will get an exception if you attempt to use kubectl create on a resource that already occurs. Let’s try using kubectl create to start creating a resource that doesn’t occur. In this scenario, we will use the name “yourdeployment” to name our Kubernetes deployment. We will use the following command to make the deployment:

$ kubectl create deployment yourdeployment --image=nginx

If everything goes well, you will have the output shown in the attached image.

Let’s make some changes to the initial deployment we made: “mydeployment”. The image shows how to do this by modifying the manifest file, “deployment.YAML”. As seen in the attached image(Line#8), replicas have been raised from 3 to 4.

To change the deployment from 3 to 4 replicas, run kubectl apply command as follows:

$ kubectl apply -f deployment.yaml

The deployment has been completed, according to the output. This indicates that a modification has been made to an existing deployment. To validate that the deployment is truly operating four pods, use the following set kubectl get deployment. You will get something like this as a result:

$ kubectl get deployment

Conclusion

In this article, the dissimilarity between kubectl apply, and kubectl create been explained in detail along with practical examples. kubectl create can only be cast-off to make a new resource declaratively. On the other side, you can use kubectl apply to state a new Kubernetes resource to use a manifest file using kubectl apply. By updating the configuration in the provided manifest file, you can use kubectl apply to modify an existing resource. I hope you will not have any concerns regarding this guide.

About the author

Kalsoom Akhtar

Hello, I am a freelance writer and usually write for Linux and other technology related content