Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Testing memory-based horizontal pod autoscaling on OpenShift

March 19, 2020
Mohammad Ahmad
Related topics:
ContainersKubernetes

Share:

    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

    • Exploring Llama Stack with Python: Tool calling and agents

    • Enhance data security in OpenShift Data Foundation

    • AI meets containers: My first step into Podman AI Lab

    • Live migrating VMs with OpenShift Virtualization

    • Storage considerations for OpenShift Virtualization

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    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
    © 2025 Red Hat

    Red Hat legal and privacy links

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

    Report a website issue