Launching your Kubernetes Cluster with Deployment

Launching your Kubernetes Cluster with Deployment

Day 32 of 90daysofdevops

ยท

5 min read

Deployment in K8s

  • A Deployment is a high-level resource that provides declarative updates for managing a set of identical replicas of a pod. A pod is the smallest deployable unit in Kubernetes that represents a single instance of a containerized application.

  • Deployments use a rolling update strategy by default, which ensures that new replicas are gradually introduced and old replicas are gracefully terminated. This strategy helps to minimize downtime and ensure that the application remains available during the update process.

Use Case

The following are typical use cases for Deployments:

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

The key components of deployment in Kubernetes are:

  1. Labels and Selectors: Labels are key-value pairs attached to Kubernetes objects, including pods and deployments. Selectors are used to specify criteria based on labels to identify and group related objects. Deployments use labels and selectors to manage and control the pods associated with them.

  2. Scaling: Deployments allow you to scale your application horizontally by adjusting the number of replicas. You can manually scale up or down based on the desired load or configure autoscaling based on metrics such as CPU utilization.

  3. Replicas: The number of replicas determines how many instances of your application should be running at any given time. It allows you to scale your application horizontally by increasing or decreasing the number of replicas.

  4. Pod Template: A deployment specifies a pod template that defines the desired state of the pods running your application. It includes details such as the container image, environment variables, resource limits, and volumes.

  5. Rollback: Kubernetes deployments support rollback functionality. If an update doesn't work as expected, you can roll back to the previous version of your application by specifying the revision or using the built-in rollback command.

  6. Health Checks: Kubernetes provides liveness and readiness probes to check the health of your application. Liveness probes determine if the application is running properly, and readiness probes indicate if the application is ready to accept traffic.

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

  1. Clone the repository into the master.

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

  2. Create an image from the Dockerfile

     cat Dockerfile
    
     docker build . -t abhisek6/django-todo:latest
    

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

     docker images
    
     docker login
    
     docker push abhisek6/django-todo:latest
    

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

  5. 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: abhisek6/django-todo:latest
             ports:
             - containerPort: 8000
    

  6. Apply the deployment to your k8s (minikube) cluster by following the command

     kubectl apply -f deployment.yml
    

  7. We can verify the pods are running,

     kubectl get pods
    

  8. 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
    

  9. Now we can delete a pod by using the following command,

     kubectl delete pod <pod-name>
    

  10. 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
    

  11. To delete a complete deployment, so that deployment is brought down we can use the following command,

    kubectl delete -f deployment.yaml
    


Thank You,

I want to express my deepest gratitude to each and every one of you who has taken the time to read, engage, and support my journey.

Feel free to reach out to me if any corrections or add-ons are required on blogs. Your feedback is always welcome & appreciated.

~ Abhisek Moharana ๐Ÿ˜Š

ย