In this blog, we are going to see about Kubernetes object Deployment and how to deploy applications using deployment.
What is Deployment in Kubernetes?
Deployment is an object of Kubernetes that deploys and manages applications with the help of replicasets, it helps you to configure and deploy applications as you desire.
We can create the deployment in two ways
- Imperative method
- Declarative method
Imperative Method
This method uses the kubectl command 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, it will create a single pod as default.
You can check the status of deployment using the following command
kubectl get deploy
And, you can see the number of pods running using the following command
kubectl get po
Declarative Method
This method uses an YAML file to create a deployment.
The basic required file that should be in the YAML file is given below
- apiVerison – Here you have to specify the API version
- kind – Here you specify the object type, in our case it’s deployment.
- 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.
- spec – Here you have to specify the field in which you desire your application to deploy, such as replicas, selectors, and templates.
- replicas – Here you have to specify the number of pods you need, if not specified it will deploy one pod as default.
- selector – Here you have to give a label for the pod for identification.
- 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
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
Conclusion
These are the two methods to create a deployment in kubenetes, I belive this blog give you a decent understanding about Kubernetes Deployment.