0
0
Kubernetesdevops~5 mins

Cluster Autoscaler concept in Kubernetes - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes your Kubernetes cluster needs more or fewer worker nodes depending on the workload. Cluster Autoscaler automatically adjusts the number of nodes to match the demand, saving resources and cost.
When your app traffic suddenly increases and you need more nodes to handle the load.
When your cluster has idle nodes that are not running any pods and you want to save cost by removing them.
When you want your cluster to scale up or down automatically without manual intervention.
When you run batch jobs that require extra nodes temporarily.
When you want to maintain application availability by ensuring enough nodes are available.
Config File - cluster-autoscaler.yaml
cluster-autoscaler.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cluster-autoscaler
  namespace: kube-system
  labels:
    app: cluster-autoscaler
spec:
  replicas: 1
  selector:
    matchLabels:
      app: cluster-autoscaler
  template:
    metadata:
      labels:
        app: cluster-autoscaler
    spec:
      serviceAccountName: cluster-autoscaler
      containers:
      - name: cluster-autoscaler
        image: k8s.gcr.io/autoscaling/cluster-autoscaler:v1.26.0
        command:
          - ./cluster-autoscaler
          - --v=4
          - --stderrthreshold=info
          - --cloud-provider=aws
          - --skip-nodes-with-local-storage=false
          - --expander=least-waste
          - --nodes=1:10:example-node-group
        resources:
          limits:
            cpu: 100m
            memory: 300Mi
          requests:
            cpu: 100m
            memory: 300Mi
      tolerations:
      - key: node-role.kubernetes.io/control-plane
        effect: NoSchedule
      - key: node.kubernetes.io/not-ready
        operator: Exists
        effect: NoExecute
      - key: node.kubernetes.io/unreachable
        operator: Exists
        effect: NoExecute

This file deploys the Cluster Autoscaler as a Deployment in the kube-system namespace.

The image specifies the autoscaler version.

The --nodes=1:10:example-node-group flag sets the minimum and maximum nodes for the node group named example-node-group.

Tolerations allow the autoscaler to run on control-plane nodes and handle node conditions.

Commands
This command creates the Cluster Autoscaler deployment in your Kubernetes cluster so it can start managing node scaling automatically.
Terminal
kubectl apply -f cluster-autoscaler.yaml
Expected OutputExpected
deployment.apps/cluster-autoscaler created
Check that the Cluster Autoscaler deployment is running and ready in the kube-system namespace.
Terminal
kubectl get deployment cluster-autoscaler -n kube-system
Expected OutputExpected
NAME READY UP-TO-DATE AVAILABLE AGE cluster-autoscaler 1/1 1 1 30s
-n - Specifies the namespace where the deployment is located
View the logs of the Cluster Autoscaler to see its activity and confirm it is monitoring the cluster nodes.
Terminal
kubectl logs -f deployment/cluster-autoscaler -n kube-system
Expected OutputExpected
I0427 12:00:00.000000 1 cluster_autoscaler.go:123] Starting main loop I0427 12:00:05.000000 1 scale_up.go:456] Scale-up: adding 1 node(s) to example-node-group I0427 12:00:10.000000 1 scale_down.go:789] Scale-down: removing 1 node(s) from example-node-group
-f - Follows the log output in real time
-n - Specifies the namespace of the deployment
Key Concept

If you remember nothing else from this pattern, remember: Cluster Autoscaler automatically adds or removes nodes to match your workload demand, saving resources and keeping your apps running smoothly.

Common Mistakes
Not setting the correct node group name in the --nodes flag
The autoscaler won't know which nodes to scale, so it won't add or remove any nodes.
Use the exact node group name as configured in your cloud provider or cluster setup in the --nodes flag.
Not giving the autoscaler permission to run in the kube-system namespace
The deployment will fail or the autoscaler won't start because it lacks required permissions.
Create and assign the proper service account and roles for the autoscaler in kube-system.
Summary
Deploy the Cluster Autoscaler with a configuration specifying min and max nodes for your node group.
Verify the deployment is running and ready in the kube-system namespace.
Check autoscaler logs to monitor scaling actions and ensure it is working correctly.