Day 32: Launching your Kubernetes Cluster with Deployment

Day 32: Launching your Kubernetes Cluster with Deployment

90DaysOfDevOps

·

3 min read

What is Deployment in k8s

A Deployment provides a configuration for updates for Pods and ReplicaSets.

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.

Here are some key aspects and functionalities of a Kubernetes Deployment:

  1. Replica Sets: Behind the scenes, a Deployment manages one or more Replica Sets. A Replica Set ensures that the specified number of replica pods are running at all times.

  2. Rolling Updates: Deployments support rolling updates, allowing you to update the application without downtime. When you change the configuration of a Deployment (e.g., update the container image version), Kubernetes orchestrates the update by creating new pods with the updated configuration and gradually terminating the old pods.

  3. Rollback: If a deployment update fails or encounters issues, you can easily rollback to the previous known good state.

  4. Scaling: You can scale the number of replicas (pods) up or down dynamically to meet changing demand. This can be done manually or automatically based on metrics using Horizontal Pod Autoscalers (HPA).

  5. Self-healing: Deployments ensure that the desired number of replicas are always running. If a pod fails or becomes unhealthy, Kubernetes automatically replaces it with a new pod to maintain the desired state.

Here's an example of a simple Deployment configuration in YAML:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-app-container
          image: my-app-image:v1

In this example:

  • The replicas field specifies that there should be three replicas (pods) of the application.

  • The selector defines how pods are selected to be part of this Deployment.

  • The template specifies the configuration for the pods, including the container image to use.

When you apply this configuration to your Kubernetes cluster, it will create a Deployment that ensures three replicas of your application are running. If you later update the image version or other parameters, Kubernetes will manage the rolling update to maintain the desired state.

Today's Task:-

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

  1. Create a deployment.yml file.

  2. The number of replicas specified is 3 (Auto-scaling). Even if one of the pod goes down, it will deploy a new pod thus maintaining the required number of replicas (Auto-healing).

Here we are pulling the node-todo-app-image and exposing it on port 80

Step2. Create the deployment.yml with the following command

kubectl apply -f deployment.yml

Thank you for reading this Blog!