Using the Application Autoscaler to Manage Your Workload

Objective

After completing this lesson, you will be able to use the Application Autoscaler to manage your workload.

Scale Cloud Foundry Applications with Application Autoscaler

Business Scenario

In this exercise, you'll learn how to scale Cloud Foundry applications using Application Autoscaler.

Let’s say that you're an application developer, and you have a shopping application running on BTP Cloud Foundry. The application receives high incoming traffic that increases the CPU usage. As a result, your application is taking longer to respond and the customer is facing performance issues.

This issue clearly states that it’s time to scale your application because of high CPU usage patterns. You can use Application Autoscaler, a service in BTP Cloud Foundry that automatically scales cf applications based on the defined scaling policy. It offers various scaling metrics, for example, CPU and memory utilization.

However, you'll leverage CPU utilization metric of Application Autoscaler service in this exercise.

Exercise Options

You can perform this exercise in two ways:

  1. Live Environment by using the instructions provided below, you can perform the steps in the SAP BTP Free Tier account.
  2. Platform Simulation – follow the step-by-step instructions within the simulation by clicking the ‘Start Exercise’ button below.

Note

It's strongly recommended that you perform the steps in the live environment first.

Task Flow

In this exercise, you'll perform the following tasks:

  1. Prepare a scaling policy with the CPU utilization metric.
  2. Deploy a sample application on Cloud Foundry.
  3. Create a service instance of Application Autoscaler.
  4. Attach (bind) a scaling policy to the application.
  5. Apply a load on the application.
  6. Monitor Application Scaling via CF CLI.
  7. Clean up (optional).

Prerequisites

  • You have created a Free Tier Account on SAP BTP. If not, follow this tutorial.
  • Cloud Foundry org and space should be available in the CF environment.
  • Cloud Foundry CLI is installed locally to interact with the CF environment. If not, follow this tutorial.
  • Download the sample CAP application from GitHub repo.

How to Obtain Support

To get support during the exercises, please add your question in our SAP BTP Learning Group.

Platform Simulation

As part of the exercise, you'll use CF CLI quite extensively to perform autoscaling of the application. Use your own CF environment, for example, free tier account to perform the exercise. However, the following CF environment is used in the exercise:

  • CF API Endpoint: https://api.cf.us10-001.hana.ondemand.com
  • CF org: ccc5375etrial
  • CF space: dev
  • Application name: hello-cloud-foundry-srv

Task 1: Prepare a Scaling Policy with CPU Utilization Metric

Let’s prepare a scaling policy for the application. The scaling policy defines the minimum and maximum number of instances. It also defines the scaling rules based on the metric type, threshold, operator, and adjustment.

Steps

  1. Clone the repository from GitHub (if you haven’t yet).

    1. Enter:

      git clone https://github.com/SAP-samples/cloud-foundry-runtime-learning-journey.git

    2. Navigate to the sample application directory:

      – cd cloud-foundry-runtime-learning-journey

  2. Build the application.

    1. Enter:

      npm install

    2. Enter:

      cds build --production

  3. Create a file cpu_scaling_policy.json with the following content:

    JSON
    12345678910111213141516171819202122
    { "instance_min_count": 1, "instance_max_count": 2, "scaling_rules": [ { "metric_type": "cpuutil", "breach_duration_secs": 120, "threshold": 80, "operator": ">", "cool_down_secs": 300, "adjustment": "+1" }, { "metric_type": "cpuutil", "breach_duration_secs": 120, "threshold": 80, "operator": "<", "cool_down_secs": 300, "adjustment": "-1" } ] }
    1. Enter the code from above.

      Let's understand the above scaling policy:

      • Scale out rule: If the CPU utilization is greater than 80% for two minutes (120 seconds), then add one instance to the application, provided the maximum instance count is not reached.
      • Scale in rule: If the CPU utilization is less than 80% for two minutes (120 seconds), then remove one instance from the application, provided the minimum instance count is not reached.

Task 2: Deploy Sample Application on Cloud Foundry

Steps

  1. Log in to the Cloud Foundry environment using the Cloud Foundry CLI.

    1. Enter:

      cf login --sso

      Target the cf org and space, where <org> and <space> are the org and space names respectively:

      cf target -o <org> -s <space>

  2. Push the application to Cloud Foundry without starting it.

    1. Enter:

      cf push -f manifest.yml --no-start

      Info: You used the CF manifest file (manifest.yml) to deploy the application on BTP Cloud Foundry. Another approach is to use a multitarget application (MTA). Check out the documentation for more details.

Task 3: Create a Service Instance of Application Autoscaler

Steps

  1. Create a service instance of Application Autoscaler with service instance name my-autoscaler-service, service plan standard, and service name autoscaler.

    1. Enter:

      cf create-service autoscaler standard my-autoscaler-service

Task 4: Attach (Bind) Scaling Policy with the Application

Steps

  1. Attach the autoscaler and scaling policy with the application.

    1. Enter:

      cf bind-service hello-cloud-foundry-srv my-autoscaler-service -c cpu_scaling_policy.json
  2. Start the application.

    1. Enter:

      cf start hello-cloud-foundry-srv
  3. Check if the application is in running state.

    cf app hello-cloud-foundry-srv

    Output:

    Code Snippet
    1234567891011121314151617
    # Output Showing health and status for app hello-cloud-foundry-srv in org ccc5375etrial / space name: hello-cloud-foundry-srv requested state: started isolation segment: trial routes: hello-cloud-foundry-srv.cfapps.us10-001.hana.ondemand.com last uploaded: Mon 02 Sep 12:20:35 CEST 2024 stack: cflinuxfs4 buildpacks: isolation segment: trial name version detect output buildpack name nodejs_buildpack 1.8.25 nodejs nodejs type: web sidecars: instances: 1/1 memory usage: 256M state since cpu memory disk details #0 running 2024-09-03T11:26:37Z 1.1% 70M of 256M 184.8M of 1G

    As shown in the output, the application is running with one instance at zero index.

  4. Access our deployed application.

    1. Grab the application route from the output of the previous command. To do so, enter:

      Code Snippet
      1
      cf app hello-cloud-foundry-srv | grep routes

      In our case, the application route is: https://hello-cloud-foundry-srv.cfapps.us10-001.hana.ondemand.com. If you access the URL, you'll see the welcome message of the application, which confirms that the application is running

Task 5: Apply a Load on the Application

Now, your application is attached to the autoscaler service and scaling policy.

Let’s apply some CPU load on the application to see the scaling in action.

Note

In practice, a Cloud Foundry platform automatically calculates the standard metrics, for example, CPU, memory, disk usage, and so on. These metrics are then read by the autoscaler service to perform scaling based on the scaling policy. However, if you scale based on the custom metrics (user-defined metrics), you need to submit the metrics to the autoscaler service.

Luckily, your sample application offers an endpoint ’/useCPU’ to artificially increase high cpu usage on the application.

Steps

  1. Open a browser, and issue an HTTP GET request to apply a CPU load on the application. Also, you are free to use any tool of your choice, for example, Postman for a HTTP client, or curl to send a HTTP request to the application.

    1. Enter:

      https://hello-cloud-foundry-srv.cfapps.us10-001.hana.ondemand.com/odata/v4/catalog/useCPU(action= 'start',duration='4')

      The output:

      JSON
      12345678
      { "@odata.context": "$metadata#Edm.String", "value": { "status": "started", "metric": "cpuutil", "service": "Application Autoscaler" } }

Result

You have successfully increased the CPU for 4 minutes. After 4 minutes, the CPU returns to normal.

Task 6: Monitor Application Scaling

In this task, you'll verify if the application is scaled based on our scaling policy (defined in Task 1).

Steps

  1. Check the application scaling status.

    1. Use the following command:

      cf app hello-cloud-foundry-srv

      Output:

      Code Snippet
      12345678910111213141516171819
      # Output Showing health and status for app hello-cloud-foundry-srv in org ccc5375etrial / space name: hello-cloud-foundry-srv requested state: started isolation segment: trial routes: hello-cloud-foundry-srv.cfapps.us10-001.hana.ondemand.com last uploaded: Tue 03 Sep 17:30:56 CEST 2024 stack: cflinuxfs4 buildpacks: isolation segment: trial name version detect output buildpack name nodejs_buildpack 1.8.25 nodejs nodejs type: web sidecars: instances: 2/2 memory usage: 256M state since cpu memory disk details #0 running 2024-09-03T15:38:43Z 0.9% 69.4M of 256M 184.8M of 1G #1 running 2024-09-03T15:36:56Z 0.9% 71.1M of 256M 184.8M of 1G

      As shown in the output, the application is scaled out to two instances based on the scaling policy (defined in Task 1) by the Application Autoscaler service.

      Now, relax and enjoy the performance of your application using the Application Autoscaler service.

Task 7: Clean Up (Optional)

Steps

  1. Unbind the autoscaler service from the application.

    1. Enter:

      cf unbind-service hello-cloud-foundry-srv my-autoscaler-service
  2. Delete the autoscaler service instance.

    1. Enter:

      cf delete-service my-autoscaler-service -f
  3. Delete the application.

    1. Enter:

      cf delete hello-cloud-foundry-srv -f

Result

Congratulations! You've successfully completed the exercise. You've learned how to scale Cloud Foundry applications using Application Autoscaler by following the following steps:

  • Defined a scaling policy based on CPU utilization metric.
  • Deployed a sample application on Cloud Foundry.
  • Created and bound the service instance of Application Autoscaler to the application.
  • Applied a load on the application to increase the CPU usage.
  • Finally, monitored application scaling and cleaned up the resources.

Further Reading / Reference Links

Log in to track your progress & complete quizzes