Day 32 of 90 Days of DevOps Challenge: Launching Your Kubernetes Cluster with Deployment

·

3 min read

Welcome to Day 32 of the 90 Days of DevOps challenge! 🎉 After launching your first Kubernetes pod yesterday, it’s time to take the next step and dive into Kubernetes Deployments. Today, you’ll create a deployment file to manage the lifecycle of your application and explore Kubernetes features like auto-healing and auto-scaling.

Let’s get started! 🚀


What is a Deployment in Kubernetes?

In Kubernetes, a Deployment allows you to define a desired state for your pods and ReplicaSets. It’s a higher-level object that helps manage pods by ensuring they’re running correctly and can scale when necessary.

Key benefits of using a deployment:

  • Auto-healing: Automatically replaces failed or unhealthy pods.

  • Auto-scaling: Scales the number of pod replicas up or down based on load or resource usage.

Deployments ensure that your application is always running in the desired state. You can also use deployments for updates, rollbacks, and scaling your application to handle more traffic or users.


Task-01: Create a Kubernetes Deployment

Today’s task is to create a deployment file for a sample todo-app. This deployment will include auto-healing and auto-scaling features, which are essential for running production-grade applications on Kubernetes.

Step 1: Create a Deployment YAML File

We’ll create a deployment file called deployment.yml. This file defines the configuration for your application, including the number of replicas, the container image, and resource limits for scaling.

Here’s a sample deployment file for a todo-app:

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: rishikeshops/todo-app
        ports:
        - containerPort: 3000

Step 2: Apply the Deployment

Once you’ve created the deployment.yml file, you can apply it to your Minikube Kubernetes cluster. Here’s the command:

kubectl apply -f deployment.yaml

This command will create the deployment and start running the todo-app with 3 replicas (as specified in the file). The service exposes the application on port 5000 inside the cluster and port 30007 outside the cluster (on your Minikube instance).


Auto-Healing and Auto-Scaling Features

Auto-Healing:

In Kubernetes, the Deployment Controller ensures that your desired state (as described in your deployment file) is maintained. If a pod crashes or becomes unhealthy, Kubernetes will automatically replace it, ensuring that the specified number of replicas is always running.

You can test this by deleting a pod and watching Kubernetes automatically recreate it:

kubectl delete pod <pod-name>

Auto-Scaling:

Kubernetes can automatically scale your application based on CPU and memory usage. You can configure Horizontal Pod Autoscaling (HPA) to scale the number of replicas up or down based on resource usage.

Here’s how you can create an HPA for your todo-app deployment:

kubectl autoscale deployment todo-app --cpu-percent=50 --min=2 --max=5

This command will scale the deployment up to 5 replicas if CPU usage exceeds 50%, and down to 2 replicas when the load decreases.


Conclusion:

Today’s task introduced you to the concept of Kubernetes Deployments, a powerful tool for managing your applications. You also explored auto-healing and auto-scaling, essential features for production-grade applications that ensure reliability and flexibility under varying loads.

Deployments are a key part of managing Kubernetes applications, so make sure to experiment with different configurations and scenarios to get comfortable with them.

Remember to share your progress on LinkedIn using #90DaysOfDevOps and keep exploring! 🚀

Â