使用 Application Autoscaler 管理工作负载
学习目标
使用 Application Autoscaler 管理您的工作负载。
业务场景
在本练习中,您将了解如何使用 SAP BTP Application Autoscaler 服务扩展 Cloud Foundry 应用程序。
假设您是管理托管在 SAP BTP Cloud Foundry 上的购物应用程序的应用程序开发人员。应用程序的传入流量较高,导致 CPU 使用率增加。因此,应用程序的响应时间正在减慢,从而导致客户出现性能问题。
由于 CPU 使用模式较高,该场景突出显示了扩展应用程序的需求。SAP BTP Cloud Foundry 中的 Application Autoscaler 服务可以根据定义的扩缩策略自动扩展 Cloud Foundry 应用程序。它支持各种扩缩指标,例如 CPU 和内存利用率。
在本练习中,您将使用 Application Autoscaler 服务的 CPU 利用率指标。
行使选项
您可以通过两种方式执行此练习:
实时环境通过使用下面提供的说明,您可以在 SAP BTP 免费层级账户中执行步骤。
平台模拟 – 单击下面的"开始练习"按钮,按照模拟中的逐步说明进行操作。
注意
强烈建议您首先在实时环境中执行这些步骤。
任务流
在本练习中,您将执行以下任务:
使用 CPU 利用率指标准备扩缩策略
在 Cloud Foundry 上部署示例应用程序
创建 Application Autoscaler 的服务实例
将扩缩策略附加(绑定)到应用程序
在应用上应用负载
通过 CF CLI 监控应用程序扩展
清理(可选)
前提条件
您已在 SAP BTP 上创建免费层级账户。如果没有, 请按本教程进行操作 。
Cloud Foundry 组织和空间应在 CF 环境中可用。
已在本地安装 Cloud Foundry CLI 以与 CF 环境进行交互。如果没有, 请按本教程进行操作 。
从 GitHub 资源库 下载示例 CAP 应用程序。
如何获得支持
要在练习期间获得支持,请在我们的 SAP BTP 学习组中添加您的问题。
平台模拟
作为练习的一部分,您将广泛使用 CF CLI 来执行应用程序的自动扩缩。使用自己的 CF 环境(例如,免费层级账户)执行练习。但是,练习中使用了以下 CF 环境:
CF 组织:ccc5375etrial
CF 空间:开发
应用程序名称:hello-cloud-foundry-srv
练习:开始练习(SAP 官方交互式练习)
任务 1: 使用 CPU 利用率指标准备扩缩策略
让我们为应用程序准备扩缩策略。扩缩策略定义实例的最小和最大数量。它还根据指标类型、阈值、运算符和调整定义扩缩规则。
步骤
从 GitHub 克隆资源库(如果尚未克隆)。
输入:
git clone https://github.com/SAP-samples/cloud-foundry-runtime-learning-journey.git导航到示例应用程序目录:
– cd cloud-foundry-runtime-learning-journey构建应用程序。
输入:
npm install输入:
cds build --production使用以下内容创建文件 cpu_scaling_policy.json:
{
"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"
}
]
}
输入上述代码。 让我们了解一下上述扩缩策略:
向外扩展规则 :如果 CPU 利用率在两分钟(120 秒)内大于 80%,则向应用程序添加一个实例,前提是未达到最大实例计数。
规则缩放 :如果 CPU 利用率在两分钟(120 秒)内小于 80%,则从应用程序中移除一个实例,前提是未达到最小实例计数。
任务 2: 在 Cloud Foundry 上部署示例应用程序
步骤
使用 Cloud Foundry CLI 登录到 Cloud Foundry 环境。
输入:
cf login --sso将 cf 组织 和 空间 作为目标,其中<org>和<space>分别是组织和空间名称:cf target -o <org> -s <space>将应用程序推送到 Cloud Foundry 而不启动。
输入: cf push -f manifest.yml --no-start 信息:您使用 CF 清单文件 (manifest.yml) 在 BTP Cloud Foundry 上部署应用程序。另一种方法是使用多目标应用程序 (MTA)。有关详细信息,请查看文档。
任务 3: 创建 Application Autoscaler 的服务实例
步骤
使用服务实例名称 my-autoscaler-service、服务计划标准和服务名称 autoscaler 创建 Application Autoscaler 的服务实例。
输入: cf create-service autoscaler standard my-autoscaler-service
任务 4: 将(绑定)扩缩策略附加到应用程序
步骤
将自动扩缩器和扩缩策略与应用程序附加。
输入: cf bind-service hello-cloud-foundry-srv my-autoscaler-service -c cpu_scaling_policy.json
启动应用程序。
输入: cf start hello-cloud-foundry-srv
检查应用程序是否处于运行状态。
cf app hello-cloud-foundry-srv输出:
# 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
如 输出中所示,应用程序正使用一个零索引实例运行。 4. 访问我们部署的应用程序。
从上一 命令的输出中获取应用程序路由。为此,请输入:
cf app hello-cloud-foundry-srv | grep routes
在我们的案例中,应用程序路由为:*https://hello-cloud-foundry-srv.cfapps.us10-001.hana.ondemand.com*。如果访问 URL,您将看到应用程序的欢迎消息,确认应用程序正在运行
任务 5: 在应用程序上应用负载
现在,您的应用程序通过扩缩策略附加到自动扩缩器服务。
让我们在应用程序上应用一些 CPU 负载,以查看扩缩操作。
注意
实际上,Cloud Foundry 平台自动计算标准指标,例如 CPU、内存、磁盘使用情况等。然后,自动扩缩服务读取这些指标,以根据扩缩策略执行扩展。但是,如果基于自定义指标(用户定义的指标)进行扩展,则需要将指标提交到自动扩缩器服务。
幸运的是,您的示例应用程序提供端点"/useCPU",以人工增加应用程序的高 CPU 使用率。
步骤
打开浏览器,并发出 HTTP GET 请求以在应用程序上应用 CPU 负载。此外,您还可以自由使用任何所选的工具,例如,Postman 代表 HTTP 客户端,或者使用 curl 向应用程序发送 HTTP 请求。
输入:
https://hello-cloud-foundry-srv.cfapps.us10-001.hana.ondemand.com/odata/v4/catalog/useCPU(action= 'start',duration='4')输出:
{
"@odata.context": "$metadata#Edm.String",
"value": {
"status": "started",
"metric": "cpuutil",
"service": "Application Autoscaler"
}
}
结果
已成功将 CPU 增加 4 分钟。4 分钟后,CPU 恢复正常。
任务 6: 监控应用程序扩缩
在此任务中,您将验证应用程序是否基于我们的扩缩策略进行扩展(在任务 1 中定义)。
步骤
检查应用程序扩缩状态。
使用以下命令: cf app hello-cloud-foundry-srv 您可以重复使用上述命令,例如,watch cf app hello-cloud-foundry-srv。2-3 分钟后,您将看到实例数量增加。 输出:
# 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
如 输出中所示,应用程序将根据 Application Autoscaler 服务的扩缩策略(在任务 1 中定义)扩展到两个实例。 现在,使用 Application Autoscaler 服务放松并享受应用程序的性能。
任务 7: 清理(可选)
步骤
从应用程序中取消绑定 autoscaler 服务。
输入: cf unbind-service hello-cloud-foundry-srv my-autoscaler-service
删除自动扩缩程序服务实例。
输入: cf delete-service my-autoscaler-service -f
删除应用程序。
输入:
cf delete hello-cloud-foundry-srv -f
结果
恭喜!您已成功完成练习。您已了解如何通过以下步骤使用 Application Autoscaler 扩展 Cloud Foundry 应用程序:
基于 CPU 利用率指标定义扩缩策略。
已在 Cloud Foundry 上部署示例应用程序。
已创建 Application Autoscaler 的服务实例并将其绑定到应用程序。
对应用程序应用负载以增加 CPU 使用率。
最后,监控应用程序扩缩并清理资源。
进一步阅读/参考链接
了解有关 Application Autoscaler 服务 的详细信息
了解 扩缩策略配置参数
使用 Application Autoscaler 仪表盘监控扩缩历史记录
使用基于计划的动态 扩缩策略文档
使用 CPU 利用率指标的应用程序可扩展性 - 分步指南