Ubuntu 22.04
Sponsored Link

Kubernetes : Horizontal Pod Autoscaler
2022/11/03
 
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-694bc864c8-q7sh9   1/1     Running   0          5s

root@ctrl:~#
kubectl top pod

NAME                        CPU(cores)   MEMORY(bytes)
my-nginx-694bc864c8-q7sh9   5m           6Mi

# 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          52s

# 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   200%/20%   1         4         1          103s

# pods have been scaled for settings

root@ctrl:~#
kubectl get pods

NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-694bc864c8-6zn47   1/1     Running   0          20s
my-nginx-694bc864c8-7kpjq   1/1     Running   0          20s
my-nginx-694bc864c8-86nlb   1/1     Running   0          20s
my-nginx-694bc864c8-q7sh9   1/1     Running   0          110s

root@ctrl:~#
kubectl top pod

NAME                        CPU(cores)   MEMORY(bytes)
my-nginx-694bc864c8-6zn47   0m           6Mi
my-nginx-694bc864c8-7kpjq   0m           6Mi
my-nginx-694bc864c8-86nlb   0m           6Mi
my-nginx-694bc864c8-q7sh9   0m           6Mi
Matched Content