|
| 1 | +--- |
| 2 | +title: "Tutorial: Scale a container app with Java metrics" |
| 3 | +description: Scale a container app with Java metrics. |
| 4 | +services: azure-container-apps |
| 5 | +author: craigshoemaker |
| 6 | +ms.service: azure-container-apps |
| 7 | +ms.custom: devx-track-extended-java |
| 8 | +ms.topic: tutorial |
| 9 | +ms.date: 12/23/2024 |
| 10 | +ms.author: cshoe |
| 11 | +#customer intent: As a developer, I want to set up auto scale using Java metrics exposed from Azure Container Apps |
| 12 | +--- |
| 13 | + |
| 14 | +# Tutorial: Scale a container app with Java metrics |
| 15 | + |
| 16 | +Azure Container Apps manages automatic horizontal scaling through a set of declarative scaling rules. You can create your own scale rules with [customized event sources](./scale-app.md#custom). |
| 17 | + |
| 18 | +In this tutorial, you add a custom scale rule to scale your container app with Java metrics and observe how your application scales. |
| 19 | + |
| 20 | +## Prerequisites |
| 21 | + |
| 22 | +* An Azure account with an active subscription. If you don't already have one, you can [create one for free](https://azure.microsoft.com/free/). |
| 23 | +* [Azure CLI](/cli/azure/install-azure-cli). |
| 24 | +* [A Java application deployed in Azure Container Apps](java-get-started.md). |
| 25 | + |
| 26 | +## Set up the environment |
| 27 | + |
| 28 | +Use the following steps to define environment variables and set up the environment. |
| 29 | + |
| 30 | +1. Create variables to support your scale configuration. |
| 31 | + |
| 32 | + ```bash |
| 33 | + export LOCATION=eastus |
| 34 | + export TENANT_ID={tenant-id} |
| 35 | + export SUBSCRIPTION_ID={subscription-id} |
| 36 | + export RESOURCE_GROUP=my-resource-group |
| 37 | + export APP_NAME=my-aca-app |
| 38 | + export IDENTITY_NAME=my-identity |
| 39 | + ``` |
| 40 | + |
| 41 | + | Variable | Description | |
| 42 | + |-------------------------|------------------------------------------------------------------------------------| |
| 43 | + | `LOCATION` | The Azure region location where you create your Azure Container Apps. | |
| 44 | + | `TENANT_ID` | Your tenant's ID. | |
| 45 | + | `SUBSCRIPTION_ID` | The subscription ID which you use to create your Azure Container Apps. | |
| 46 | + | `RESOURCE_GROUP` | The Azure resource group name for your Azure Container Apps. | |
| 47 | + | `APP_NAME` | The app name for your Azure Container Apps. | |
| 48 | + | `IDENTITY_NAME` | The name for your managed identity, which is assigned to your Azure Container Apps.| |
| 49 | +
|
| 50 | +1. Sign in to Azure with the Azure CLI. |
| 51 | +
|
| 52 | + ```azurecli |
| 53 | + az login |
| 54 | + ``` |
| 55 | +
|
| 56 | +## Set up a managed identity for your Azure Container Apps |
| 57 | +To scale with Azure Container Apps platform metrics, you need a managed identity to access metrics from Azure Monitor. |
| 58 | +
|
| 59 | +1. Create a user-assigned identity and assign it to your Azure Container Apps. You can follow the doc [add a user-assigned identity](./managed-identity.md#add-a-user-assigned-identity). After you create the identity, run the CLI command to set the identity ID. |
| 60 | +
|
| 61 | + ```azurecli |
| 62 | + USER_ASSIGNED_IDENTITY_ID=$(az identity show --resource-group $RESOURCE_GROUP --name $IDENTITY_NAME --query "id" --output tsv) |
| 63 | + ``` |
| 64 | +
|
| 65 | +2. Grant the `Monitoring Reader` role for your managed identity to read data from Azure Monitor. You can find more details about the roles for Azure Monitor in [Azure built-in roles for Monitor](../role-based-access-control/built-in-roles/monitor.md#monitoring-reader). |
| 66 | +
|
| 67 | + ```azurecli |
| 68 | + # Get the principal ID for your managed identity |
| 69 | + PRINCIPAL_ID=$(az identity show --resource-group $RESOURCE_GROUP --name $IDENTITY_NAME --query "principalId" --output tsv) |
| 70 | + |
| 71 | + az role assignment create --assignee $PRINCIPAL_ID --role "Monitoring Reader" --scope /subscriptions/$SUBSCRIPTION_ID |
| 72 | + ``` |
| 73 | +
|
| 74 | +## Add a scale rule with Azure Monitor metrics |
| 75 | +To scale with Azure Monitor metrics, you can refer to [Azure Monitor KEDA scaler](https://keda.sh/docs/2.16/scalers/azure-monitor/) to define your Container Apps scale rule. |
| 76 | +
|
| 77 | +Here's a list of core metadata to set up the scale rule. |
| 78 | + |
| 79 | +| Metadata key | Description | |
| 80 | +|------------------------------------|-------------------------------------------------------------------------------------------------------| |
| 81 | +| tenantId | ID of the tenant that contains the Azure resource. | |
| 82 | +| subscriptionId | ID of the Azure subscription that contains the Azure resource. | |
| 83 | +| resourceGroupName | Name of the resource group for the Azure resource. | |
| 84 | +| resourceURI | Shortened URI to the Azure resource with format `<resourceProviderNamespace>/<resourceType>/<resourceName>`. | |
| 85 | +| metricName | Name of the metric to query. | |
| 86 | +| metricAggregationType | Aggregation method of the Azure Monitor metric. Options include Average, Total, Maximum. | |
| 87 | +| metricFilter | Name of the filter to be more specific by using dimensions listed in the official documentation. (Optional) | |
| 88 | +| metricAggregationInterval | Collection time of the metric in format "hh:mm:ss" (Default: "0:5:0", Optional) | |
| 89 | +| targetValue | Target value to trigger scaling actions. (This value can be a float) | |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +Add a scale rule with [Azure Monitor metrics for Azure Container Apps](./metrics.md) for your application. |
| 94 | + |
| 95 | +### [Azure CLI](#tab/azurecli) |
| 96 | + |
| 97 | +```azurecli |
| 98 | +az containerapp update \ |
| 99 | + --name $APP_NAME \ |
| 100 | + --resource-group $RESOURCE_GROUP \ |
| 101 | + --min-replicas 1 \ |
| 102 | + --max-replicas 10 \ |
| 103 | + --scale-rule-name scale-with-azure-monitor-metrics \ |
| 104 | + --scale-rule-type azure-monitor \ |
| 105 | + --scale-rule-metadata "tenantId=${TENANT_ID}" \ |
| 106 | + "subscriptionId=${SUBSCRIPTION_ID}" \ |
| 107 | + "resourceGroupName=${RESOURCE_GROUP}" \ |
| 108 | + "resourceURI=Microsoft.App/containerapps/${APP_NAME}" \ |
| 109 | + "metricName=JvmGcCount" \ |
| 110 | + "metricAggregationType=Total" \ |
| 111 | + "metricAggregationInterval=0:1:0" \ |
| 112 | + "targetValue=30" \ |
| 113 | + --scale-rule-identity $USER_ASSIGNED_IDENTITY_ID |
| 114 | +``` |
| 115 | + |
| 116 | +### [ARM Template](#tab/arm-template) |
| 117 | + |
| 118 | +```json |
| 119 | +{ |
| 120 | + "resources": { |
| 121 | + "properties": { |
| 122 | + "template": { |
| 123 | + "scale": { |
| 124 | + "minReplicas": 1, |
| 125 | + "maxReplicas": 10, |
| 126 | + "rules": [ |
| 127 | + { |
| 128 | + "name": "scale-with-azure-monitor-metrics", |
| 129 | + "custom": { |
| 130 | + "type": "azure-monitor", |
| 131 | + "metadata": { |
| 132 | + "metricAggregationInterval": "0:1:0", |
| 133 | + "metricAggregationType": "Total", |
| 134 | + "metricName": "JvmGcCount", |
| 135 | + "resourceGroupName": "<your-resource-group>", |
| 136 | + "resourceURI": "Microsoft.App/containerapps/<your-app>", |
| 137 | + "subscriptionId": "<your-subscription-id>", |
| 138 | + "targetValue": "30", |
| 139 | + "tenantId": "<your-tenant-id>" |
| 140 | + }, |
| 141 | + "identity": "<your-managed-identity-id>" |
| 142 | + } |
| 143 | + } |
| 144 | + ] |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +--- |
| 153 | + |
| 154 | +This command adds a scale rule to your container app with the name `scale-with-azure-monitor-metrics` |
| 155 | +- The scale type is set to `azure-monitor`. |
| 156 | +- KEDA uses the managed identity with resource ID `USER_ASSIGNED_IDENTITY_ID` to authenticate with Azure Monitor and query metrics for your container app. |
| 157 | +- KEDA queries the metric `JvmGcCount`, and aggregates the metric values within 1 minute with aggregation type `Total`. |
| 158 | +- The target value is set to `30`, which means KEDA calculates the `desiredReplicas` using `ceil(AggregatedMetricValue(JvmGcCount)/30)`. |
| 159 | + |
| 160 | +> [!NOTE] |
| 161 | +> The metric `JvmGcCount` is only used as an example. You can use any metric from Azure Monitor. Before setting up the scale rule, view the metrics in the Azure portal to determine the appropriate metric, aggregation interval, and target value based on your application's requirements. Additionally, consider using the built-in [HTTP/TCP scale rules](./scale-app.md#http), which can meet most common scaling scenarios, before opting for a custom metric. |
| 162 | +
|
| 163 | +## View scaling in Azure portal (optional) |
| 164 | +Once your new revision is ready, [send requests](./tutorial-scaling.md#send-requests) to your container app to trigger auto scale with your Java metrics. |
| 165 | +1. Go to the `Metrics` blade in the Azure portal for your Azure Container Apps. |
| 166 | +1. Add a chart, use the metric `jvm.gc.count`, with filter `Revision=<your-revision>`, aggregation using `Sum`, and split by `Replica`. You can see the `JvmGcCount` metric value for each replica in this chat. |
| 167 | +1. Add a chart, use the metric `jvm.gc.count`, with filter `Revision=<your-revision>` and aggregation using `Sum`. You can see the total aggregated `JvmGcCount` metric value for the revision in this chat. |
| 168 | +1. Add a chart, use the metric `Replica Count`, with filter `Revision=<your-revision>` and aggregation using `Max`. You can see the replica count for the revision in this chat. |
| 169 | +
|
| 170 | +
|
| 171 | +Here's a sample metric snapshot for the example scale rule. |
| 172 | + |
| 173 | +:::image type="content" source="media/java-metrics-keda/keda-auto-scale-java-gc-portal.png" alt-text="Screenshot of KEDA scale with JVM metrics." lightbox="media/java-metrics-keda/keda-auto-scale-java-gc-portal.png"::: |
| 174 | + |
| 175 | +1. Initially, there's one replica (the `minReplicas`) for the app. |
| 176 | +1. A spike in requests causes the Java app to experience frequent JVM garbage collection (GC). |
| 177 | +1. KEDA observes the aggregated metric value for `jvm.gc.count` is increased to `256`, and calculates the `desiredReplicas` value as `ceil(256/30)=9`. |
| 178 | +1. KEDA scales out the container app's replica count to 9. |
| 179 | +1. The http traffic is distributed across more replicas, reducing the average GC count. |
| 180 | +1. The GC count further decreases when no requests are coming in. |
| 181 | +1. After a cooldown period, KEDA scales the replica count down to `minReplicas=1`. |
| 182 | + |
| 183 | +## Scale Log |
| 184 | + |
| 185 | +To view the KEDA scale logs, you can run the query in `Logs` blade. |
| 186 | + |
| 187 | +```kusto |
| 188 | +ContainerAppSystemLogs |
| 189 | +| where RevisionName == "<your-revision>" |
| 190 | +| where EventSource == "KEDA" |
| 191 | +| project TimeGenerated, Type, Reason, ContainerAppName, Log |
| 192 | +``` |
| 193 | + |
| 194 | +:::image type="content" source="media/java-metrics-keda/keda-auto-scale-java-log.png" alt-text="Screenshot of KEDA scale log query." lightbox="media/java-metrics-keda/keda-auto-scale-java-log.png"::: |
| 195 | + |
| 196 | +## Clean up resources |
| 197 | + |
| 198 | +The resources created in this tutorial have an effect on your Azure bill. If you aren't going to use these services long-term, run the following command to remove everything created in this tutorial. |
| 199 | +
|
| 200 | +```azurecli |
| 201 | +az group delete --resource-group $RESOURCE_GROUP |
| 202 | +``` |
| 203 | +
|
| 204 | +## Related content |
| 205 | +
|
| 206 | +> [!div class="nextstepaction"] |
| 207 | +- [Set scaling rules in Azure Container Apps](./scale-app.md) |
| 208 | +- [Java metrics for Azure Container Apps](./java-metrics.md) |
0 commit comments