Day 32: 90DaysOfChallenge

Day 32: 90DaysOfChallenge

Launching Kubernetes Cluster with Deployment

Deployment

A Deployment in Kubernetes is a YAML or manifest file that defines the desired state for a set of Pods. It manages the lifecycle of the Pods by creating and updating ReplicaSets. ReplicaSets ensure that a specified number of replica Pods are running at any given time. They maintain the desired number of replicas as specified by the Deployment. Deployments and ReplicaSets focuses on managing the deployment and scaling of application Pods based on the desired state. When a Deployment is updated or replaced, Kubernetes handles the transition smoothly by adopting resources from the old Deployment to the new one, ensuring a seamless transition without downtime.

Use Case

The following are typical use cases for Deployments:

  1. Create a Deployment to rollout a ReplicaSet.

  2. Declare the new state of the Pods by updating the PodTemplateSpec of the Deployment.

  3. Rollback to an earlier Deployment revision if the current state of the Deployment is not stable.

  4. Scale up the Deployment to facilitate more load.

  5. Pause the rollout of a Deployment to apply multiple fixes to its PodTemplateSpec and then resume it to start a new rollout.

  6. Use the status of the Deployment as an indicator that a rollout has stuck.

  7. Clean up older ReplicaSets that you don't need anymore.

Task-1:

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

Before we start writing the deployment.yml file, we need to have image in our DockerHub account for which we want to take a pull and create the container. Hence, first login to DockerHub account and push the image. To learn about the steps to push the docker image to DockerHub, kindly go through Day-17 of 90DaysOfChallenge. We can see below, the docker image node-todo has been pushed to DockerHub.

  • add a deployment.yml file (sample is kept in the folder for your reference).

    Now we can start writing the deployment.yml file as we have our image from where it would take a pull. Let's write the below code in deployment.yml file -

apiVersion: apps/v1
kind: Deployment
metadata:
  name: todo-app
  labels:
    app: todo
spec:
  replicas: 2
  selector:
    matchLabels:
      app: todo
  template:
    metadata:
      labels:
        app: todo
    spec:
      containers:
      - name: todo
        image: shilpidns/node-todo:latest
        ports:
        - containerPort: 8000

The above example creates a ReplicaSet to bring up two node-todo Pods: In this example:

  • A Deployment named todo-app is created, indicated by the .metadata.name field. This name will become the basis for the ReplicaSets and Pods which are created later.

  • The Deployment creates a ReplicaSet that creates three replicated Pods, indicated by the .spec.replicas field.

  • The .spec.selector field defines how the created ReplicaSet finds which Pods to manage.

  • The template field contains the following sub-fields:

    • The Pods are labeled app: todo using the .metadata.labels field.

    • The Pod template's specification, or .template.spec field, indicates that the Pods run one container, todo, which runs the shilpidns/node-todo:latest Docker Hub image.

    • Create one container and name it todo using the .spec.template.spec.containers[0].name field.

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

    1. Create the Deployment by running the following command - kubectl apply -f deployment.yml

    2. Run the below command to check if deployment was created - kubectl get deployments

    3. Run the below command to see the ReplicaSet (rs) created by the Deployment kubectl get rs

    4. Run the below command to see the labels automatically generated for each Pod, kubectl get pods --show-labels

    5. Updating a deployment - We can update the deployment from one command line. Let's update the replica set from 2 to 1. This can be done from the command: kubectl scale deployment todo-app --replicas=3 and run the kubectl get pods -o wide command to see the Deployment status.

    6. Autohealing - To validate the functionality of the auto-healing feature, remove one of the pods. Kubernetes will autonomously generate a new pod to substitute the removed instance kubectl delete pod <pod_name>

      We can see even after deleting one pod, when we do kubectl get pods -o wide, we can see 3 pods are still running which means as soon as one pod gets deleted, a new one gets created.

    7. Verify whether the docker container is working using docker ps command but before that you need to login to minikube VM using minikube ssh as below:

       minikube ssh
       docker ps
       sudo docker exec -it <docker-container> bash #Get into the docker container
       curl -L http://10.244.0.4:8000
      

      Pick the IP address of the running container and add the container exposed port in the end to go inside the running container.

    8. Delete the deployment( entire Deployment and its associated pods,) using the command: kubectl delete -f deployment.yml

Hurray! We have successfully created a Kubernetes Deployment for a sample todo-app with "auto-healing" and "auto-scaling" features.

Thanks for reading!

Happy Learning!