Menu Close

K8 – How to set up automatic scaling for a Kubernetes deployment or replicaset

Here, we will learn How to set up automatic scaling for a Kubernetes deployment or replicaset.

The kubectl autoscale command is used to set up automatic scaling for a Kubernetes deployment or replicaset.

As the number of containers running in a Kubernetes cluster grows, it becomes increasingly important to ensure that the cluster is properly scaled to meet the demands of the applications it is hosting. One of the ways to achieve this is by using the kubectl autoscale command, which allows the user to set up an automatic scaling mechanism for their Kubernetes resources. In this article, we will discuss the kubectl autoscale command and its usage with examples.

Autoscaling allows Kubernetes to automatically adjust the number of replicas of a resource based on the current demand, such as CPU or memory utilization.

By using this command, the user can specify the minimum and maximum number of replicas for a resource and the target resource utilization. When the resource utilization exceeds the target utilization, Kubernetes will automatically increase the number of replicas. Conversely, if the utilization falls below the target, Kubernetes will decrease the number of replicas.

Syntax

kubectl autoscale <resource-type> <resource-name> --min=<min-replicas> --max=<max-replicas> --cpu-percent=<target-utilization>

Where:

  • <resource-type> and <resource-name> parameters specify the type and name of the resource to be autoscaled.
  • --min and --max options specify the minimum and maximum number of replicas that should be created for the resource.
  • --cpu-percent option specifies the target utilization level for the resource.

For example, to create an autoscaler for a deployment named “my-deployment” with a minimum of 2 replicas, a maximum of 10 replicas, and a target CPU utilization of 80%, the following command can be used:

kubectl autoscale deployment my-deployment --min=2 --max=10 --cpu-percent=80

This command will create an autoscaler for the “my-deployment” deployment that will automatically adjust the number of replicas based on the current CPU utilization.

Once the autoscaler is created, the user can view the current status of the autoscaler using the following command:

kubectl get hpa

This command will display a list of all the horizontal pod autoscalers (HPAs) in the cluster, along with their current status.

Example of kubectl Autoscale

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-webapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-webapp
  template:
    metadata:
      labels:
        app: my-webapp
    spec:
      containers:
      - name: my-webapp
        image: my-webapp:latest
        ports:
        - containerPort: 80

This deployment specifies a replica count of 3 and creates a pod template that contains a single container running a web application on port 80.

To create an autoscaler for this deployment, we can use the following command:

kubectl autoscale deployment my-webapp --min=2 --max=10 --cpu-percent=80

This will create an autoscaler for the “my-webapp” deployment with a minimum of 2 replicas, a maximum of 10 replicas, and a target CPU utilization of 80%.

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/go-lang-tutorial/

References:

https://golang.org/pkg/

Posted in kubernetes

Leave a Reply

Your email address will not be published. Required fields are marked *