DevOps(Day-32) : Launching your Kubernetes Cluster with Deployment

DevOps(Day-32) : Launching your Kubernetes Cluster with Deployment

ยท

3 min read

What is Deployment in k8s:

Deployment in Kubernetes (k8s) is a resource object that provides declarative updates to the desired state of a ReplicaSet. Deployments manage a set of ReplicaSets, which in turn manage a set of Pods.

The Deployment object allows you to define the desired state of your application, including the number of replicas (Pods) that should be running, the container image version, and other configurations. Kubernetes then ensures that the actual state matches the desired state by creating, updating, or deleting Pods as necessary.

Deployments also support rolling updates, which allow you to update your application without downtime by gradually updating the Pods to the new version. Additionally, deployments provide features such as scaling, rollback, and history tracking, making it easier to manage your application's lifecycle.

Task-1:

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

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

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

Write the deployment file on the server for the application that is ready for deployment.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: todo-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: todo
  template:
    metadata:
      labels:
        app: todo
    spec:
      containers:
      - name: todo
        image: rishikeshops/todo-app
        ports:
        - containerPort: 3000
      # remove autoscaling from here
  autoscaling:
    enabled: true
    minReplicas: 2
    maxReplicas: 10
    targetCPUUtilizationPercentage: 50

verify it on Node:

Detailed View of this code:

Here's a detailed explanation of the YAML file using stickers:

๐Ÿ”„ apiVersion: apps/v1: This line specifies the Kubernetes API version that this deployment will use.

๐Ÿ“Œ kind: Deployment: This line specifies that this YAML file defines a deployment object in Kubernetes. Deployments are used to manage and scale containerized applications.

๐Ÿ“ metadata: name: todo-app: This section specifies the metadata for the deployment object, including its name. In this case, the deployment is named "todo-app".

๐Ÿ“ spec: replicas: 2: The "spec" section of the YAML file specifies the desired state of the deployment. In this case, we want to run two replicas of the application.

๐Ÿ”Ž selector: matchLabels: app: todo: This section specifies the label selector for the deployment. It uses the label "app: todo" to match the pods that belong to this deployment.

๐Ÿ“ template: metadata: labels: app: todo: This section specifies the pod template for the deployment. It sets the "app" label to "todo".

๐Ÿ“ spec: containers: - name: todo image: rishikeshops/todo-app ports: - containerPort: 3000: This section specifies the container configuration for the deployment. It defines a single container named "todo" with the specified image and port number.

๐Ÿ“ autoscaling: enabled: true minReplicas: 2 maxReplicas: 10 targetCPUUtilizationPercentage: 50: This section specifies the autoscaling configuration for the deployment. When enabled, it will automatically adjust the number of replicas based on CPU utilization. The minimum number of replicas is 2 and the maximum number is 10. The target CPU utilization percentage is set to 50.

๐Ÿš€ Overall, this YAML file defines a scalable deployment for a todo app running in Kubernetes. By using the autoscaling feature, the deployment can automatically adjust the number of replicas based on demand, ensuring that the application is always available and responsive to users.

ย