Raw New Markdown
Generating updated version of doc...
Rendered New Markdown
Generating updated version of doc...
---
title: 'Tutorial: Scale an Azure Container Apps application'
description: Scale an Azure Container Apps application using the Azure CLI.
services: container-apps
author: craigshoemaker
ms.service: azure-container-apps
ms.topic: tutorial
ms.date: 03/26/2025
ms.author: cshoe
ms.custom: devx-track-azurecli
ms.devlang: azurecli
---
# Tutorial: Scale a container app
Azure Container Apps manages automatic horizontal scaling through a set of declarative scaling rules. As a container app scales out, new instances of the container app are created on-demand. These instances are known as replicas.
In this tutorial, you add an HTTP scale rule to your container app and observe how your application scales.
## Prerequisites
| Requirement | Instructions |
|--|--|
| Azure account | If you don't have an Azure account, you can [create one for free](https://azure.microsoft.com/pricing/purchase-options/azure-account?cid=msft_learn). <br><br>You need the *Contributor* permission on the Azure subscription to proceed. Refer to [Assign Azure roles using the Azure portal](/azure/role-based-access-control/role-assignments-portal?tabs=current) for details. |
| GitHub Account | Get one for [free](https://github.com/join). |
| Azure CLI | Install the [Azure CLI](/cli/azure/install-azure-cli). |
[!INCLUDE [container-apps-create-cli-steps.md](../../includes/container-apps-create-cli-steps.md)]
## Create and deploy the container app
Create and deploy your container app with the `containerapp up` command. This command creates a:
- Resource group
- Container Apps environment
- Log Analytics workspace
If any of these resources already exist, the command uses the existing resources rather than creating new ones.
Lastly, the command creates and deploys the container app using a public container image, `mcr.microsoft.com/dotnet/samples:aspnetapp`. This image is used to trigger the scale rules you create in this article. You don't need to know or use .NET to complete this procedure.
# [Bash](#tab/bash)
```azurecli
az containerapp up \
--name my-container-app \
--resource-group my-container-apps \
--location centralus \
--environment 'my-container-apps' \
--image mcr.microsoft.com/dotnet/samples:aspnetapp \
--target-port 8080 \
--ingress external \
--query properties.configuration.ingress.fqdn \
```
# [PowerShell](#tab/powershell)
```powershell
az containerapp up `
--name my-container-app `
--resource-group my-container-apps `
--location centralus `
--environment my-container-apps `
--image mcr.microsoft.com/dotnet/samples:aspnetapp `
--target-port 8080 `
--ingress external `
--query properties.configuration.ingress.fqdn `
```
---
> [!NOTE]
> Make sure the value for the `--image` parameter is in lower case.
By setting `--ingress` to `external`, you make the container app available to public requests.
The `up` command returns the fully qualified domain name (FQDN) for the container app. Copy this FQDN to a text file. You use it in the [Send requests](#send-requests) section. Your FQDN looks like the following example:
```text
https://my-container-app.icydune-96848328.centralus.azurecontainerapps.io
```
## Add scale rule
Add an HTTP scale rule to your container app by running the `az containerapp update` command.
# [Bash](#tab/bash)
```azurecli
az containerapp update \
--name my-container-app \
--resource-group my-container-apps \
--min-replicas 1 \
--max-replicas 10 \
--scale-rule-name my-http-scale-rule \
--scale-rule-http-concurrency 1
```
# [PowerShell](#tab/powershell)
```powershell
az containerapp update `
--name my-container-app `
--resource-group my-container-apps `
--min-replicas 1 `
--max-replicas 10 `
--scale-rule-name my-http-scale-rule `
--scale-rule-http-concurrency 1
```
---
This command adds an HTTP scale rule to your container app with the name `my-http-scale-rule` and a concurrency setting of `1`. If your app receives more than one concurrent HTTP request, the runtime creates replicas of your app to handle the requests.
The `update` command returns the new configuration as a JSON response to verify your request was a success.
## Start log output
You can observe the effects of your application scaling by viewing the logs generated by the Container Apps runtime. Use the `az containerapp logs show` command to start listening for log entries.
# [Bash](#tab/bash)
```azurecli
az containerapp logs show \
--name my-container-app \
--resource-group my-container-apps \
--type=system \
--follow=true
```
# [PowerShell](#tab/powershell)
```powershell
az containerapp logs show `
--name my-container-app `
--resource-group my-container-apps `
--type=system `
--follow=true
```
---
The `show` command returns entries from the system logs for your container app in real time. You can expect a response like the following example:
```json
{
"TimeStamp":"2023-08-01T16:49:03.02752",
"Log":"Connecting to the container 'my-container-app'..."
}
{
"TimeStamp":"2023-08-01T16:49:03.04437",
"Log":"Successfully Connected to container:
'my-container-app' [Revision: 'my-container-app--9uj51l6',
Replica: 'my-container-app--9uj51l6-5f96557ffb-5khg9']"
}
{
"TimeStamp":"2023-08-01T16:47:31.9480811+00:00",
"Log":"Microsoft.Hosting.Lifetime[14]"
}
{
"TimeStamp":"2023-08-01T16:47:31.9481264+00:00",
"Log":"Now listening on: http://[::]:80"
}
{
"TimeStamp":"2023-08-01T16:47:31.9490917+00:00",
"Log":"Microsoft.Hosting.Lifetime[0]"
}
{
"TimeStamp":"2023-08-01T16:47:31.9491036+00:00",
"Log":"Application started. Press Ctrl+C to shut down."
}
{
"TimeStamp":"2023-08-01T16:47:31.949723+00:00",
"Log":"Microsoft.Hosting.Lifetime[0]"
}
{
"TimeStamp":"2023-08-01T16:47:31.9497292+00:00",
"Log":"Hosting environment: Production"
}
{
"TimeStamp":"2023-08-01T16:47:31.9497325+00:00",
"Log":"Microsoft.Hosting.Lifetime[0]"
}
{
"TimeStamp":"2023-08-01T16:47:31.9497367+00:00",
"Log":"Content root path: /app/"
}
```
For more information, see [az containerapp logs](/cli/azure/containerapp/logs).
## Send requests
# [Bash](#tab/bash)
Open a new bash shell. Run the following command, replacing `<YOUR_CONTAINER_APP_FQDN>` with the fully qualified domain name for your container app that you saved from the [Create and deploy the container app](#create-and-deploy-the-container-app) section.
```bash
seq 1 50 | xargs -Iname -P10 curl "<YOUR_CONTAINER_APP_FQDN>"
```
These commands send 50 requests to your container app in concurrent batches of 10 requests each.
| Command or argument | Description |
|---|---|
| `seq 1 50` | Generates a sequence of numbers from 1 to 50. |
| `|` | The pipe operator sends the sequence to the `xargs` command. |
| `xargs` | Runs `curl` with the specified URL |
| `-Iname` | Acts as a placeholder for the output of `seq`. This argument prevents the return value from being sent to the `curl` command. |
| `curl` | Calls the given URL. |
| `-P10` | Instructs `xargs` to run up to 10 processes at a time. |
For more information, see the documentation for:
- [seq](https://www.man7.org/linux/man-pages/man1/seq.1.html)
- [xargs](https://www.man7.org/linux/man-pages/man1/xargs.1.html)
- [curl](https://www.man7.org/linux/man-pages/man1/curl.1.html)
# [PowerShell](#tab/powershell)
Open a new command prompt and enter PowerShell. Run the following commands, replacing `<YOUR_CONTAINER_APP_FQDN>` with the fully qualified domain name for your container app that you saved from the [Create and deploy the container app](#create-and-deploy-the-container-app) section.
```powershell
$url="<YOUR_CONTAINER_APP_FQDN>"
$Runspace = [runspacefactory]::CreateRunspacePool(1,10)
$Runspace.Open()
1..50 | % {
$ps = [powershell]::Create()
$ps.RunspacePool = $Runspace
[void]$ps.AddCommand("Invoke-WebRequest").AddParameter("UseBasicParsing",$true).AddParameter("Uri",$url)
[void]$ps.BeginInvoke()
}
```
These commands send 50 requests to your container app in asynchronous batches of 10 requests each.
| Command or argument | Description |
|---|---|
| `[runspacefactory]::CreateRunspacePool(1,10)` | Creates a `RunspacePool` that allows up to 10 runspaces to run concurrently. |
| `1..50 | % { }` | Runs the code enclosed in the curly braces 50 times. |
| `$ps = [powershell]::Create()` | Creates a new PowerShell instance. |
| `$ps.RunspacePool = $Runspace` | Tells the PowerShell instance to run in the `RunspacePool`. |
| `[void]$ps.AddCommand("Invoke-WebRequest")` | Sends a request to your container app. |
| `.AddParameter("UseBasicParsing", $true)` | Sends a request to your container app. |
| `.AddParameter("Uri", $url)` | Sends a request to your container app. |
| `[void]$ps.BeginInvoke()` | Tells the PowerShell instance to run asynchronously. |
For more information, see [Beginning Use of PowerShell Runspaces](https://devblogs.microsoft.com/scripting/beginning-use-of-powershell-runspaces-part-3/)
---
In the first shell, where you ran the `az containerapp logs show` command, the output now contains one or more log entries like the following.
```json
{
"TimeStamp":"2023-08-01 18:09:52 +0000 UTC",
"Type":"Normal",
"ContainerAppName":"my-container-app",
"RevisionName":"my-container-app--00001111",
"ReplicaName":"my-container-app--00001111-aaaaa22222-bbbb",
"Msg":"Replica 'my-container-app--00001111-aaaaa22222-bbbb' has been scheduled to run on a node.",
"Reason":"AssigningReplica",
"EventSource":"ContainerAppController",
"Count":0
}
```
## View scaling in Azure portal (optional)
1. Sign in to the [Azure portal](https://portal.azure.com).
1. In the **Search** bar at the top, enter **my-container-app**.
1. In the search results, under *Resources*, select **my-container-app**.
1. In the navigation bar at the left, expand **Application** and select **Scale and replicas**.
1. In the *Scale and Replicas* page, select **Replicas**.
1. Your container app now has more than one replica running.
:::image type="content" source="media/scale-app/azure-container-apps-scale-replicas.png" alt-text="Screenshot of container app replicas.":::
You might need to select **Refresh** to see the new replicas.
1. In the navigation bar at the left, expand **Monitoring** and select **Metrics**.
1. In the *Metrics* page, set **Metric** to **Requests**.
1. Select **Apply splitting**.
1. Expand the **Values** drop-down and check **Replica**.
1. Select the blue checkmark icon to finish editing the splitting.
1. The graph shows the requests received by your container app, split by replica.
:::image type="content" source="media/scale-app/azure-container-apps-scale-replicas-metrics-1.png" alt-text="Container app metrics graph, showing requests split by replica.":::
1. By default, the graph scale is set to last 24 hours, with a time granularity of 15 minutes. Select the scale and change it to the last 30 minutes, with a time granularity of one minute. Select the **Apply** button.
1. Select on the graph and drag to highlight the recent increase in requests received by your container app.
:::image type="content" source="media/scale-app/azure-container-apps-scale-replicas-metrics-2.png" alt-text="Screenshot of container app metrics graph, showing requests split by replica, with a scale of 30 minutes and time granularity of one minute.":::
The following screenshot shows a zoomed view of how the requests received by your container app are divided among replicas.
:::image type="content" source="media/scale-app/azure-container-apps-scale-replicas-metrics-3.png" alt-text="Screenshot of container app metrics graph, showing requests split by replica, in a zoomed view.":::
## CPU and memory scaling
You should prefer [HTTP scale rules](/azure/container-apps/scale-app#http) to CPU or memory scale rules when possible. CPU and memory scaling don't allow your container app to scale to zero.
After you add a CPU or memory scale rule, you can test it by [sending requests to your container app](#send-requests) and [viewing the scaling in Azure portal](#view-scaling-in-azure-portal-optional).
After you send requests to your scale app, it might take a minute before the scale rule is triggered and the new replicas are created.
### CPU scaling
CPU scaling allows your app to scale in or out depending on how much the CPU is being used.
For example, if you create a CPU scale rule with a utilization value of `50`, Azure Container Apps creates more replicas of your container app when the average CPU utilization for all replicas reaches 50%.
CPU scaling doesn't allow your container app to scale to zero. For more information about this trigger, see [KEDA CPU scale trigger](https://keda.sh/docs/scalers/cpu/).
Add a CPU scale rule to your container app by running the `az containerapp update` command.
> [!NOTE]
> When you use the Azure CLI to add a scale rule to a container app that already has a scale rule, the new scale rule replaces the old scale rule. To see how to add multiple scale rules, see [Multiple scale rules](#multiple-scale-rules).
Before running the following command, replace the `<PLACEHOLDERS>` with your values. For this tutorial, replace `<UTILIZATION>` with `1`. This causes your container app to scale when the average CPU utilization for all replicas reaches 1%. This value is for demonstration only. The number of replicas is limited to 10 by the `--max-replicas 10` you specified when running `az containerapp update`.
# [Bash](#tab/bash)
```azurecli
az containerapp update \
--name my-container-app \
--resource-group my-container-apps \
--min-replicas 1 \
--max-replicas 10 \
--scale-rule-name my-cpu-scale-rule \
--scale-rule-type cpu \
--scale-rule-metadata type=Utilization value=<UTILIZATION>
```
# [PowerShell](#tab/powershell)
```powershell
az containerapp update `
--name my-container-app `
--resource-group my-container-apps `
--min-replicas 1 `
--max-replicas 10 `
--scale-rule-name my-cpu-scale-rule `
--scale-rule-type cpu `
--scale-rule-metadata type=Utilization value=<UTILIZATION>
```
---
### Memory scaling
Memory scaling allows your app to scale in or out depending on how much memory is being used.
For example, if you create a memory scale rule with a utilization value of `50`, Azure Container Apps creates more replicas of your container app when the average memory utilization for all replicas reaches 50%.
Memory scaling doesn't allow your container app to scale to zero. For more information about this trigger, see [KEDA memory scale trigger](https://keda.sh/docs/scalers/memory/).
Add a memory scale rule to your container app by running the `az containerapp update` command.
> [!NOTE]
> When you use the Azure CLI to add a scale rule to a container app that already has a scale rule, the new scale rule replaces the old scale rule. To see how to add multiple scale rules, see [Multiple scale rules](#multiple-scale-rules).
Before running the following command, replace the `<PLACEHOLDERS>` with your values. For this tutorial, replace `<UTILIZATION>` with `1`. This causes your container app to scale when the average memory utilization for all replicas reaches 1%. This value is for demonstration only. The number of replicas is limited to 10 by the `--max-replicas 10` you specified when running `az containerapp update`.
# [Bash](#tab/bash)
```azurecli
az containerapp update \
--name my-container-app \
--resource-group my-container-apps \
--min-replicas 1 \
--max-replicas 10 \
--scale-rule-name my-memory-scale-rule \
--scale-rule-type memory \
--scale-rule-metadata type=Utilization value=<UTILIZATION>
```
# [PowerShell](#tab/powershell)
```powershell
az containerapp update `
--name my-container-app `
--resource-group my-container-apps `
--min-replicas 1 `
--max-replicas 10 `
--scale-rule-name my-memory-scale-rule `
--scale-rule-type memory `
--scale-rule-metadata type=Utilization value=<UTILIZATION>
```
---
## Multiple scale rules
To add multiple scale rules to your container app using the Azure CLI, you must use YAML.
1. Export your container app configuration to YAML with the `az containerapp show` command.
# [Bash](#tab/bash)
```azurecli
az containerapp show \
--name my-container-app \
--resource-group my-container-apps \
--output yaml > app.yaml
```
# [PowerShell](#tab/powershell)
```powershell
az containerapp show `
--name my-container-app `
--resource-group my-container-apps `
--output yaml > app.yaml
```
---
1. In the `properties` > `template` > `scale` > `rules` section of `app.yaml`, add the following properties. Replace the `<PLACEHOLDERS>` with your values.
```yaml
...
properties:
...
template:
...
scale:
...
rules:
- name: cpu-scaling-rule
custom:
type: cpu
metadata:
type: "Utilization"
value: "<CPU_UTILIZATION>"
- name: memory-scaling-rule
custom:
type: memory
metadata:
type: "Utilization"
value: "<MEMORY_UTILIZATION>"
...
```
1. Import your container app configuration from `app.yaml` with the `az containerapp update` command.
# [Bash](#tab/bash)
```azurecli
az containerapp update \
--name my-container-app \
--resource-group my-container-apps \
--yaml app.yaml
```
# [PowerShell](#tab/powershell)
```powershell
az containerapp update `
--name my-container-app `
--resource-group my-container-apps `
--yaml app.yaml
```
---
## Clean up resources
If you're not going to continue to use this application, run the following command to delete the resource group along with all the resources created in this tutorial.
>[!CAUTION]
> The following command deletes the specified resource group and all resources contained within it. If resources outside the scope of this tutorial exist in the specified resource group, they'll also be deleted.
```azurecli
az group delete --name my-container-apps
```
> [!TIP]
> Having issues? Let us know on GitHub by opening an issue in the [Azure Container Apps repo](https://github.com/microsoft/azure-container-apps).
## Next steps
> [!div class="nextstepaction"]
> [Set scaling rules in Azure Container Apps](scale-app.md)