What is Deployment in Kubernetes
Deployments allow users to manage a group of replica Pods. It represents an update model that handles the creation of a new ReplicaSet for each deployment. Simultaneously, it scales down the old Pods under the old ReplicaSet while scaling up new Pods under the new ReplicaSet. This gradual transition ensures a smooth and efficient update process.
Deployments are incredibly useful primarily when used for creating new Pods, updating existing Pods, or even scaling them up or down according to the application's requirements. Moreover, Kubernetes deployments also provide the capability to rollback to a previous deployment, making it a robust system for managing applications.
Creating a Deployment in Kubernetes
I will take a hands-on to creating a Deployment in Kubernetes.
Launching Minikube
Before we start with our Deployment, we need to make sure that Minikube is running. Start it with the following command:
$ minikube start
Initiating Pod as Deployment
Next, we will create a Deployment using the kubectl create deployment
command. In this case, we'll be deploying a sample application called helloworld
.
The --image
flag is used to specify the Docker image to be used for creating the Pods in this Deployment. Here's the command:
$ kubectl create deployment --image gcr.io/google-samples/hello-app:1.0 helloworld
deployment.apps/helloworld created
With this command, Kubernetes pulls the specified image from the container registry and starts a single instance of our application, called a Pod.
Listing the Deployments
Once the Deployment is created, we can use the kubectl get deployment command to list all the Deployments that are running in our current context. Here's the command:
$ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
helloworld 1/1 1 1 2m35s
This command will provide a list of all Deployments, including our newly created helloworld
Deployment.
Scaling Up the Deployment
Kubernetes allows you to easily scale your Deployment up or down. This is incredibly useful for handling traffic spikes or reducing resource usage during off-peak times.
Let's increase the number of Pods in our helloworld
Deployment to 5. We can achieve this by using the kubectl scale
command:
$ kubectl scale --replicas=5 deploy/helloworld
This command tells Kubernetes that we want 5 replicas of our helloworld
Pods. Kubernetes then schedules the additional Pods to start, increasing the total number of helloworld
Pods to 5. You can verify this by using the kubectl get deployment
command again.
$ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
helloworld 5/5 5 5 2m35s
Implementing Rolling Update on Deployment
We will take a look at how to implement a rolling update on a Kubernetes Deployment. Rolling updates allow Deployments' updates to take place with zero downtime by incrementally updating Pods instances with new ones.
Now, we have five helloworld
Pods.
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
helloworld-75db9fdfbc-5n68f 1/1 Running 0 3m23s
helloworld-75db9fdfbc-h5c28 1/1 Running 0 3m23s
helloworld-75db9fdfbc-hm8c5 1/1 Running 0 3m23s
helloworld-75db9fdfbc-skjbw 1/1 Running 0 3m28s
helloworld-75db9fdfbc-zq69x 1/1 Running 0 3m23s
Rolling updates can be implemented by using the kubectl set image command
. This command updates the image of a particular container of the Deployment. Let's change the image of the helloworld
Deployment:
$ kubectl set image deploy/helloworld hello-app=gcr.io/google-samples/hello-app:2.0
This command updates the image of the hello-app
container in helloworld
Pods to version 2.0. Kubernetes will create a new ReplicaSet and increase the count of replicas while the count of old replicas decreases.
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
helloworld-75db9fdfbc-h5c28 1/1 Terminating 0 3m33s
helloworld-75db9fdfbc-zq69x 0/1 Terminating 0 3m33s
helloworld-7c6785877d-27n2l 1/1 Running 0 5s
helloworld-7c6785877d-5w67z 1/1 Running 0 6s
helloworld-7c6785877d-9dwzh 1/1 Running 0 6s
helloworld-7c6785877d-ch9fr 1/1 Running 0 6s
helloworld-7c6785877d-pp8g9 0/1 ContainerCreating 0 4s
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
helloworld-7c6785877d-27n2l 1/1 Running 0 10s
helloworld-7c6785877d-5w67z 1/1 Running 0 11s
helloworld-7c6785877d-9dwzh 1/1 Running 0 11s
helloworld-7c6785877d-ch9fr 1/1 Running 0 11s
helloworld-7c6785877d-pp8g9 1/1 Running 0 9s
Checking Deployment History
We can use the kubectl rollout history
command to see the history of our helloworld
Deployment:
$ kubectl rollout history deploy/helloworld
deployment.apps/helloworld
REVISION CHANGE-CAUSE
1 <none>
2 <none>
This command will provide us with a history of revisions for our Deployment, along with the change that occurred in each revision.
Performing a Rollback on Deployment
In case we need to roll back our helloworld
Deployment to a previous revision, we can use the kubectl rollout undo command:
$ kubectl rollout undo deploy/helloworld
deployment.apps/helloworld rolled back
This command undoes the last Deployment, which in this case, reverts the hello-app
container image back to its previous version.
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
helloworld-75db9fdfbc-4bqbv 1/1 Running 0 2s
helloworld-75db9fdfbc-58z5v 1/1 Running 0 2s
helloworld-75db9fdfbc-6sn7j 0/1 Pending 0 1s
helloworld-75db9fdfbc-8gcd6 0/1 ContainerCreating 0 2s
helloworld-75db9fdfbc-zwq44 0/1 Pending 0 1s
helloworld-7c6785877d-5w67z 1/1 Terminating 0 89s
helloworld-7c6785877d-9dwzh 1/1 Terminating 0 89s
helloworld-7c6785877d-ch9fr 1/1 Running 0 89s
helloworld-7c6785877d-pp8g9 1/1 Running 0 87s
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
helloworld-75db9fdfbc-4bqbv 1/1 Running 0 9s
helloworld-75db9fdfbc-58z5v 1/1 Running 0 9s
helloworld-75db9fdfbc-6sn7j 1/1 Running 0 8s
helloworld-75db9fdfbc-8gcd6 1/1 Running 0 9s
helloworld-75db9fdfbc-zwq44 1/1 Running 0 8s
Cleaning Up the Deployment
After completing all operations and experiments with the Deployment, it is good practice to clean up the resources that were created. This ensures that the system remains uncluttered.
In order to delete the helloworld
Deployment, use the kubectl delete command:
$ kubectl delete deploy helloworld
This command removes the Deployment and all its related resources from the Kubernetes cluster, ensuring that no unused resources are left behind.