Aller au contenu

Lab 06: Autoscaling an OKS Cluster

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.

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.

Before starting this lab, make sure you have:

⚠️ Important:

  • Autoscaling is only supported for single-Subregion (single-AZ) NodePools.
  • If your NodePool is deployed across multiple subregions (multi-AZ), the autoscaling: true option 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.

Create a file named nodepool.yaml with the following content:

apiVersion: oks.dev/v1beta2
kind: NodePool
metadata:
name: nodepool-01
spec:
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: true

Apply the configuration to the cluster:

Terminal window
kubectl apply -f nodepool.yaml

List the created NodePools:

Terminal window
kubectl get nodepools.oks.dev

Example output:

Terminal window
NAME TYPE DESIRED NODES ATTACHED READY RUNNING PENDING CURRENT PROCESSING PHASE
nodepool-01 tinav7.c2r4p1 2 2 2 2 0 idle

Also verify the cluster nodes:

Terminal window
kubectl get nodes

Example output:

Terminal window
NAME STATUS ROLES AGE VERSION
ip-10-50-34-159 Ready <none> 12d v1.32.5
ip-10-50-42-1 Ready <none> 12d v1.32.5

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.


Create a file named stress-test.yaml with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
name: stress-test
spec:
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:

Terminal window
kubectl apply -f stress-test.yaml

Once the NodePool is ready, we will validate autoscaling by generating load.

Monitor pod creation:

Terminal window
kubectl get pods

Example output:

Terminal window
NAME READY STATUS RESTARTS AGE
stress-test-7dc77b76f5-22phs 1/1 Running 0 2m11s
stress-test-7dc77b76f5-d96bc 0/1 Pending 0 2m11s
stress-test-7dc77b76f5-gvqjc 1/1 Running 0 2m11s
stress-test-7dc77b76f5-nwbwp 0/1 Pending 0 2m11s
stress-test-7dc77b76f5-p9lsw 1/1 Running 0 2m11s
stress-test-7dc77b76f5-qg9lf 1/1 Running 0 2m11s
stress-test-7dc77b76f5-tls8c 1/1 Running 0 2m11s

Then monitor the number of nodes:

Terminal window
kubectl get nodepools.oks.dev

Example output:

Terminal window
NAME TYPE DESIRED NODES ATTACHED READY RUNNING PENDING CURRENT PROCESSING PHASE
nodepool-01 tinav7.c2r4p1 4 3 3 3 0 idle
nodepool-01 tinav7.c2r4p1 4 3 3 3 0 reconciliation
nodepool-01 tinav7.c2r4p1 4 3 3 4 1 reconciliation

After verifying that scale-up works correctly, we will now test automatic scale-down.


Delete the deployment to remove the simulated CPU load:

Terminal window
kubectl delete -f stress-test.yaml

Monitor the cluster and node count:

Terminal window
kubectl get nodes

Example output:

Terminal window
NAME STATUS ROLES AGE VERSION
ip-10-50-33-211 Ready <none> 14m v1.32.5
ip-10-50-34-159 Ready <none> 12d v1.32.5
ip-10-50-42-1 Ready <none> 12d v1.32.5
ip-10-50-57-106 Ready <none> 14m v1.32.5

After 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:

Terminal window
NAME STATUS ROLES AGE VERSION
ip-10-50-34-159 Ready <none> 12d v1.32.5
ip-10-50-57-106 Ready <none> 26m v1.32.5

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).