Lab 06: Autoscaling an OKS Cluster
Lab Objectives
Section intitulée « Lab Objectives »In this lab, you will learn how to enable and test autoscaling on a Kubernetes cluster deployed with OKS.
The goal is to observe how the cluster automatically adds or removes nodes based on workload.
Lab Architecture
Section intitulée « Lab Architecture »By the end of this lab, you will be able to:
- Configure a NodePool with autoscaling enabled.
- Deploy a variable workload on the cluster.
- Observe scale-up and scale-down behavior.
Environment Preparation
Section intitulée « Environment Preparation »Before starting this lab, make sure you have:
- An OKS cluster.
👉 Click here to access the OKS cluster creation lab if not already done.
Creating the NodePool with Autoscaling
Section intitulée « Creating the NodePool with Autoscaling »⚠️ Important:
- Autoscaling is only supported for single-Subregion (single-AZ) NodePools.
- If your NodePool is deployed across multiple subregions (multi-AZ), the
autoscaling: trueoption will not work.- Nodes using local volumes (e.g., mounted via
hostPath) are excluded from autoscaling and upgrade operations to prevent data loss.
Once your OKS cluster is up and running, we will create a NodePool configured to automatically adapt to workload.
Step 1: Create the NodePool YAML file
Section intitulée « Step 1: Create the NodePool YAML file »Create a file named nodepool.yaml with the following content:
apiVersion: oks.dev/v1beta2kind: NodePoolmetadata: name: nodepool-01spec: desiredNodes: 2 minNodes: 2 maxNodes: 4 autoscaling: true nodeType: tinav7.c2r4p1 zones: - eu-west-2a rootVolume: storageType: gp2 size: 20 upgradeStrategy: maxUnavailable: 1 maxSurge: 0 autoUpgradeEnabled: true autoUpgradeMaintenance: durationHours: 1 startHour: 12 weekDay: Tue autoHealing: trueApply the configuration to the cluster:
kubectl apply -f nodepool.yamlStep 2: Verify the NodePool creation
Section intitulée « Step 2: Verify the NodePool creation »List the created NodePools:
kubectl get nodepools.oks.devExample output:
NAME TYPE DESIRED NODES ATTACHED READY RUNNING PENDING CURRENT PROCESSING PHASEnodepool-01 tinav7.c2r4p1 2 2 2 2 0 idleAlso verify the cluster nodes:
kubectl get nodesExample output:
NAME STATUS ROLES AGE VERSIONip-10-50-34-159 Ready <none> 12d v1.32.5ip-10-50-42-1 Ready <none> 12d v1.32.5Testing Autoscaling with Real CPU Load
Section intitulée « Testing Autoscaling with Real CPU Load »In this section, we will deploy an application that consumes CPU resources to observe the autoscaling behavior of the OKS cluster.
This method uses the vish/stress image to simulate CPU and memory load on each pod.
Step 1: Create the stress-test.yaml file
Section intitulée « Step 1: Create the stress-test.yaml file »Create a file named stress-test.yaml with the following content:
apiVersion: apps/v1kind: Deploymentmetadata: name: stress-testspec: replicas: 7 selector: matchLabels: app: stress template: metadata: labels: app: stress spec: containers: - name: stress image: vish/stress args: ["-cpus", "2"] resources: requests: cpu: "1500m" memory: "512Mi" limits: cpu: "2000m" memory: "1Gi"Apply the deployment:
kubectl apply -f stress-test.yamlOnce the NodePool is ready, we will validate autoscaling by generating load.
Step 2: Observe cluster behavior
Section intitulée « Step 2: Observe cluster behavior »Monitor pod creation:
kubectl get podsExample output:
NAME READY STATUS RESTARTS AGEstress-test-7dc77b76f5-22phs 1/1 Running 0 2m11sstress-test-7dc77b76f5-d96bc 0/1 Pending 0 2m11sstress-test-7dc77b76f5-gvqjc 1/1 Running 0 2m11sstress-test-7dc77b76f5-nwbwp 0/1 Pending 0 2m11sstress-test-7dc77b76f5-p9lsw 1/1 Running 0 2m11sstress-test-7dc77b76f5-qg9lf 1/1 Running 0 2m11sstress-test-7dc77b76f5-tls8c 1/1 Running 0 2m11sThen monitor the number of nodes:
kubectl get nodepools.oks.devExample output:
NAME TYPE DESIRED NODES ATTACHED READY RUNNING PENDING CURRENT PROCESSING PHASEnodepool-01 tinav7.c2r4p1 4 3 3 3 0 idlenodepool-01 tinav7.c2r4p1 4 3 3 3 0 reconciliationnodepool-01 tinav7.c2r4p1 4 3 3 4 1 reconciliationTesting Scale-Down
Section intitulée « Testing Scale-Down »After verifying that scale-up works correctly, we will now test automatic scale-down.
Delete the deployment to remove the simulated CPU load:
kubectl delete -f stress-test.yamlMonitor the cluster and node count:
kubectl get nodesExample output:
NAME STATUS ROLES AGE VERSIONip-10-50-33-211 Ready <none> 14m v1.32.5ip-10-50-34-159 Ready <none> 12d v1.32.5ip-10-50-42-1 Ready <none> 12d v1.32.5ip-10-50-57-106 Ready <none> 14m v1.32.5After a delay of around 10 minutes, the autoscaler removes unused nodes to reduce unused capacity and costs.
Once scale-down is complete, the number of nodes should return to the minimum value defined in the NodePool.
Example output:
NAME STATUS ROLES AGE VERSIONip-10-50-34-159 Ready <none> 12d v1.32.5ip-10-50-57-106 Ready <none> 26m v1.32.5Expected Result
Section intitulée « Expected Result »During this lab:
- The OKS cluster automatically added nodes when CPU load increased (scale-up).
- After about 10 minutes of inactivity, it removed excess nodes to return to the minimum configuration (
minNodes: 2).