Debian 12 bookworm
Sponsored Link

Kubernetes : Horizontal Pod Autoscaler2023/07/28

 

Configure Horizontal Pod Autoscaler to set auto scaling to Pods.

This example is based on the environment like follows.

-----------+---------------------------+--------------------------+------------
           |                           |                          |
       eth0|10.0.0.25              eth0|10.0.0.71             eth0|10.0.0.72
+----------+-----------+   +-----------+-----------+   +-----------+-----------+
|  [ ctrl.srv.world ]  |   |  [snode01.srv.world]  |   |  [snode02.srv.world]  |
|     Control Plane    |   |      Worker Node      |   |      Worker Node      |
+----------------------+   +-----------------------+   +-----------------------+

[1]
[2] This is an example of Deployment to set Horizontal Pod Autoscaler.
root@ctrl:~#
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@ctrl:~#
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@ctrl:~#
kubectl apply -f my-nginx.yml -f hpa.yml

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

# verify settings

root@ctrl:~#
kubectl get pods

NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-55f9ff4dc6-sfmtq   1/1     Running   0          7s

root@ctrl:~#
kubectl top pod

NAME                        CPU(cores)   MEMORY(bytes)
my-nginx-55f9ff4dc6-sfmtq   0m           7Mi

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

root@ctrl:~#
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@ctrl:~#
kubectl get hpa

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

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

root@ctrl:~#
kubectl get hpa

NAME           REFERENCE             TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
my-nginx-hpa   Deployment/my-nginx   50%/20%   1         4         4          3m52s

# pods have been scaled for settings

root@ctrl:~#
kubectl get pods

NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-55f9ff4dc6-7zp5n   1/1     Running   0          57s
my-nginx-55f9ff4dc6-842tc   1/1     Running   0          57s
my-nginx-55f9ff4dc6-9w2tt   1/1     Running   0          57s
my-nginx-55f9ff4dc6-sfmtq   1/1     Running   0          3m57s

root@ctrl:~#
kubectl top pod

NAME                        CPU(cores)   MEMORY(bytes)
my-nginx-55f9ff4dc6-7zp5n   0m           7Mi
my-nginx-55f9ff4dc6-842tc   0m           4Mi
my-nginx-55f9ff4dc6-9w2tt   0m           4Mi
my-nginx-55f9ff4dc6-sfmtq   59m          7Mi
Matched Content