OpenShift - Automatically scale a deployment using the oc autoscale command

An image contains the code used to create a deployment. Then, a deployment can be created from an image, which should then create a replica set (which is the number of pods that should be created), and then the pods should be created.

If you are not familiar with the oc command, refer to OpenShift - Getting Started with the oc command.

The oc get deployments command can be used to return the deployments in a project / namespace. Something like this should be returned. The DESIRED, CURRENT, UP-TO-DATE and AVAILABLE columns represent the number of replicas for the deployment.

Replicas is the number of pods that should be created for the deployment.

~]# oc get deployments
app001-5b9879db6d   1         1         1            1           205d


The DESIRED replicas is defined in the deployments YAML file, which can be seen with the --output yaml or --output json option.

~]# oc get deployments --output yaml | grep -i replicas
replicas: 1


Similarly, the oc get replicaset command can be used to display the DESIRED, CURRENT, and READY replicas for the deployments.

~]# oc get replicaset
NAME                      DESIRED   CURRENT   READY   AGE
app001-5b9879db6d         1         1         1       205d


You can update a replicaset or deployment to have a certain number of replicas:

  • Manually, using the oc scale command
  • Automatically, using the oc autoscale command (this article)

The oc autoscale command can be used to automatically update a replicaset and deployment to have a certain number of replicas based on some condition, such as the amount of CPU being used by the deployment. In this example, the deployment will always have a least 1 pod, and will automatically scale up to a maximum of 3 pods when a pod exceeds 50% used CPU.

The oc autoscale command does not have an option to autoscale based on memory used. However, you can use a template YAML file to autoscale based on memory used.

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


The oc get horizontalpodautoscaler command can then be used to list the autoscalers that have been created in the project / namespace.

~]$ oc get horizontalpodautoscaler
my-app   Deployment/my-app   <unknown>/50%   1         3         1          10m


And the oc describe horizontalpodautoscaler command can also be used to display information about an autoscaler.

~]$ oc describe horizontalpodautoscaler my-app
Name:                                                  my-app
Namespace:                                             default
Labels:                                                <none>
Annotations:                                           <none>
CreationTimestamp:                                     Tue, 23 Aug 2022 06:33:50 -0500
Reference:                                             Deployment/my-app
Metrics:                                               ( current / target )
  resource cpu on pods  (as a percentage of request):  <unknown> / 50%
Min replicas:                                          1
Max replicas:                                          3
Deployment pods:                                       1 current / 0 desired


Or, the oc get horizontalpodautoscaler command with the --output yaml or --output json option can be used.

~]$ oc get horizontalpodautoscaler my-app --output yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
  creationTimestamp: "2022-08-23T11:33:50Z"
  name: my-app
  namespace: default
  resourceVersion: "472074474"
  uid: 65ca2ce0-52e4-4565-becc-bb79742c4428
  maxReplicas: 3
  minReplicas: 1
    apiVersion: apps/v1
    kind: Deployment
    name: my-app
  targetCPUUtilizationPercentage: 50
  currentReplicas: 1
  desiredReplicas: 0


Did you find this article helpful?

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

Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

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


Web design by yours truely - me, myself, and I   |   |