Ubuntu 26.04

MicroK8s : Horizontal Pod Autoscaler2026/05/20

 

Configure Horizontal Pod Autoscaler to set auto scaling to Pods.

[1]

Enable Metrics Server, refer to here.

[2] This is an example of Deployment to set Horizontal Pod Autoscaler.
root@dlp:~#
vi my-nginx.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    run: my-nginx
  name: my-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      run: my-nginx
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - image: nginx
        name: my-nginx
        resources:
          # requests : set minimum required resources when creating pods
          requests:
            # 250m : 0.25 CPU
            cpu: 250m
            memory: 64Mi
          # set maximum resources
          limits:
            cpu: 500m
            memory: 128Mi

root@dlp:~#
vi hpa.yml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: my-nginx-hpa
  namespace: default
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    # target Deployment name
    name: my-nginx
  minReplicas: 1
  # maximum number of replicas
  maxReplicas: 4
  metrics:
  - type: Resource
    resource:
      # scale if target CPU utilization is over 20%
      name: cpu
      target:
        type: Utilization
        averageUtilization: 20

root@dlp:~#
microk8s kubectl apply -f my-nginx.yml -f hpa.yml

deployment.apps/my-nginx created
horizontalpodautoscaler.autoscaling/my-nginx-hpa created

# verify settings

root@dlp:~#
microk8s kubectl get pods

NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-7ff597f895-rp9wb   1/1     Running   0          17s

root@dlp:~#
microk8s kubectl top pod

NAME                        CPU(cores)   MEMORY(bytes)
my-nginx-7ff597f895-rp9wb   0m           7Mi

root@dlp:~#
microk8s kubectl get hpa

NAME           REFERENCE             TARGETS       MINPODS   MAXPODS   REPLICAS   AGE
my-nginx-hpa   Deployment/my-nginx   cpu: 0%/20%   1         4         1          64s

# run some processes in a pod manually and see current state of pods again

root@dlp:~#
microk8s kubectl get hpa

NAME           REFERENCE             TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
my-nginx-hpa   Deployment/my-nginx   cpu: 100%/20%   1         4         4          3m8s

# pods have been scaled for settings

root@dlp:~#
microk8s kubectl get pods

NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-7ff597f895-59hb8   1/1     Running   0          55s
my-nginx-7ff597f895-btb7z   1/1     Running   0          40s
my-nginx-7ff597f895-mj48j   1/1     Running   0          55s
my-nginx-7ff597f895-rp9wb   1/1     Running   0          3m25s

root@dlp:~#
microk8s kubectl top pod

NAME                        CPU(cores)   MEMORY(bytes)
my-nginx-7ff597f895-59hb8   0m           7Mi
my-nginx-7ff597f895-btb7z   0m           7Mi
my-nginx-7ff597f895-mj48j   0m           7Mi
my-nginx-7ff597f895-rp9wb   0m           7Mi
Matched Content