Day 32 Task: Launching your Kubernetes Cluster with Deployment

Day 32 Task: Launching your Kubernetes Cluster with Deployment

ยท

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.

Task-1:

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

  • Prerequisites Make instances of t2.medium.
  # First all update system and install Docker 
  sudo apt-get update
  sudo apt-get install docker.io -y
  sudo usermod -aG docker $USER && newgrp docker

  # To install Minikube & Kubectl
  curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
  sudo install minikube-linux-amd64 /usr/local/bin/minikube 

  sudo snap install kubectl --classic
  minikube start --driver=docker
  • Clone this repository to your local machine:

        git clone https://github.com/niluflip/django-todo-cicd.git
    
sudo docker build . -t nileshsahare/django-todo:latest

  • Login to your Docker Hub by using your username and password and push your image to Docker Hub.

Create a Deployment YAML File

Create a new YAML file named deployment.yml and add the following content to it. This YAML file defines the Deployment with auto-healing and auto-scaling features.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: todo-app
spec:
  replicas: 3  # Initial number of replicas
  selector:
    matchLabels:
      app: todo-app
  template:
    metadata:
      labels:
        app: todo-app
    spec:
      containers:
        - name: todo-app
          image: nileshsahare/django-todo
          ports:
            - containerPort: 8000.
  • Explanation of deployment.yml

    • apiVersion: Specifies the Kubernetes API version to use.

    • kind: Defines the resource type as a Deployment.

    • metadata: Contains metadata about the Deployment, including its name.

    • spec.replicas: Specifies the desired number of replicas (pods) to maintain. This is the basis for auto-scaling.

    • spec.Selector: Defines how the Deployment selects which pods to manage based on labels.

    • spec.template: Specifies the pod template to be used for creating new pods.

    • spec.template.metadata.labels: Labels to be applied to the pods.

    • spec.template.spec.containers: Defines the containers within the pod, including the image and ports.

  • apply the deployment to your k8s (minikube) cluster by command kubectl apply -f deployment.yml

kubectl apply -f deployment.yml

  • To validate the functionality of the auto-healing feature, deliberately delete one of the pods.

๐Ÿš€ In completing these steps, you've successfully orchestrated a Kubernetes Deployment for a sample todo-app, complete with robust auto-healing and auto-scaling capabilities. With the Docker image thoughtfully hosted on Docker Hub, Kubernetes efficiently retrieved it during the deployment phase

Happy Learning :)

If you find my blog valuable, I invite you to like, share, and join the discussion. Your feedback is immensely cherished as it fuels continuous improvement. Let's embark on this transformative DevOps adventure together! ๐Ÿš€ #devops #90daysofdevop #git&github #jenkins #k8s

ย