Skip to content

Commit 9eca73d

Browse files
Merge pull request #247127 from v-jaswel/aca/v-jaswel_20230802
[ACA] Create scaling tutorial
2 parents aa1265d + cb7ff8a commit 9eca73d

6 files changed

+386
-0
lines changed

articles/container-apps/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
items:
5959
- name: Deploy an app with containerapp up
6060
href: containerapp-up.md
61+
- name: Scale a container app
62+
href: tutorial-scaling.md
6163
- name: Create and deploy a container app using the CLI
6264
href: tutorial-deploy-first-app-cli.md
6365
- name: Connect to apps & services
39.2 KB
Loading
48.1 KB
Loading
64.8 KB
Loading
18.2 KB
Loading
Lines changed: 384 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,384 @@
1+
---
2+
title: 'Tutorial: Scale an Azure Container Apps application'
3+
description: Scale an Azure Container Apps application using the Azure CLI.
4+
services: container-apps
5+
author: v-jaswel
6+
ms.service: container-apps
7+
ms.topic: tutorial
8+
ms.date: 08/02/2023
9+
ms.author: v-wellsjason
10+
ms.custom: devx-track-azurecli
11+
ms.devlang: azurecli
12+
---
13+
14+
# Tutorial: Scale a container app
15+
16+
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.
17+
18+
In this tutorial, you add an HTTP scale rule to your container app and observe how your application scales.
19+
20+
## Prerequisites
21+
22+
| Requirement | Instructions |
23+
|--|--|
24+
| Azure account | If you don't have an Azure account, you can [create one for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). <br><br>You need the *Contributor* permission on the Azure subscription to proceed. Refer to [Assign Azure roles using the Azure portal](../role-based-access-control/role-assignments-portal.md?tabs=current) for details. |
25+
| GitHub Account | Get one for [free](https://github.com/join). |
26+
| Azure CLI | Install the [Azure CLI](/cli/azure/install-azure-cli). |
27+
28+
## Setup
29+
30+
Run the following command and follow the prompts to sign in to Azure from the CLI and complete the authentication process.
31+
32+
# [Bash](#tab/bash)
33+
34+
```azurecli
35+
az login
36+
```
37+
38+
# [Azure PowerShell](#tab/azure-powershell)
39+
40+
```azurepowershell
41+
az login
42+
```
43+
44+
---
45+
46+
Ensure you're running the latest version of the CLI via the `az upgrade` command.
47+
48+
# [Bash](#tab/bash)
49+
50+
```azurecli
51+
az upgrade
52+
```
53+
54+
# [Azure PowerShell](#tab/azure-powershell)
55+
56+
```azurepowershell
57+
az upgrade
58+
```
59+
60+
---
61+
62+
Install or update the Azure Container Apps extension for the CLI.
63+
64+
# [Bash](#tab/bash)
65+
66+
```azurecli
67+
az extension add --name containerapp --upgrade
68+
```
69+
70+
# [Azure PowerShell](#tab/azure-powershell)
71+
72+
73+
```azurepowershell
74+
az extension add --name containerapp --upgrade
75+
```
76+
77+
---
78+
79+
Register the `Microsoft.App` and `Microsoft.OperationalInsights` namespaces if you haven't already registered them in your Azure subscription.
80+
81+
# [Bash](#tab/bash)
82+
83+
```azurecli
84+
az provider register --namespace Microsoft.App
85+
```
86+
87+
```azurecli
88+
az provider register --namespace Microsoft.OperationalInsights
89+
```
90+
91+
# [Azure PowerShell](#tab/azure-powershell)
92+
93+
```azurepowershell
94+
az provider register --namespace Microsoft.App
95+
```
96+
97+
```azurepowershell
98+
az provider register --namespace Microsoft.OperationalInsights
99+
```
100+
101+
---
102+
103+
## Create and deploy the container app
104+
105+
Create and deploy your container app with the `containerapp up` command. This command creates a:
106+
107+
- Resource group
108+
- Container Apps environment
109+
- Log Analytics workspace
110+
111+
If any of these resources already exist, the command uses the existing resources rather than creating new ones.
112+
113+
Lastly, the command creates and deploys the container app using a public container image.
114+
115+
# [Bash](#tab/bash)
116+
117+
```azurecli
118+
az containerapp up \
119+
--name my-container-app \
120+
--resource-group my-container-apps \
121+
--location centralus \
122+
--environment 'my-container-apps' \
123+
--image mcr.microsoft.com/azuredocs/containerapps-helloworld:latest \
124+
--target-port 80 \
125+
--ingress external \
126+
--query properties.configuration.ingress.fqdn \
127+
```
128+
129+
# [Azure PowerShell](#tab/azure-powershell)
130+
131+
```powershell
132+
az containerapp up `
133+
--name my-container-app `
134+
--resource-group my-container-apps `
135+
--location centralus `
136+
--environment my-container-apps `
137+
--image mcr.microsoft.com/azuredocs/containerapps-helloworld:latest `
138+
--target-port 80 `
139+
--ingress external `
140+
--query properties.configuration.ingress.fqdn `
141+
```
142+
143+
---
144+
145+
> [!NOTE]
146+
> Make sure the value for the `--image` parameter is in lower case.
147+
148+
By setting `--ingress` to `external`, you make the container app available to public requests.
149+
150+
The `up` command returns the fully qualified domain name (FQDN) for the container app. Copy this FQDN to a text file. You'll use it in the [Send requests](#send-requests) section. Your FQDN looks like the following example:
151+
152+
```text
153+
https://my-container-app.icydune-96848328.centralus.azurecontainerapps.io
154+
```
155+
156+
## Add scale rule
157+
158+
Add an HTTP scale rule to your container app by running the `az containerapp update` command.
159+
160+
# [Bash](#tab/bash)
161+
162+
```azurecli
163+
az containerapp update \
164+
--name my-container-app \
165+
--resource-group my-container-apps \
166+
--scale-rule-name my-http-scale-rule \
167+
--scale-rule-http-concurrency 1
168+
```
169+
170+
# [Azure PowerShell](#tab/azure-powershell)
171+
172+
```powershell
173+
az containerapp update `
174+
--name my-container-app `
175+
--resource-group my-container-apps `
176+
--scale-rule-name my-http-scale-rule `
177+
--scale-rule-http-concurrency 1
178+
```
179+
180+
---
181+
182+
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.
183+
184+
The `update` command returns the new configuration as a JSON response to verify your request was a success.
185+
186+
## Start log output
187+
188+
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.
189+
190+
# [Bash](#tab/bash)
191+
192+
```azurecli
193+
az containerapp logs show \
194+
--name my-container-app \
195+
--resource-group my-container-apps \
196+
--type=system \
197+
--follow=true
198+
```
199+
200+
# [Azure PowerShell](#tab/azure-powershell)
201+
202+
```powershell
203+
az containerapp logs show `
204+
--name my-container-app `
205+
--resource-group my-container-apps `
206+
--type=system `
207+
--follow=true
208+
```
209+
210+
---
211+
212+
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:
213+
214+
```json
215+
{
216+
"TimeStamp":"2023-08-01T16:49:03.02752",
217+
"Log":"Connecting to the container 'my-container-app'..."
218+
}
219+
{
220+
"TimeStamp":"2023-08-01T16:49:03.04437",
221+
"Log":"Successfully Connected to container:
222+
'my-container-app' [Revision: 'my-container-app--9uj51l6',
223+
Replica: 'my-container-app--9uj51l6-5f96557ffb-5khg9']"
224+
}
225+
{
226+
"TimeStamp":"2023-08-01T16:47:31.9480811+00:00",
227+
"Log":"Microsoft.Hosting.Lifetime[14]"
228+
}
229+
{
230+
"TimeStamp":"2023-08-01T16:47:31.9481264+00:00",
231+
"Log":"Now listening on: http://[::]:3500"
232+
}
233+
{
234+
"TimeStamp":"2023-08-01T16:47:31.9490917+00:00",
235+
"Log":"Microsoft.Hosting.Lifetime[0]"
236+
}
237+
{
238+
"TimeStamp":"2023-08-01T16:47:31.9491036+00:00",
239+
"Log":"Application started. Press Ctrl+C to shut down."
240+
}
241+
{
242+
"TimeStamp":"2023-08-01T16:47:31.949723+00:00",
243+
"Log":"Microsoft.Hosting.Lifetime[0]"
244+
}
245+
{
246+
"TimeStamp":"2023-08-01T16:47:31.9497292+00:00",
247+
"Log":"Hosting environment: Production"
248+
}
249+
{
250+
"TimeStamp":"2023-08-01T16:47:31.9497325+00:00",
251+
"Log":"Microsoft.Hosting.Lifetime[0]"
252+
}
253+
{
254+
"TimeStamp":"2023-08-01T16:47:31.9497367+00:00",
255+
"Log":"Content root path: /app/"
256+
}
257+
```
258+
259+
For more information, see [az containerapp logs](/cli/azure/containerapp/logs).
260+
261+
## Send requests
262+
263+
# [Bash](#tab/bash)
264+
265+
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.
266+
267+
```bash
268+
seq 1 50 | xargs -Iname -P10 curl "<YOUR_CONTAINER_APP_FQDN>"
269+
```
270+
271+
These commands send 50 requests to your container app in concurrent batches of 10 requests each.
272+
273+
| Command or argument | Description |
274+
|---|---|
275+
| `seq 1 50` | Generates a sequence of numbers from 1 to 50. |
276+
| `|` | The pipe operator sends the sequence to the `xargs` command. |
277+
| `xargs` | Runs `curl` with the specified URL |
278+
| `-Iname` | Acts as a placeholder for the output of `seq`. This argument prevents the return value from being sent to the `curl` command. |
279+
| `curl` | Calls the given URL. |
280+
| `-P10` | Instructs `xargs` to run up to 10 processes at a time. |
281+
282+
For more information, see the documentation for:
283+
- [seq](https://www.man7.org/linux/man-pages/man1/seq.1.html)
284+
- [xargs](https://www.man7.org/linux/man-pages/man1/xargs.1.html)
285+
- [curl](https://www.man7.org/linux/man-pages/man1/curl.1.html)
286+
287+
# [Azure PowerShell](#tab/azure-powershell)
288+
289+
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.
290+
291+
```powershell
292+
$url="<YOUR_CONTAINER_APP_FQDN>"
293+
$Runspace = [runspacefactory]::CreateRunspacePool(1,10)
294+
$Runspace.Open()
295+
1..50 | % {
296+
$ps = [powershell]::Create()
297+
$ps.RunspacePool = $Runspace
298+
[void]$ps.AddCommand("Invoke-WebRequest").AddParameter("UseBasicParsing",$true).AddParameter("Uri",$url)
299+
[void]$ps.BeginInvoke()
300+
}
301+
```
302+
303+
These commands send 50 requests to your container app in asynchronous batches of 10 requests each.
304+
305+
| Command or argument | Description |
306+
|---|---|
307+
| `[runspacefactory]::CreateRunspacePool(1,10)` | Creates a `RunspacePool` that allows up to 10 runspaces to run concurrently. |
308+
| `1..50 | % { }` | Runs the code enclosed in the curly braces 50 times. |
309+
| `$ps = [powershell]::Create()` | Creates a new PowerShell instance. |
310+
| `$ps.RunspacePool = $Runspace` | Tells the PowerShell instance to run in the `RunspacePool`. |
311+
| `[void]$ps.AddCommand("Invoke-WebRequest")` | Sends a request to your container app. |
312+
| `.AddParameter("UseBasicParsing", $true)` | Sends a request to your container app. |
313+
| `.AddParameter("Uri", $url)` | Sends a request to your container app. |
314+
| `[void]$ps.BeginInvoke()` | Tells the PowerShell instance to run asynchronously. |
315+
316+
For more information, see [Beginning Use of PowerShell Runspaces](https://devblogs.microsoft.com/scripting/beginning-use-of-powershell-runspaces-part-3/)
317+
318+
---
319+
320+
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.
321+
322+
```json
323+
{
324+
"TimeStamp":"2023-08-01 18:09:52 +0000 UTC",
325+
"Type":"Normal",
326+
"ContainerAppName":"my-container-app",
327+
"RevisionName":"my-container-app--9uj51l6",
328+
"ReplicaName":"my-container-app--9uj51l6-5f96557ffb-f795d",
329+
"Msg":"Replica 'my-container-app--9uj51l6-5f96557ffb-f795d' has been scheduled to run on a node.",
330+
"Reason":"AssigningReplica",
331+
"EventSource":"ContainerAppController",
332+
"Count":0
333+
}
334+
```
335+
336+
## View scaling in Azure portal (optional)
337+
338+
1. Sign in to the [Azure portal](https://portal.azure.com).
339+
1. In the **Search** bar at the top, enter **my-container-app**.
340+
1. In the search results, under *Resources*, select **my-container-app**.
341+
1. In the navigation bar at the left, expand **Application** and select **Scale and replicas**.
342+
1. In the *Scale and Replicas* page, select **Replicas**.
343+
1. Your container app now has more than one replica running.
344+
345+
:::image type="content" source="media/scale-app/azure-container-apps-scale-replicas.png" alt-text="Screenshot of container app replicas.":::
346+
347+
You may need to select **Refresh** to see the new replicas.
348+
349+
1. In the navigation bar at the left, expand **Monitoring** and select **Metrics**.
350+
1. In the *Metrics* page, set **Metric** to **Requests**.
351+
1. Select **Apply splitting**.
352+
1. Expand the **Values** drop-down and check **Replica**.
353+
1. Select the blue checkmark icon to finish editing the splitting.
354+
1. The graph shows the requests received by your container app, split by replica.
355+
356+
:::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.":::
357+
358+
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.
359+
1. Select on the graph and drag to highlight the recent increase in requests received by your container app.
360+
361+
:::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.":::
362+
363+
The following screenshot shows a zoomed view of how the requests received by your container app are divided among replicas.
364+
365+
:::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.":::
366+
367+
## Clean up resources
368+
369+
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.
370+
371+
>[!CAUTION]
372+
> 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 will also be deleted.
373+
374+
```azurecli
375+
az group delete --name my-container-apps
376+
```
377+
378+
> [!TIP]
379+
> Having issues? Let us know on GitHub by opening an issue in the [Azure Container Apps repo](https://github.com/microsoft/azure-container-apps).
380+
381+
## Next steps
382+
383+
> [!div class="nextstepaction"]
384+
> [Set scaling rules in Azure Container Apps](scale-app.md)

0 commit comments

Comments
 (0)