CentOS Stream 9
Sponsored Link

Kubernetes : Horizontal Pod Autoscaler2023/10/20

 

Configure Horizontal Pod Autoscaler to set auto scaling to Pods.

This example is based on the environment like follows.

+----------------------+   +----------------------+
|   [ mgr.srv.world ]  |   |   [ dlp.srv.world ]  |
|     Manager Node     |   |     Control Plane    |
+-----------+----------+   +-----------+----------+
        eth0|10.0.0.25             eth0|10.0.0.30
            |                          |
------------+--------------------------+-----------
            |                          |
        eth0|10.0.0.51             eth0|10.0.0.52
+-----------+----------+   +-----------+----------+
| [ node01.srv.world ] |   | [ node02.srv.world ] |
|     Worker Node#1    |   |     Worker Node#2    |
+----------------------+   +----------------------+

[1]

Deploy Metrics Server, refer to here.

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

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

[root@mgr ~]#
kubectl get pods

NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-566ff895cb-ksfjm   1/1     Running   0          9s

[root@mgr ~]#
kubectl top pod

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

[root@mgr ~]#
kubectl get hpa

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

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

[root@mgr ~]#
kubectl get hpa

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

# pods have been scaled for settings

[root@mgr ~]#
kubectl get pods

NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-566ff895cb-62s6p   1/1     Running   0          100s
my-nginx-566ff895cb-6q6z7   1/1     Running   0          100s
my-nginx-566ff895cb-ksfjm   1/1     Running   0          5m40s
my-nginx-566ff895cb-xptl9   1/1     Running   0          100s

[root@mgr ~]#
kubectl top pod

NAME                        CPU(cores)   MEMORY(bytes)
my-nginx-566ff895cb-62s6p   0m           4Mi
my-nginx-566ff895cb-6q6z7   0m           4Mi
my-nginx-566ff895cb-ksfjm   0m           8Mi
my-nginx-566ff895cb-xptl9   0m           7Mi
Matched Content