Launching your Kubernetes Cluster with Deployment

Dhwarika JhaDhwarika Jha
3 min read

What is Deployment in k8s

  • In Kubernetes (also known as K8s), deployment refers to the process of creating and managing a set of identical application instances, called pods, across a cluster of machines. It is a higher-level abstraction that provides declarative updates and scaling for your applications.

  • You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate. You can define Deployments to create new replicas for scaling or to remove existing Deployments and adopt all their resources with new Deployments.

  • Deployments in Kubernetes offer several benefits:

    1. Scaling and self-healing: Deployments provide automatic scaling and healing capabilities. You can specify the desired number of replicas, and Kubernetes ensures that this number is maintained, creating or terminating pods as needed.

    2. Rolling updates and rollbacks: Deployments support rolling updates, allowing you to update your application without downtime. When you modify the deployment's configuration (e.g., update the container image version), Kubernetes orchestrates a rolling update, gradually replacing old pods with new ones. If any issues are detected, you can easily roll back to the previous version.

    3. Load balancing: Deployments leverage Kubernetes services to automatically distribute incoming traffic among the running pods, providing load balancing for your application.

    4. Version control and history: Kubernetes deployments keep track of revision history, allowing you to roll back to a specific version if needed. This feature is especially useful in troubleshooting or managing changes in production environments.

To create a deployment, you define a YAML or JSON file that describes the desired state of your application. This includes specifying the container image, resource requirements, environment variables, ports, and other parameters. Once you apply this configuration to Kubernetes, it will create and manage the specified number of pods according to your desired state.

Task

Create one Deployment file to deploy a sample todo-app on K8s using the "Auto-healing" and "Auto-Scaling" features.

Deployment YAML file that deploys a sample todo-app on Kubernetes with auto-healing and auto-scaling features

apiVersion: apps/v1
kind: Deployment  
metadata:
  name: todo-app  #The Deployment is named "todo-app-deployment".
  labels:
    app: todo
spec:
  replicas: 2  # want 2 replicas of the todo-app.
  selector:
    matchLabels:
      app: todo
  template:
    metadata:
      labels:
        app: todo
    spec:
      containers:
      - name: todo
        image: dwarika9167/todoapp #image will pull from docker hub
        ports:
        - containerPort: 3000

With this Deployment file, Kubernetes will create two replicas of the todo-app and ensure they are continuously running. If any pod fails or terminates, Kubernetes will automatically recreate a new pod to maintain the desired number of replicas (auto-healing). Additionally, you can modify the replicas field to scale the application up or down based on the demand

Apply the deployment to your k8s cluster by command

kubectl apply -f Deployment.yaml #you will get conformation message

Thanks ......

0
Subscribe to my newsletter

Read articles from Dhwarika Jha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dhwarika Jha
Dhwarika Jha