| by Arround The Web | No comments

Create Deployment Using “kubectl create deployment”

Kubernetes is a free, open-source distribution and also known as k8s. It is utilized for automating deployment, management, and scaling of containerized software and applications. It provides different components for container management such as nodes, control plane, pods, and Kubernetes deployment. With the help of Kubernetes,  the developers can easily automate the operational task, roll back the changes, and monitor and scale the containerized applications and software.

In this blog, we will explain:

What is Kubernetes Deployment?

The Kubernetes Deployment is referred to as a resource object that provides declarative instruction and updates to its major elements such as Pods, ReplicaSet, and containerized applications. The Kubernetes Deployment permits the developer to describe the containerized application such as image, number of pods, port, replicas, and so on. The one major reason why Kubernetes is better than Docker is that it offers us auto-healing and auto-scaling properties and these states are achievable due to Kubernetes Deployment.

In “auto-healing” mechanism is a test automation technique that tests its running object and in case of any error occurrence, implements the best match for correction, and applies the fix to match the actual state with the desired state. In “auto-scaling” service scales the application’s workload automatically.

Why Creating Deployment is Better than Creating Pods?

Kubernetes deployment permits the developer to easily keep a group of the same pods running with a common configuration. In contrast, while creating a pod, users are required to create and run the pod separately. In deployment, if one pod is deleted or terminated, it will automatically be recreated and started due to an auto-healing mechanism. But if pods are directly executing without deployment, in case of error, users are required to recreate and run the pod manually.

What is a ReplicaSet in Deployment?

When the developer created the deployment, three major components were generated and ReplicaSet is one of them. The ReplicaSet is a controller of deployment and is responsible for running the specified number of Pods within a deployment. In case, if one pod is deleted, the ReplicaSet quickly instructs to regenerate the new pod to match the actual state of the pods with the desired state. It is majorly responsible for providing declarative updates to pods.

What is the “kubectl create deployment” Command?

The “kubectl create deployment” is a command of the Kubectl tool that is used to create and start the Kubernetes deployment. Then deployment instructs Kubernetes how to create and update instances of your application.

Syntax

kubectl create deployment <deployment-name> --image=<image-name> --<option=value>

 
Option

The “kubectl create deployment” command supports different options to provide an extra layer of functionality and to give extra instruction to deploy instances of an application. The options supported by the “kubectl create deployment” command is given below in tabular form:

Option Description
–allow-missing-template-keys  If its value is set as true, it will ignore any errors in templates when a map key or field is missing in the template.
–dry-run Its value can be “none”, “server”, or “client”. If the value is client, it will only show or print the object that would be sent without sending it.

If the value is server, it will only submit server-side requests.

–field-manager Shows or sets the manager’s name used to track field ownership.
–image Specify the image name to specify the container template.
-o, –output Specify the output format.
–port It sets the port to expose the container.
-r, –replicas  It is used to define a number of pod replicas.
–save-config It is used to save the configuration of an object in its annotation. If its value is false, the annotation will be unchanged.
–show-managed-fields By default, its value is false. But if it is set as true, it will keep or save the managedFields when printing objects in JSON or YAML format.
–validate=’strict’ Its value can be “strict”, “warn”, or “ignore”. If it is “strict”, it will validate the input and fail the request if invalid.

If its value is “warn”, it will warn about duplicate and unknown fields.

If its value is “ignore”, it will not perform any schema validation.

 

Prerequisite: Install kubectl and minikube

To start Kubernetes Deployment in the Kubernetes cluster, first, install the below given tools on the system:

    • Kubectl Tool: Kubectl is a Kubernetes command line tool that is utilized to control, manage, and operate a Kubernetes cluster and its components such as Kubernetes Deployment.
    • Minikube Tool: Minikube is an implementation system for Kubernetes that provides or creates a virtual machine on the system to start the Kubernetes cluster. It can execute single-node clusters and is mostly used by beginners or for testing purposes.

To get started with Kubernetes and to install essential components like “kubectl” and “minikube”, go through our linked “Get Started With Kubernetes and Kubectl” article.

How to Create Kubernetes Deployment Using “kubectl create” Command?

The Kubernetes deployment can be created by applying the Yaml manifest or by the “kubectl create deployment” command. To create the Kubernetes deployment using the “kubectl create deployment” command, follow the below-provided demonstration.

Step 1: Run PowerShell

First, launch the Windows PowerShell with administrative privileges from the Start menu. The administrative privileges are required because minikube starts the Kubernetes cluster on the virtual machine by using HyperV:


Step 2: Start Kubernetes Cluster

