In this blog, we will explore Kubernetes object Deployment and how to deploy applications using both imperative and declarative methods.

What is Deployment in Kubernetes?

Deployment is an object of Kubernetes that manages rollout and lifecyle of applications pod with the help of replicasets, it helps you to configure and deploy applications as you desire.

We can create the deployment in two ways

You can create a Kubernetes deployment in two main ways:

  1. Imperative method - Using direct kubectl commands
  2. Declarative method- Using a YAML manifest file

Imperative Method

This method uses the kubectl commands directly to create a deployment.

For example, lets create a deployment nginx using the latest nginx image with two replicas, the command from the deployment is given below

kubectl create deploy nginx --image=nginx:latest --replicas=2

If you don't specify the --replicas flag, it will create a single pod as default.

You can check the status of deployment using the following command

kubectl get deploy
Kubernetes Object Deployment: Checking the status of deployment

And, you can see the number of pods running using the following command

kubectl get po
Kubernetes Object Deployment: checking number of pods on deployment

Declarative Method

This method uses a YAML file to create a deployment.

This method is important for the CKA exam.

The basic required file that should be in the YAML file is given below

  1. apiVerison - Here you have to specify the API version
  2. kind - Here you specify the object type, in our case it's deployment.
  3. metadata - Here we specify the name of the deployment and the namespace in which it needs to be deployed, if we don't specify the namespace it will deploy the application on the default namespace.
  4. spec - Here you have to specify the field in which you desire your application to deploy, such as replicas, selectors, and templates.
  5. replicas - Here you have to specify the number of pods you need, if not specified it will deploy one pod as default.
  6. selector - Here you have to give a label for the pod for identification.
  7. template - Here you have to specify the container image, volumes for the pod, etc.

First, create a YAML file using the following dry run command

kubectl create deploy nginx --image=nginx --replicas=3 --dry-run=client -o yaml > nginx.yaml

This command created a YAML file for deployment nginx using the latest nginx image with three replica as shown below

Kubernetes Object Deployment: deployment YAML file

Run the following command to deploy it

kubectl apply -f nginx.yaml

List the pods using the following command

Kubectl get po

You can see three nginx pods running as given below

Kubernetes Object Deployment: Checking pods of deployment

Conclusion

These are the two methods to create a deployment in kubenetes, I belive this blog give you a decent understanding about Kubernetes Deployment.