Working With Deployment In Kubernetes

What is Deployment in Kubernetes? How to maintain a rolling update of our application?

@pramodAIML
Nov 26, 2020 · 5 min read
Image for post
Image for post

Kubernetes Deployment(controller) is your buddy if you want to deploy, update , maintain your hosted application on the production server without any downtime with utmost efficiency and with the required scalability and availability

Many times while working on your production server, you may encounter an instance where you would like to push a new update of your web application, while doing so it is desired not to break the running web app and to avoid any downtime. You may be required to deploy many instances of your web application to manage user workloads and to ensure the application is always available for the user to access.

Kubernetes Deployment here is your true mate which helps you to seamlessly perform your application rolling update. It also helps you to scale the web app horizontally by helping you create multiple replicas of the web application.

So in the Kubernetes ecosystem,

A Deployment provides declarative updates for Pods and ReplicaSets without breaking the user side experience

You can effortlessly roll-out application updates and also rollback the update if anything breaks using the deployment object in Kubernetes.

Deployment act as a controller in Kubernetes which helps Kubernetes developers and administrator achieve the desired state for pods, replica sets in the manner decided by the controller.

One can create Deployments to create new ReplicaSets or to remove existing Deployments and adopt all their resources with new Deployments.

Defining Deployment:

Here we are making use of imperative command to create a deployment

named: test-deploy, with an docker image = nginx having 3 replicas, as using the Kubectl CLI as shown below:

kubectl create deployment test-deploy --image=nginx --replicas=3

When you run this command you will see the,

Output:

Image for post
Image for post
Fig: 1.0

As seen in Fig 1.0, our test-deploy deployment has been created successfully and is in a ready state. We have made use of :

kubectl get deployment

and,

kubectl describe deployment test-deploy

To view the details of the deployment, we have created

demo-deploy.yaml:

We can create a deployment using a declarative way as shown below in our YAML file definition, here

  • kind is of type Deployment
  • apiVersion: apps/v1(These needs to be aways confirmed with Kubernetes API doc, as it may change in future Kubernetes API version updates. )
apiVersion: apps/v1
kind: Deployment
metadata:
# Unique key of the Deployment instance
name: demo-deployment
spec:
# 3 Pods should exist at all times.
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
# Apply this label to pods and default
# the Deployment label selector to this value
app: nginx
spec:
containers:
- name: nginx
# Run this image
image: nginx:1.14

Here we are deploying the Pod using the pod definition under the template section as shown below:

template:
metadata:
labels:
# Apply this label to pods and default
# the Deployment label selector to this value
app: nginx
spec:
containers:
- name: nginx
# Run this image
image: nginx:1.14

Using the selector under the spec, as shown below

selector:
matchLabels:
app: nginx

Here label is matched with the given pod label, if the label is not matched or missing our deployment will fail

apiVersion: apps/v1
kind: Deployment
metadata:
# Unique key of the Deployment instance
name: demo-deployment
spec:
# 3 Pods should exist at all times.
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
# Apply this label to pods and default
# the Deployment label selector to this value
app: nginx
spec:
containers:
- name: nginx
# Run this image
image: nginx

Use the below command to,

$ kubectl apply -f demo-deploy.yaml
Image for post
Image for post
fig 2.0

We can see from fig 2.0, that our deployment has been created successfully and is in a ready state.

When you inspect the Deployments in your cluster using the command

$ kubectl get deploy demo-deployment

, the following fields are displayed:

You will see the following as output:

  • NAME lists the names of the Deployments in the namespace.
  • READY displays how many replicas of the application are available to your users. It follows the pattern ready/desired.
  • UP-TO-DATE displays the number of replicas that have been updated to achieve the desired state.
  • AVAILABLE displays how many replicas of the application are available to your users.
  • AGE displays the amount of time that the application has been running.

The number of desired replicas is 3 according to the below field on our YAML definition.

spec:
# 3 Pods should exist at all times.
replicas: 3

Updating the Deployment :

Now that we have successfully deployed our deployment : demo-deployment

let’s update the same with new nginx, image version, nginx:1.16.1.

One can do so by using the set image command, as shown below:

$ kubectl set image deployment/demo-deployment nginx=nginx:1.16.1 --record

If it succeeds you will see the output as shown below, where demo-deployment is updated successfully

Image for post
Image for post
Fig 3.0

Let’s describe our deployment to get more details about our updated deployment

$ kubectl describe deploy demo-deployment
Image for post
Image for post
Fig 4.0

As can be seen in fig 4.0, we have updated the pod with the new image nginx:1.16.1, as a rolling update, with all the 3 desired replicas being created successfully.

One can also edit the deployment YAML file Alternatively, using the following edit command:

kubectl edit deployment.v1.apps/demo-deployment

and change the,.spec.template.spec.containers[0].image from nginx to nginx:1.16.1,

spec:
containers:
- name: nginx
# Run this image
image: nginx:1.16.1

save the file in the vim editor and your deployment will be updated with new, nginx:1.16.1, image version.

What's Next?

We saw

  • What is the Deployment workload in Kubernetes?
  • How to create deployment?
  • How to edit and update the deployment?

Next, we will continue to learn more about deployment where we will cover

  • How to rollback the update using deployment?
  • How to check the rollback and rollout status?

and more ……

Thanks for being there and supporting…..

SysopsMicro

Learn System Designing | Architecting | DevOps| Microservices | Clean Coding | Data Science

@pramodAIML

Written by

Everything About Artificial Intelligence | Machine Learning | Applied statistics| Data Science. Let’s learn AI & ML together

SysopsMicro

If you want to learn how to code and maintain scalable tech architecture, you have come to the right place. We are a team of consultant who spwcialise in clean coding, programming, system designing & archtecting, Decomposing Monolith to Microservices and Impelmenting Ci-CD

@pramodAIML

Written by

Everything About Artificial Intelligence | Machine Learning | Applied statistics| Data Science. Let’s learn AI & ML together

SysopsMicro

If you want to learn how to code and maintain scalable tech architecture, you have come to the right place. We are a team of consultant who spwcialise in clean coding, programming, system designing & archtecting, Decomposing Monolith to Microservices and Impelmenting Ci-CD

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store