Skip to content

Commit 7e71cfe

Browse files
committed
Added KEDA autoscaler config
1 parent c3eb459 commit 7e71cfe

5 files changed

+70
-1
lines changed

demo.http

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
GET https://techtalks-producer.happywater-d5036088.eastasia.azurecontainerapps.io/api/TechTalks/Generate?numberOfMessages=50000
1+
GET https://techtalks-producer.happywater-d5036088.eastasia.azurecontainerapps.io/api/TechTalks/Generate
2+
?numberOfMessages=50000
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Autoscale RabbitMQ consumers using KEDA
2+
3+
We need to enable [KEDA](keda.sh) for the Azure Container Apps. This will allow us to scale the Azure Container App for `techtalks consumer` based on the messages in the RabbitMQ queue. These messages are represented by the `queue length` property.
4+
5+
## Create secret for RabbitMQ connection
6+
7+
First we create a `secret` for the RabbitMQ cluster. The [script](/powershell/setup-tech-talks-container-app.ps1) runs the following command to create the secret:
8+
9+
```Powershell
10+
11+
az containerapp secret set `
12+
--name techtalks-consumer `
13+
--resource-group $resourceGroupName `
14+
--secrets "rabbitmq-host=amqp://user:[email protected]:5672/"
15+
16+
```
17+
18+
Note: The `rabbitmq-host` is the connection string for the RabbitMQ cluster. The connection string is in the format `amqp://<username>:<password>@<host>:<port>/`. The `username` and `password` are the credentials for the RabbitMQ cluster. The `host` and `port` are the host and port for the RabbitMQ cluster. Modify the connection string as per your RabbitMQ cluster.
19+
20+
## Create KEDA autoscaler configuration
21+
22+
Next we create a KEDA scaler for the Azure Container App. The [script](/powershell/setup-tech-talks-container-app.ps1) runs the following command to create the scaler:
23+
24+
```Powershell
25+
26+
az containerapp update `
27+
--name techtalks-consumer `
28+
--resource-group $resourceGroupName `
29+
--min-replicas 1 `
30+
--max-replicas 15 `
31+
--scale-rule-name "rabbitmq-keda-autoscale" `
32+
--scale-rule-type "rabbitmq" `
33+
--scale-rule-auth "host=rabbitmq-host" `
34+
--scale-rule-metadata "queueName=rabbitmq-consumer-techtalks" `
35+
"mode=QueueLength" `
36+
"value=50" `
37+
"protocol=amqp" `
38+
"hostFromEnv=rabbitmq-host"
39+
40+
```
41+
42+
The `--scale-rule-name` parameter is the name of the scaler. The `--scale-rule-type` parameter is the type of the scaler. The `--scale-rule-auth` parameter is the name of the secret that contains the RabbitMQ connection string. The `--scale-rule-metadata` parameter contains the metadata for the scaler. The `queueName` parameter is the name of the queue. The `mode` parameter is the mode of the scaler. The `value` parameter is the value for the scaler. The `protocol` parameter is the protocol to use for the scaler. The `hostFromEnv` parameter is the name of the environment variable that contains the RabbitMQ connection string.
43+
44+
Refer to the [KEDA documentation](https://keda.sh/docs/2.8/scalers/rabbitmq-queue/) for more information on the RabbitMQ scaler.
45+
46+
## Verify autoscaler configuration
47+
48+
Once the autoscaler configuration is created, we should see the number of consumers increasing in the rabbitmq management console. The following screenshot shows the number of consumers increasing from 1 to 15.
49+
50+
![Autoscaled RabbitMQ consumers](/images/keda-consumers.png)
51+
52+
We can see that there are 15 consumers for the `rabbitmq-consumer-techtalks` queue. This is the same number of consumers as the `max-replicas` value. There are also 750 messages in untracked state. This is the same number of messages as the `prefetech-count` parameter from the [RabbitMQ-dapr](/config/Dapr-components/rabbitmq-dapr.yaml) configuration.
53+
54+
This shows that the autoscaler is working as expected.
55+
56+
## Verify autoscaler scaling back down
57+
58+
Let the 15 consumers run and process all the messages from the queue. Once the queue is empty, the consumers will scale back down to 1. This can be verified in the RabbitMQ management console. The following screenshot shows the number of consumers scaling back down to 1. The screenshot also shows that there are no messages in the queue.
59+
60+
![Autoscaled RabbitMQ consumers](/images/keda-consumers-scaled-down.png)
61+
62+
## Verify KEDA autoscaler using Azure Portal
63+
64+
We can also verify that the KEDA autoscaler is configured properly for the Azure Container App using the Azure Portal. The following screenshot shows the KEDA autoscaler configuration for the Azure Container App.
65+
66+
![KEDA autoscaler configuration](/images/keda-scale-configuration-portal.png)
67+
68+
To access this information, navigate to the Azure Container App in the Azure Portal. Click on the `Scale` tab. The `Scale Rule` section shows the KEDA autoscaler configuration. Clicking on the `Scale Rule` section will show the details of the KEDA autoscaler configuration like the secret reference and the metadata.

images/keda-consumers-scaled-down.png

292 KB
Loading

images/keda-consumers.png

312 KB
Loading
243 KB
Loading

0 commit comments

Comments
 (0)