To start the Kubernetes cluster, utilize the “minikube start” command:

minikube start

 

Step 3: Get Kubernetes Nodes

Next, access the Kubernetes nodes to check if the cluster is started or not using the “kubectl get nodes” command:

kubectl get nodes

 

Step 4: Create Kubernetes Deployment

Create a new Kubernetes deployment in the cluster through the “kubectl create deployment <deployment-name> –image=<image-name>” command. For demonstration, we have created “nginx-deployment” that will execute the Nginx application in a pod using the “nginx:stable-perl” image:

kubectl create deployment nginx-deployment --image=nginx:stable-perl

 

Step 5: Get Kubernetes Deployment, ReplicaSet and Pod

The “kubectl create deployment” command will create three components Deployment, ReplicaSet, and Pod. To access the Kubernetes deployments, run the “kubectl get deploy” command:

kubectl get deploy

 
Here, you can see “nginx-deployment” is available in the Kubernetes cluster:


The ReplicaSet is the controller of deployment that ensures the application in pods is executing error-free and repairs the breakpoints. To access the ReplicaSet, utilize the “kubectl get rs” command:

kubectl get rs

 
Here, the ReplicaSet shows the desired number of pods and current running pods:


The pod is a smaller unit of the Kubernetes cluster that runs the containerized application. To access the Kubernetes pods, use the “kubectl get pods” command. Here the “-o” option is used to specify the output format. To view the Pod IP address, we have used the “wide” format:

kubectl get pods -o wide

 

Step 6: Log in to Kubernetes Cluster

To access the application running in a container, first, access the Kubernetes cluster using the “minikube ssh” command:

minikube ssh

 

Step 7: Access Application Running in Kubernetes Deployment

After that, utilize the “curl” command along with the Pod IP address to access the containerized application:

curl 10.244.0.7

 

How to Run Replicas in Kubernetes Deployment Using “kubectl create deployment” Command?

Creating Kubernetes Deployment is better than running Pod as it can run and manage a group of pods at one time by creating Pod replicas. To run replicas in Kubernetes deployment using the “kubectl create deployment” command, follow the below instructions.

Step 1: Create Kubernetes Deployment

First, create a deployment using the “kubectl create deployment” command along with the “–replicas” option. The value of the “replicas” option will specify the desired number of pods that should be managed and run by ReplicaSet:

kubectl create deployment nginx-deployment --image=nginx:stable-perl --replicas=2

 

Step 2: Get Kubernetes Deployment, ReplicaSet, and Pods

Now, access the Kubernetes deployment using the “kubectl get deploy” command:

kubectl get deploy

 
Here, you can see deployment is showing the ready, up-to-date, and available number of pods:


Use the below command to access ReplicaSet information. You can see that ReplicaSet is showing the status of the desired number of pods and currently executing pods:

kubectl get rs

 

Similarly, to access the running pods, use the below command:

kubectl get pods

 
The output indicates that the Kubernetes deployment is successfully executing the desired number of Pods replicas in the Kubernetes cluster:

How ReplicaSet Works to Control Kubernetes Deployment?

The ReplicaSet is responsible for running the desired number of Pods within a deployment. If one pod is broken down or deleted, it will automatically recreate the pod to match the current status of the Pods with the desired state. This mechanism is actually implementing the auto-healing technique. For demonstration, follow the below instructions.

Step 1: View Live Kubernetes Pods

First, view the Pods status live using the “kubectl get pods -w” command. Also, note the name of the pod that will be broken or deleted:

kubectl get pods -w

 

Step 2: Delete a Pod

Next, open another PowerShell terminal and try to delete the second pod using the “kubectl delete pods <pod-name>” command:

kubectl delete pods nginx-deployment-7584b4674d-hbx4f

 
The output shows that we have successfully deleted the pod:


Now, open the PowerShell window where the pod’s status is live. Here, you can see when we deleted the Pod, the ReplicaSet automatically created and executed the new pod to match the current pods status to the desired status:


We have covered the method for creating Kubernetes Deployment using the “kubectl create deployment” command.

Conclusion

To create the Kubernetes Deployment using the “kubectl create deployment” command, first, install the essential tools like minikube and kubectl. After that, run the new Kubernetes cluster using minikube. Now, create a new deployment using the “kubectl create deployment <deployment-name> –image=<image-name>” command. After that, view the Kubernetes deployment, ReplicaSet, and Pods using the “kubectl get all” command. This article has illustrated how to create the Kubernetes Deployment using the “kubectl create deployment” command.

Share Button

Source: linuxhint.com

Leave a Reply