Launching Kubernetes Cluster with Deployment

Launching Kubernetes Cluster with Deployment

Deployment in K8s

  1. Create: Write the deployment configuration in YAML format.

    • In this step, you define how your application should be deployed, including the image, number of replicas, and other settings.
  2. Apply: Deploy the configuration to your Kubernetes cluster.

    • Using the kubectl apply command, you send the YAML file to the cluster, and Kubernetes creates the necessary resources based on your deployment configuration.
  3. Verify: Check the deployment status and make sure everything is running.

    • With kubectl get deployments and kubectl get pods, you can verify that your application's deployment is successful and that the desired number of replicas are up and running in the cluster.

Use Case

deployments in Kubernetes are used for:

  1. Updating apps without downtime.

  2. Ensuring desired pod replicas.

  3. Automatic self-healing of failed pods.

  4. Managing different app versions.

  5. Rolling back to stable versions.

  6. Blue-Green and Canary deployments.

  7. Optimizing resource usage.

  8. Scaling apps based on demand.

  9. Supporting multi-region setups.

Example of NGINX Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

Components of Deployment

  1. Labels and Selectors: Labels are tags for Kubernetes resources, and selectors are filters to group and manage resources with those tags.

  2. Scaling: Scaling in Kubernetes adjusts the number of replicas (pods) for an app based on demand. It can be done manually or automatically using metrics like CPU utilization, ensuring efficient resource usage and high availability.

  3. Replicas: In Kubernetes, replicas refer to the desired number of identical copies (pods) running for an application. Defining the number of replicas allows you to achieve high availability and load balancing, ensuring your app can handle varying levels of traffic without downtime.

  4. Pod Template: In Kubernetes, a pod template is a blueprint that defines how a pod should be created. It contains specifications for containers, volumes, and other settings. Pod templates are used in controllers like deployments and replica sets to create and manage pods with consistent configurations, making it easy to scale and update applications.

  5. Rollback:In Kubernetes, a pod template is a blueprint that defines how a pod should be created. It contains specifications for containers, volumes, and other settings. Pod templates are used in controllers like deployments and replica sets to create and manage pods with consistent configurations, making it easy to scale and update applications.

  6. Health Checks: In Kubernetes, health checks are used to monitor the app's status inside pods. Readiness probes check if the pod is ready to handle traffic, while liveness probes verify if the pod is still running correctly. They help ensure app reliability and automatic recovery.

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

step 1 : Clone the repository into the master.

 git clone https://github.com/prabirmahatha/django-todo-cicd.git

step 2 : Create an image from the Dockerfile

 cat Dockerfile

 docker build . -t prabirmahatha/django-todo:latest

step 3 :Check the docker image and push it into DockerHub.

 docker images

 docker login

 docker push prabirmahathadjango-todo:latest

step 4 :Now we can verify in the DockerHub that image is pushed into the repo.

Then create a Manifest file named deployment.yml where all the configuration related to the image will store. Add a deployment.yml file.

vim deployment.yml

 apiVersion: apps/v1
 kind: Deployment
 metadata:
   name: my-django-app-deployment
   labels:
     app: django-app
 spec:
   replicas: 2
   selector:
     matchLabels:
       app: django-app
   template:
     metadata:
       labels:
         app: django-app
     spec:
       containers:
       - name: django-app-container
         image: prabirmahatha/django-todo:latest
         ports:
         - containerPort: 8000

step 5 : Apply the deployment to your k8s (minikube) cluster by following the command

 kubectl apply -f deployment.yml

step 6 :We can verify the pods are running,

 kubectl get pods

step 7 :On the worker node, to test docker container working or not.

 # Get the list of running docker container
 docker ps

 # Get into the docker container
 sudo docker exec -it <docker-container> bash

 curl -L http://127.0.0.1:8000

step 8 : Now we can delete a pod by using the following command,

 kubectl delete pod <pod-name>

step 9 :But after deleting a pod still we get 2 pods as Auto Healing is working, so even though 1 pod is removed or deleted a new pod is replaced with the older one.

kubectl get pods

step 10:To delete a complete deployment, so that deployment is brought down we can use the following command,

kubectl delete -f deployment.yaml

For another kubernetes project.

Follow me on LinkedIn to see interesting posts like this : )

linkedin.com/in/prabir-kumar-mahatha-6a0025..

Visit my git hub profile: github.com/PrabirKumarMahatha