Kubernetes Object : Deployments ☸☸

What are Deployments in Kubernetes ?

Vibhor Chinda
FAUN — Developer Community 🐾

--

A Deployment is a Kubernetes Object which can runs multiple replicas of your application and automatically replace any instances that fail or become unresponsive.

Hierarchy of Kubernetes Objects

If we want to deploy an application then we have Pod. If we want to scale the application then we have ReplicaSet. Then what is Deployment and why do we need Deployment. This question comes to everyone’s mind.

If we try to explain in very simple terms, Deployment sits top on the Kubernetes hierarchy layer. First Pod, then ReplicaSet, and then Deployment. By using Deployment we can easily upgrade the Pod instances using rolling update and we can easily undo the changes. So Deployment in the Kubernetes cluster is very flexible and gives us more value.

Let’s try to understand why do we need Deployments using the real world example.

Why do we need Deployments ?? 🤔🤔

Kubernetes Deployments

Imagine you have a production environment, and you need to deploy a web application in that production environment. But you need to have following things in check and control too.

  1. Not one but you need to deploy many such instances of your web application.
  2. Whenever there is an upgrade available for your web application, you need to upgrade the instances of your deployed applications too.
  3. Sometimes you don’t want to upgrade all the instances of your deployed web application at once, rather you want to upgrade the instances one after the other.
  4. Sometimes the upgrade which happened may be buggy or not so user friendly in nature. So at that point of time you might want to downgrade your deployed instances back to its stable/previous version.
  5. In some cases, you might want to make multiple changes to your deployed application. In this scenario, you can pause your environment then make all the changes you wanted to and finally resume your environment so that every change can be applied at once only after you are finished making all the changes.

Keeping in mind whatever we know till now about PODs and ReplicaSets. Having a check of all the above requirements can be a very tedious task.

So here comes the Hero of our movie i.e Deployments. As Deployments provide some functionalities with which all the above things cane be done quite easily and smoothly without any hassle.

Let’s list down the functionalities provided by Deployments in the coming section 🤩 🤩

Advantage of using Deployments 💪💪

Photo by Den Harrson on Unsplash
  1. We can deploy multiple instances of our applications for our end users with the help of Deployments.
  2. We can scale up or down our deployed application instances depending on the user load which we are facing.
  3. We can upgrade the instances of our deployed applications whenever a new version is available to us.
  4. The functionality of Rolling Update is available with the help of which we can upgrade the instances of our applications one after the other.
  5. We can Rollback to the previous version if the current version is not stable to use.
  6. Pause the rollout of new deployments whenever we feel like doing so.
  7. Resume the rollout of new deployments whenever we are ready with our changes.

So with the above functionalities and advantages, it might be clear WHY we need to use deployments. :))

How can we create Deployments ?? 🤔🤔

We can create Deployments using YAML files. To create a Kubernetes Deployment with YAML, you first create an empty file, assign it the necessary access permissions, and then define the necessary key-value pairs.

Below is an example of a Deployment definition file (deployment.yaml)

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80

There are many aspects and components in this file. Lets break down each one of them 😀

  • Let’s start with the apiVersion (key-value pair). This is used to clarify what API server and version you will be running in the background when creating the Deployment.
  • Next is kind which signifies the kind of definition file this is. In our case, it is a “Deployment”.
  • Next is metadata, which is a dictionary including the item name and labels. The metadata stores values that are assigned to the Deployment which is being created.
  • Finally there is spec which is actually an array/list. Following are the values which are present in it and what they mean.
    replicas : The number specified here, define how many PODs needs to be running and up all the time.
    selector : It basically has the labels which help the Deployment to identify the PODs it has to watch and keep the guard of while it is up.
    template : It is the section which basically contains the POD specifications. This section comes handy when Deployment needs to spin up new PODs during downtime scenarios.

We are done with the Deployment definition file. Now we can save and exit the file.

Use this command to create the Deployment based on the above YAML file :

kubectl create -f deployment.yaml

Use this command to view the Deployment :

kubectl get deployment

Use this command to view the ReplicaSet created :

kubectl get replicaset

Use this command to view the pods created :

kubectl get pods

What next ? 👀 👀

Thanks a lot for reaching till here! This is the end of this article.
But we have only scratched the surface of the K8s ecosystem :))
Much more to go, it will be a fun journey where we will learn a lot of cool stuff together.

Do clap and follow me 🙈 if you like my writings and want to read more from me in the future :))

In case of any doubts around this article or for some general chit chat, feel free to reach out to me on my social media handles

Twitter — https://twitter.com/ChindaVibhor

LinkedIn — https://www.linkedin.com/in/vibhor-chinda-465927169/

Related Articles

I will still keep on coming with new articles covering a bunch of topics I am exploring.

That’s All folks !! Doodles :))

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇

🚀Join FAUN & get similar stories in your inbox each week

--

--

Software Developer 2 @Guidewire | Ex - VMware | CKA | Exploring Cloud Tech | Developing Patience ✨✨