Debian 12 bookworm
Sponsored Link

MicroK8s : Horizontal Pod Autoscaler2023/06/22

 
Configure Horizontal Pod Autoscaler to set auto scaling to Pods.
[1]
[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 resorces
          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-65d9d7d54-r6kkx     1/1     Running   0          8s

root@dlp:~#
microk8s kubectl top pod

NAME                         CPU(cores)   MEMORY(bytes)
my-nginx-65d9d7d54-r6kkx     0m           7Mi

# after creating, [TARGETS] value is [unknown]

root@dlp:~#
microk8s kubectl get hpa

NAME           REFERENCE             TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
my-nginx-hpa   Deployment/my-nginx   <unknown>/20%   1         4         1          32s

# if some processes run in a pod, [TARGETS] value are gotten

root@dlp:~#
microk8s kubectl get hpa

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

# 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   108%/20%   1         4         1          12m

# pods have been scaled for settings

root@dlp:~#
microk8s kubectl get pods

NAME                         READY   STATUS    RESTARTS   AGE
my-nginx-65d9d7d54-r6kkx     1/1     Running   0          12m
my-nginx-65d9d7d54-sqvd2     1/1     Running   0          17s
my-nginx-65d9d7d54-rh25s     1/1     Running   0          17s
my-nginx-65d9d7d54-6j8tv     1/1     Running   0          17s

root@dlp:~#
microk8s kubectl top pod

NAME                         CPU(cores)   MEMORY(bytes)
my-nginx-65d9d7d54-6j8tv     0m           7Mi
my-nginx-65d9d7d54-r6kkx     0m           7Mi
my-nginx-65d9d7d54-rh25s     0m           7Mi
my-nginx-65d9d7d54-sqvd2     0m           7Mi
Matched Content