Ubuntu 24.04
Sponsored Link

MicroK8s : Horizontal Pod Autoscaler2024/06/14

 

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-566ff895cb-fvgjj   1/1     Running   0          7s

root@dlp:~#
microk8s kubectl top pod

NAME                        CPU(cores)   MEMORY(bytes)
my-nginx-566ff895cb-fvgjj   0m           7Mi

root@dlp:~#
microk8s kubectl get hpa

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

# 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   200%/20%   1         4         2          3m31s

# pods have been scaled for settings

root@dlp:~#
microk8s kubectl get pods

NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-566ff895cb-524l9   1/1     Running   0          17s
my-nginx-566ff895cb-fvgjj   1/1     Running   0          3m47s
my-nginx-566ff895cb-ksmq6   1/1     Running   0          17s
my-nginx-566ff895cb-xq2wp   1/1     Running   0          32s

root@dlp:~#
microk8s kubectl top pod

NAME                        CPU(cores)   MEMORY(bytes)
my-nginx-566ff895cb-524l9   3m           7Mi
my-nginx-566ff895cb-fvgjj   0m           8Mi
my-nginx-566ff895cb-ksmq6   3m           7Mi
my-nginx-566ff895cb-xq2wp   0m           7Mi
Matched Content