Ubuntu 20.04
Sponsored Link

Kubernetes : Horizontal Pod Autoscaler2020/08/19

 
Configure Horizontal Pod Autoscaler to set auto scaling to Pods.
This example is based on the environment like follows.
-----------+---------------------------+--------------------------+------------
           |                           |                          |
       eth0|10.0.0.30              eth0|10.0.0.51             eth0|10.0.0.52
+----------+-----------+   +-----------+----------+   +-----------+----------+
|   [ dlp.srv.world ]  |   | [ node01.srv.world ] |   | [ node02.srv.world ] |
|      Master Node     |   |      Worker Node     |   |      Worker Node     |
+----------------------+   +----------------------+   +----------------------+

[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/v2beta1
kind: HorizontalPodAutoscaler
metadata:
  name: my-nginx-hpa
  namespace: default
spec:
  scaleTargetRef:
    kind: Deployment
    apiVersion: apps/v1
    # target Deployment name
    name: my-nginx
  # maximum number of replicas
  maxReplicas: 4
  minReplicas: 1
  metrics:
  - type: Resource
    resource:
      # scale if target CPU utilization is over 20%
      name: cpu
      targetAverageUtilization: 20

root@dlp:~#
kubectl apply -f my-nginx.yml -f hpa.yml

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

# verify settings

root@dlp:~#
kubectl get pods

NAME                          READY   STATUS    RESTARTS   AGE
my-nginx-fdd659f4b-fwzj9      1/1     Running   0          71s

root@dlp:~#
kubectl top pod

NAME                          CPU(cores)   MEMORY(bytes)
my-nginx-fdd659f4b-fwzj9      2m           3Mi

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

root@dlp:~#
kubectl get hpa

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

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

root@dlp:~#
kubectl get hpa

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

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

root@dlp:~#
kubectl get hpa

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

# pods have been scaled for settings

root@dlp:~#
kubectl get pods

NAME                          READY   STATUS    RESTARTS   AGE
my-nginx-fdd659f4b-99dtf      1/1     Running   0          50s
my-nginx-fdd659f4b-ffsz4      1/1     Running   0          50s
my-nginx-fdd659f4b-fwzj9      1/1     Running   0          5m23s
my-nginx-fdd659f4b-gnpm7      1/1     Running   0          50s

root@dlp:~#
kubectl top pod

NAME                          CPU(cores)   MEMORY(bytes)
my-nginx-fdd659f4b-99dtf      0m           3Mi
my-nginx-fdd659f4b-ffsz4      0m           3Mi
my-nginx-fdd659f4b-fwzj9      501m         5Mi
my-nginx-fdd659f4b-gnpm7      0m           3Mi
Matched Content