Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Testing memory-based horizontal pod autoscaling on OpenShift

March 19, 2020
Mohammad Ahmad
Related topics:
ContainersKubernetes

    Red Hat OpenShift offers horizontal pod autoscaling (HPA) primarily for CPUs, but it can also perform memory-based HPA, which is useful for applications that are more memory-intensive than CPU-intensive. In this article, I demonstrate how to use OpenShift's memory-based horizontal pod autoscaling feature (tech preview) to autoscale your pods if the demands on memory increase. The test performed in this article might not necessarily reflect a real application. The tests only aim to demonstrate memory-based HPA in the simplest way possible.

    I use a simple PHP application (index.php) that creates a large array in memory every time a request is made. The code looks like this:

    <?php
    
    $arr = array();
    $arr_size = 100000;
    
    for ($i=1;$i<=$arr_size;$i++) {
    $arr[] = $i;
    }
    
    echo "created an array of $arr_size entries";
    
    ?>
    

    You can perform this test with any language. The load test works by creating multiple parallel curl requests to the application. I chose PHP for its personal convenience.

    Check if v2beta1 is enabled

    Check if v2beta1 is enabled in your cluster:

    # oc get --raw /apis/autoscaling/v2beta1
    {"kind":"APIResourceList","apiVersion":"v1","groupVersion":"autoscaling/v2beta1","resources":[{"name":"horizontalpodautoscalers","singularName":"","namespaced":true,"kind":"HorizontalPodAutoscaler","verbs":["create","delete","deletecollection","get","list","patch","update","watch"],"shortNames":["hpa"],"categories":["all"]},{"name":"horizontalpodautoscalers/status","singularName":"","namespaced":true,"kind":"HorizontalPodAutoscaler","verbs":["get","patch","update"]}]}
    

    If it isn’t enabled, follow the documentation for enabling it on your respective OpenShift version.

    Set up your testbed

    Before you can test, you need to set up the environment you're testing in and the application you're testing.

    Set up your environment

    To set up your new environment:

    1. Create a new project:
    # oc new-project memhpa
    Now using project "memhpa" on server "https://console.ocp.mylab:8443".
    
    1. Create an image stream (this assumes that you have your authentication set up correctly):
    # oc import-image myphp --insecure --from=registry.redhat.io/openshift3/php-55-rhel7:latest --confirm
    imagestream.image.openshift.io/myphp imported
    
    1. Apply limits to the namespaces before creating your first application:
    # cat limits.yml 
    apiVersion: "v1"
    kind: "LimitRange"
    metadata:
      name: "myphp-resource-limits"
    spec:
      limits:
        - type: "Pod"
          max:
            cpu: "2"
            memory: "120Mi"
          min:
            cpu: "200m"
            memory: "6Mi"
        - type: "Container"
          max:
            cpu: "2"
            memory: "120Mi"
          min:
            cpu: "100m"
            memory: "4Mi"
          default:
            cpu: "300m"
            memory: "100Mi"
          defaultRequest:
            cpu: "200m"
            memory: "100Mi"
          maxLimitRequestRatio:
            cpu: "10"
    
    # oc create -f limits.yml
    limitrange/myphp-resource-limits created

    Set up the application

    To set up your application:

    1. Create the application:
    # oc new-app --name app1 myphp:latest~https://gitlab.mylab/myproject/phpapp.git --build-env "GIT_SSL_NO_VERIFY=true"
    
    1. Wait until your application is built and running:
    # oc get pods
    NAME READY STATUS RESTARTS AGE
    app1-1-build 0/1 Completed 0 2m
    app1-1-plchw 1/1 Running 0 27s
    
    1. Create a route:
    # oc expose svc app1
    route/app1-memhpa.apps.ocp.mylab created
    

    Test your application

    Now, to test your application:

    1. Test the application:
    # curl http://app1-memhpa.apps.ocp.mylab/
    created an array of 100000
    

    If your application fails, try reducing the size of the PHP array.

    1. Create a memory-based HPA definition:
    # cat hpa.yml 
    apiVersion: autoscaling/v2beta1
    kind: HorizontalPodAutoscaler
    metadata:
      name: hpa-resource-metrics-memory
    spec:
      scaleTargetRef:
        apiVersion: apps.openshift.io/v1
        kind: DeploymentConfig
        name: app1
      minReplicas: 1
      maxReplicas: 10
      metrics:
      - type: Resource
        resource:
          name: memory
          targetAverageUtilization: 90
    
    # oc create -f hpa.yml
    horizontalpodautoscaler.autoscaling/hpa-resource-metrics-memory created
    
    # oc get hpa
    NAME                          REFERENCE               TARGETS         MINPODS   MAXPODS   REPLICAS   AGE
    hpa-resource-metrics-memory   DeploymentConfig/app1   <unknown>/90%   1         10        0          31m
    
    1. Wait until the <unknown> value shown above changes to an integer:
    # oc get hpa
    NAME                          REFERENCE               TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
    hpa-resource-metrics-memory   DeploymentConfig/app1   75%/90%   1         10        1          33m
    
    1. Create a load. I use a simple script that loops through a curl command:
    # cat loadphp.sh
    #!/bin/bash
    
    while true; do curl http://app1-memhpa.apps.ocp.mylab/; done
    
    done
    
    1. Run the following command a few times until you notice the load increasing:
    # nohup ./loadphp.sh &

    Observe the HPA

    You will start to notice an increase in memory utilization and corresponding autoscaling:

    # oc get hpa
    NAME                          REFERENCE               TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
    hpa-resource-metrics-memory   DeploymentConfig/app1   75%/90%   1         10        1          33m
    
    # oc get hpa
    NAME                          REFERENCE               TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
    hpa-resource-metrics-memory   DeploymentConfig/app1   94%/90%   1         10        2          39m
    
    # oc get hpa
    NAME                          REFERENCE               TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
    hpa-resource-metrics-memory   DeploymentConfig/app1   90%/90%   1         10        2          48m
    
    # oc get hpa
    NAME                          REFERENCE               TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
    hpa-resource-metrics-memory   DeploymentConfig/app1   85%/90%   1         10        3          52m
    

    Next, stop the load, and then watch the HPA. Several minutes after the load stops, the autoscaler eventually downscales the pods to one:

    NAME                          REFERENCE               TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
    hpa-resource-metrics-memory   DeploymentConfig/app1   29%/90%   1         10        1          1h
    

    Conclusion

    In this article, my aim was to provide the simplest of methods to set up and test memory-based horizontal pod autoscaling. I was able to demonstrate this process with a single PHP web page that creates a large array in memory, built using a basic Red Hat S2I PHP image, and set up in a namespace with limits and an HPA.

    Once I set up the environment, I created a basic bash script to put a load on the application in order to observe an increasing load in memory, until the result is multiple autoscaled pods. After stopping the load, in a few minutes, the autoscaler reduced the pods down to one.

    Special thanks to Damon Hatchett for peer-reviewing this article.

    Last updated: June 29, 2020

    Recent Posts

    • Confidential virtual machine storage attack scenarios

    • Introducing virtualization platform autopilot

    • Integrate zero trust workload identity manager with Red Hat OpenShift GitOps

    • Best Practice Configuration and Tuning for Linux and Windows VMs

    • Red Hat UBI 8 builders have been promoted to the Paketo Buildpacks organization

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility