Business Scenario
Exercise Options
Task Flow
Prerequisites
How to Obtain Support
Platform Simulation
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
Clone the repository from GitHub (if you haven’t yet).
Enter:
git clone https://github.com/SAP-samples/cloud-foundry-runtime-learning-journey.git
Navigate to the sample application directory:
– cd cloud-foundry-runtime-learning-journey
Build the application.
Enter:
Enter:
Create a file cpu_scaling_policy.json with the following content:
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"
}
]
}
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
Log in to the Cloud Foundry environment using the Cloud Foundry CLI.
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>
Push the application to Cloud Foundry without starting it.
Enter:
cf push -f manifest.yml --no-startInfo: 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
Create a service instance of Application Autoscaler with service instance name my-autoscaler-service, service plan standard, and service name autoscaler.
Enter:
cf create-service autoscaler standard my-autoscaler-service
Task 4: Attach (Bind) Scaling Policy with the Application
Steps
Attach the autoscaler and scaling policy with the application.
Enter:
cf bind-service hello-cloud-foundry-srv my-autoscaler-service -c cpu_scaling_policy.json
Start the application.
Enter:
cf start hello-cloud-foundry-srv
Check if the application is in running state.
cf app hello-cloud-foundry-srv
Output:
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.
Access our deployed application.
Grab the application route from the output of the previous command. To do so, enter:
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 Load on the Application
Now, your application is attached to the autoscaler service with 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
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.
Enter:
https://hello-cloud-foundry-srv.cfapps.us10-001.hana.ondemand.com/odata/v4/catalog/useCPU(action= 'start',duration='4')
The output:
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
Check the application scaling status.
Use the following command:
cf app hello-cloud-foundry-srvYou can repeatedly use the above command, for example, watch cf app hello-cloud-foundry-srv. After 2-3 minutes, you will observe that the number of instances increases.
Output:
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
Unbind the autoscaler service from the application.
Enter:
cf unbind-service hello-cloud-foundry-srv my-autoscaler-service
Delete the autoscaler service instance.
Enter:
cf delete-service my-autoscaler-service -f
Delete the application.
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 these 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