Bootstrap FreeKB - OpenShift - Automatically scale a pod using Horizontal Pod Autoscaler
OpenShift - Automatically scale a pod using Horizontal Pod Autoscaler

Updated:   |  OpenShift articles

An application can be deployed as a deployment or as a deployment config. When deploying the application as a deployment (not a deployment config), a replica set will be used to create the pods.

 

When deploying an application as a deployment config (not a deployment), a replication controller will be used to create the pods.

 

You can update a deployment, deployment config, replica set, replication controller or stateful set to have a certain number of replicas. Replicas is the number of pods that should be created. 

  • Manually, using the oc scale command
  • Automatically, using the Horizontal Pod Autoscaler command (this article)

Beforing creating the autoscaler, you will need to create a Limit Range or update the deployment, deployment config, replica set, replication controller or stateful with resource requests and limits.

Here is a snippet of a deployment or deployment config YAML that could be used to set requests and limits.

spec:
  template:
    spec:
      containers:
        resources:
          limits:
            cpu: 500m
            memory: 128Mi
          requests:
            cpu: 10m
            memory: 96Mi

 

And here is an example Limit Range to set requests and limits for all pods in the project.

~]$ oc describe limitrange my-limits
Name:       my-limits
Namespace:  my-project
Type        Resource  Min    Max    Default Request  Default Limit  Max Limit/Request Ratio
----        --------  ---    ---    ---------------  -------------  -----------------------
Container   cpu       10m    500m   10m              10m            -
Container   memory    128Mi  1Gi    128Mi            1Gi            -

 

The oc autoscale command can be used to automatically update a deployment, deployment config, replica set, replication controller or stateful set to have a certain number of replicas based on some condition, such as the amount of CPU or memory being used.  In this example, the deployment will always have a least 1 pod, and will automatically scale up to a maximum of 3 pods if the pods CPU usage is 50% of the CPU limit.

~]$ oc autoscale deployment my-app --min 1 --max 3 --cpu-percent 50
horizontalpodautoscaler.autoscaling/my-app autoscaled

 

The oc autoscale command does not have an option to autoscale based on memory used, so you'll have to use a YAML file to create an autoscaler for memory.

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: my-autoscaler
  namespace: my-project
spec:
  scaleTargetRef:
    apiVersion: apps.openshift.io/v1
    kind: Deployment <- Deployment | DeploymentConfig | ReplicaSet | ReplicationController | StatefulSet
    name: my-deployment <- must match deployment | deploymentconfig | replicaset | replicationcontroller | statefulset name
  minReplicas: 1
  maxReplicas: 3
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        averageUtilization: 50
        type: Utilization
  - type: Resource
    resource:
      name: memory
      target:
        averageValue: 500Mi
        type: AverageValue

 

The oc apply or oc create command with the -f or --filename option can be used to create the autoscaler using the template JSON or YAML file.

~]$ oc create --filename autoscaler.yml 
horizontalpodautoscaler.autoscaling/my-autoscaler created

 

The oc get horizontalpodautoscaler (or oc get hpa) command can then be used to list the autoscalers that have been created.

~]$ oc get horizontalpodautoscaler
NAME     REFERENCE           TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
my-app   Deployment/my-app   <unknown>/50%   1         3         1          10m

 

The oc exec command can be used to determine how much memory is being used by the pod. In this example, the pods memory usage in 525 MB, which exceeds the autoscaler memory limit of 500Mi.

~]$ bytes=$(oc exec my-app-9mzm2 -- cat /sys/fs/cgroup/memory/memory.usage_in_bytes); echo $(awk "BEGIN{print $bytes / 1024 / 1024}") MB
525.5625 MB

 

In this example, the autoscaler should increase the replicas since the pods memory usage exceeds the autoscaler memory limit. And the oc describe horizontalpodautoscaler command can also be used to display information about an autoscaler.

~]$ oc describe horizontalpodautoscaler my-autoscaler
Name:                       my-autoscaler
Namespace:                  my-project
Labels:                     <none>
Annotations:                <none>
CreationTimestamp:          Wed, 08 Mar 2023 20:34:44 -0600
Reference:                  DeploymentConfig/my-app-2
Metrics:                    ( current / target )
  resource memory on pods:  61489152 / 32Mi
Min replicas:               1
Max replicas:               3
DeploymentConfig pods:      1 current / 2 desired
Conditions:
  Type            Status  Reason              Message
  ----            ------  ------              -------
  AbleToScale     True    SucceededRescale    the HPA controller was able to update the target scale to 2
  ScalingActive   True    ValidMetricFound    the HPA was able to successfully calculate a replica count from memory resource
  ScalingLimited  False   DesiredWithinRange  the desired count is within the acceptable range
Events:
  Type    Reason             Age   From                       Message
  ----    ------             ----  ----                       -------
  Normal  SuccessfulRescale  8s    horizontal-pod-autoscaler  New size: 2; reason: memory resource above target

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter bd3cba in the box below so that we can be sure you are a human.