Goglides Dev 🌱

Prason Pandey
Prason Pandey

Posted on

Understanding Kubernetes Deployment Object

In this article, we will be exploring the beauty of Deployment object. We will be covering how can we deploy any application using Deployment object, scale it up and scale it down.

Prerequisites

  • Kubernetes setup on your PC/Laptop.
  • Basic knowledge of Containerization.
  • Basic understanding of Kubernetes, concept like Pods, Replicaset, etc.

Deployment Object

Kubernetes deployment object is used to manage pods and replicas. Instead of creating replicaset object, you can directly manage the replica of pods from deployment object.

In a deployment, you can describe the desired state for your application and number of replicas, and Kubernetes will constantly check if this state and replicas are matched.

Create a Deployment

There are two ways to create a deployment. They are:

  1. Imperative
  2. Declarative

In Imperative way, we use a Command Line Interface(CLI) commands to create a deployment where as in Declarative way, we describe deployment state in a YAML or JSON file and create the deployment.

In this article, we will be using Imperative to generate Declarative file. Sounds confusing? Let's learn by doing it.

Creating Deployment YAML File

It can be confusing to remember the deployment yaml syntax. But dry-run flag is here to save us. You can generate the deployment file using the following command:

kubectl create deployment nginx-app --image=nginx --dry-run=client -o yaml > app.deploy.yaml
Enter fullscreen mode Exit fullscreen mode

You will end up getting file with following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx-app
  name: nginx-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-app
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx-app
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}
Enter fullscreen mode Exit fullscreen mode

Remove creationTimestamp, resources and status because we will not be using those fields in this article.

Now you will have something like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx-app
  name: nginx-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-app
  strategy: {}
  template:
    metadata:
      labels:
        app: nginx-app
    spec:
      containers:
      - image: nginx
        name: nginx
Enter fullscreen mode Exit fullscreen mode

Some of the fields, such as APIVersion, Kind, Metadata, Spec, etc., might already be familiar to you. Templates, strategies, and replicas are some of the new fields.

Template: This template is used to create pods. New pods are created using metadata and container information provided in the template section.

Strategy: A strategy is a way of changing or upgrading an application. I will go into more detail about this in my next blog.

Replicas: Using replicas, we can create multiple copies of the same pod as we did with replicaset.

Now, We can create deployment using our deployment file. For this, use command:

kubectl create -f app.deploy.yaml
Enter fullscreen mode Exit fullscreen mode

You can verify by the command:

kubectl get deployments
Enter fullscreen mode Exit fullscreen mode

You should see something like this:
Image description

We have successfully deployed the nginx app with using deployment object. Now it's time to scale.

Scaling with


kubectl scale

You can scale up or down using following command:

# Scaling Up
kubectl scale --replicas=2 deployment nginx-app
Enter fullscreen mode Exit fullscreen mode
# Scaling Down
kubectl scale --replicas=1 deployment nginx-app
Enter fullscreen mode Exit fullscreen mode

Scaling with


kubectl apply

You can edit the original deployment file and set replicas to whatever you want. To apply the changes, use the command:

kubectl apply -f filename.yaml
Enter fullscreen mode Exit fullscreen mode

Scaling with


kubectl edit

You can also directly edit the configuration by the command:

kubectl edit deployment nginx-app
Enter fullscreen mode Exit fullscreen mode

The change will automatically applied, when you exit the vi editor.

That is all for today, We will be covering about namespaces in the next blog.

Top comments (0)