Goglides Dev 🌱

Goglides Dev 🌱 is a community of amazing users

We are working on this space so that IT professionals can grow together.

Create account Log in
Jeewan Gautam
Jeewan Gautam

Posted on • Updated on

Process to deploy application in k8s using Deployment object using YAML file.

What is Deployment in Kubernetes?

A Kubernetes Deployment is used to tell Kubernetes how to create or modify instances of the pods that hold a containerized application. Deployments can scale the number of replica pods, enable rollout of updated code in a controlled manner, or roll back to an earlier deployment version if necessary.

Creating Deployment

We can create Deployment in three different ways. Let's discuss each of them.

Creating Deployment Using Yaml Definition:
We can create a deployment using a declarative way as shown below in our YAML file definition.

Step 1 - Create demo-deploy.yaml.

vim demo-deploy.yaml
Enter fullscreen mode Exit fullscreen mode

Step 2 - Declare below details and save file.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo-deployment
spec:
  replicas: 3 
  selector:
    matchLabels:
      app: static
  template:
    metadata:
      labels:
        app: static
    spec:
      containers:
      - name: demo-springapp
        image: jeewangautam02261995/springbootapp:v1

Enter fullscreen mode Exit fullscreen mode

In this example:
A Deployment named demo-deployment is created, indicated by the metadata: name field.
The Deployment creates three replicated Pods, indicated by the replicas field.
The Pod template, or spec: template field, indicates that its Pods are labelled app: static.
The Pod template's specification, or template: spec field, indicates that the Pods run one container, demo-springapp, which runs the springbootapp:v1 image from jeewangautam02261995 repository.

In sum, the Pod template contains the following instructions for Pods created by this Deployment:

Each Pod is labelled app: static.
Create one container and name it demo-springapp.
Run the springbootapp:v1 image at version 1.

Run below command to apply

kubectl apply -f demo-deploy.yaml 
Enter fullscreen mode Exit fullscreen mode

Output
Image description

Deployment is created with 3 pods. let have a look using below command.

kubectl get deployments
Enter fullscreen mode Exit fullscreen mode

Output
Image description
Command

kubectl get pods
Enter fullscreen mode Exit fullscreen mode

Output
Image description

Let's analyze pods created using deployment in more details

kubectl get pods -o wide --show-labels
Enter fullscreen mode Exit fullscreen mode

Output
Image description

Here, we can see pods are deployed in springbootdemo-control-plane node including status, IP, labels and more.

we have successfully deployed application created in previous blog in k8s cluster using deployment object. we can scale our deployment in different ways. Let's scale up and down cluster node by following below step.

Updating Deployment by making changes in Yaml Definition:
Here, we are going to changes replicas 3 to 4. If you wish you can modify labels, images or many more. here we will be using kubectl apply command to apply the changes in the deployments.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo-deployment
spec:
  replicas: 4 
  selector:
    matchLabels:
      app: static
  template:
    metadata:
      labels:
        app: static
    spec:
      containers:
      - name: demo-springapp
        image: jeewangautam02261995/springbootapp:v1
Enter fullscreen mode Exit fullscreen mode

Let's apply the command.

kubectl apply -f demo-deploy.yaml
Enter fullscreen mode Exit fullscreen mode

Output
Image description
Here, we can see our deployment is scaled up by adding 1 pods in it.

Discussion (0)