Page cover

Basic Autoscaling

Autoscaling Based on Resources Utilization

Autoscaling based on resource utilization means that HPA (Horizontal Pod Autoscaler) adjusts the number of replica pods based on the percentage of resource usage (CPU or memory) relative to the requests specified in the Pod spec. You can first run the following syntax to create a YAML file named hpa.

nano hpa.yaml

Copy and paste the following YAML contents.

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: hpa-samplepod-cpu
  namespace: hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: samplepod
  minReplicas: 1
  maxReplicas: 5
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 70
    - type: Resource
      resource:
        name: mem
        target:
          type: Utilization
          averageUtilization: 60
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300
      policies:
        - type: Percent
          value: 100
          periodSeconds: 15
    scaleUp:
      stabilizationWindowSeconds: 0
      policies:
        - type: Percent
          value: 100
          periodSeconds: 15
        - type: Pods
          value: 4
          periodSeconds: 15
      selectPolicy: Max

After pasting the syntax, save the file by pressing Ctrl + O, then press the Enter key and exit the editor by pressing Ctrl + X. The next step is to apply this configuration.

kubectl apply -f hpa.yaml

Autoscaling based on resources value

Autoscaling based on resource value means that HPA adjusts the number of replica pods based on the absolute value of the resources used (CPU/memory) without comparing it to the request. You can first run the following syntax to create a YAML file named hpa.

nano hpa.yaml

Copy and paste the following YAML contents.

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: hpa-samplepod-cpu
  namespace: hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: samplepod
  minReplicas: 1
  maxReplicas: 5
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: AverageValue
          averageValue: 500m
    - type: Resource
      resource:
        name: mem
        target:
          type: AverageValue
          averageValue: 512Mi
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 300
      poli

After pasting the syntax, save the file by pressing Ctrl + O, then press the Enter key and exit the editor by pressing Ctrl + X. The next step is to apply this configuration

kubectl apply -f hpa.yaml

Last updated