Creating a Kubernetes Deployment for Todo-App with Auto-healing and Auto-Scaling on HashiCorp Nomad Day -32

ยท

3 min read

Welcome to another exciting journey into the world of container orchestration with Kubernetes! In this tutorial, we'll guide you through the process of deploying a simple todo-app using Kubernetes on HashiCorp Nomad, taking advantage of the powerful features of Auto-healing and Auto-Scaling.

Prerequisites

Before diving in, ensure that you have the following tools installed:

  • kubectl: The Kubernetes command-line tool.

  • HashiCorp Nomad: A highly available, distributed, and scalable cluster manager.

Understanding the Components

Deployment YAML

Let's start by breaking down the contents of the deployment.yml file we're about to create:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: todo-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: todo
  template:
    metadata:
      labels:
        app: todo
    spec:
      containers:
      - name: todo-app
        image: your-todo-app-image:latest
        ports:
        - containerPort: 80
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  autoscaler:
    enabled: true
    minReplicas: 2
    maxReplicas: 5
    targetCPUUtilizationPercentage: 80
  • replicas: Specifies the desired number of replicas (pods) for our todo-app.

  • selector: Defines labels to match pods controlled by this Deployment.

  • template: Describes the pod specifications, including labels and container details.

  • strategy: Specifies the update strategy for the Deployment, here, a RollingUpdate with controlled availability and surge.

  • autoscaler: Enables the Horizontal Pod Autoscaler (HPA) for automatic scaling based on CPU utilization.

Step 1: Create a Deployment YAML File

Open your favorite text editor and create a new file named deployment.yml. Copy the above YAML content into the file, adjusting the image name as per your todo-app.

Step 2: Apply the Deployment

Save the deployment.yml file and deploy it to your Nomad cluster using the following command:

kubectl apply -f deployment.yml

This command instructs Nomad to create the necessary resources based on your deployment configuration.

Step 3: Verify the Deployment

Check the status of your deployment using the following command:

kubectl get deployments

You should see the todo-app deployment with the desired number of replicas.

NAME       READY   UP-TO-DATE   AVAILABLE   AGE
todo-app   3/3     3            3           1m

Understanding Auto-Healing and Auto-Scaling

  • Auto-Healing: The maxUnavailable field in the strategy section ensures that no more than one pod is unavailable during updates. This provides resilience and ensures the continuous availability of your application.

  • Auto-Scaling: The autoscaler section enables automatic scaling based on CPU utilization. The minimum and maximum replica values define the scaling boundaries, and the target CPU utilization percentage triggers scaling decisions.

Conclusion

Congratulations! You've successfully deployed a todo-app with Auto-healing and Auto-Scaling on HashiCorp Nomad using Kubernetes. These features enhance the resilience and scalability of your applications, making them well-suited for dynamic and ever-changing environments.

Feel free to explore further customization options in the deployment file, experiment with different configurations, and adapt the setup to match your application's unique requirements. Happy coding!

ย