Skip to content

Instantly share code, notes, and snippets.

@gbaeke
Last active August 22, 2022 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbaeke/1e9ad7a62cb6a3347e081fad6dcca4d2 to your computer and use it in GitHub Desktop.
Save gbaeke/1e9ad7a62cb6a3347e081fad6dcca4d2 to your computer and use it in GitHub Desktop.
Azure Container Apps - Bicep and scaling

Azure Container Apps: Bicep and scaling

Azure Service Bus

Create namespace and topic:

az group create --name rg-aca --location northeurope

namespaceName=MyNameSpace$RANDOM
az servicebus namespace create --resource-group rg-aca --name $namespaceName --location northeurope
az servicebus topic create --resource-group rg-aca --namespace-name $namespaceName --name mytopic

Get connection string:

az servicebus namespace authorization-rule keys list --resource-group rg-aca --namespace-name $namespaceName --name RootManageSharedAccessKey --query primaryConnectionString --output tsv

You will need the connection string when you deploy with Bicep.

Deploy with Bicep

Save the bicep file below as main.bicep:

param image string = 'ghcr.io/gbaeke/super:1.0.5'
@secure()
param sbconnstr string

resource la 'Microsoft.OperationalInsights/workspaces@2021-06-01' = {
  name: 'la-aca' 
  location: resourceGroup().location
  properties: {
    retentionInDays: 30
    sku: {
      name: 'PerGB2018'
    }
  }
}


resource env 'Microsoft.Web/kubeEnvironments@2021-02-01' = {
  name: 'myenv'
  location: resourceGroup().location
  properties: {
    // not recognized but type is required
    type: 'managed'
    internalLoadBalancerEnabled:false
    appLogsConfiguration: {
      destination: 'log-analytics'
      logAnalyticsConfiguration: {
        customerId: la.properties.customerId
        sharedKey: la.listKeys().primarySharedKey
      }
    }
    }
  }

  resource frontContainerApp 'Microsoft.Web/containerApps@2021-03-01' = {
    name: 'front'
    location: resourceGroup().location
    kind: 'containerapp'
    properties: {
      kubeEnvironmentId: env.id
      configuration: {
        ingress: {
          external: true
          targetPort: 8080
        }
      }
      template: {
        containers: [
          {
            image: image
            name: 'super'
            env: [
              {
                name: 'WELCOME'
                value: 'Welcome to Container Apps'
              }
            ]
          }
        ]
        scale: {
          minReplicas: 0
          maxReplicas: 10
          rules: [
            {
            name: 'http-rule'
            http: {
              metadata: {
                concurrentRequests: '100'
              }
            }
            }
          ]
        }
      }
    }
  }

  resource pubSubContainerApp 'Microsoft.Web/containerApps@2021-03-01' = {
    name: 'pubsub'
    location: resourceGroup().location
    kind: 'containerapp'
    properties: {
      kubeEnvironmentId: env.id
      configuration: {
        ingress: {
          external: true
          targetPort: 8080
        }
        secrets: [
          {
            name: 'sbconnstr'
            value: sbconnstr
          }
        ]
      }
      template: {
        containers: [
          {
            image: image
            name: 'super'
            env: [
              {
                name: 'WELCOME'
                value: 'Welcome to Container Apps'
              }
            ]
          }
        ]
        scale: {
          minReplicas: 0
          maxReplicas: 10
          rules: [
            {
              name: 'topic-based-autoscaling'
              custom: {
                type: 'azure-servicebus'
                metadata: {
                  topicName: 'mytopic'
                  subscriptionName: 'pubsub'
                  messageCount: '2'
                }
                auth: [
                  {
                    secretRef: 'sbconnstr'
                    triggerParameter: 'connection'
                  }
                ]
              }

            }
          ]
        }
        dapr: {
          enabled: true
          appPort: 8080
          appId: 'pubsub'
          components: [
            {
              name: 'pubsub'
              type: 'pubsub.azure.servicebus'
              version: 'v1'
              metadata: [
                {
                  name: 'connectionString'
                  secretRef: 'sbconnstr'
                }
              ]
            }
          ]
        }
      }
    }
  }

To deploy the above to a resource group rg-aca, run:

az group create -n rg-aca -l northeurope

az deployment group create -n aca-deploy \
  -g rg-aca \
  --template-file ./main.bicep \
  -p sbconnstr='SB_CONNECTION_STRING'

Publish messages

Create a components folder and in that folder, create a file sd.yaml with the following content:

apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
  name: sb
spec:
  type: pubsub.azure.servicebus
  version: v1
  metadata:
  - name: connectionString
    value: 'SERVICEBUS_CONNECTION_STRING'

On your computer with Dapr installed, use dapr run with the component in the components folder. Run the command from the folder that contains the components folder:

dapr run --dapr-http-port 3500 --app-id pubsubapp -d ./components

With the app running, run the following command to publish messages. The data you send with --data will be wrapped in a CloudEvents envelope.

while true; do dapr publish --publish-app-id pubsubapp --pubsub sb --topic mytopic --data '{"name":"geertje"}'; sleep 0.1; done

This publishes a message to the topic every tenth of a second. Adjust the timing with sleep as you see fit.

Revisions

To see the revisions of pubsub: az containerapp revision list -n pubsub -g rg-aca -o table

To get information about a revision: az containerapp revision show --app pubsub -n YOUR_REVISION -g rg-aca

Check the replicas: az containerapp revision show --app pubsub -n YOUR_REVISION -g rg-aca | jq .replicas

⚠️ Above command requires jq to be installed.

The number of revisions should be 10 if you are sending messages. If 0, start publishing again and start a watch on the replicas:

watch 'az containerapp revision show --app pubsub -n YOUR_REVISION -g rg-aca | jq .replicas'

When you stop publishing, the container app should be scaled to 0.

@rishi1kumar
Copy link

Hello,
Actually I was trying the Container App deployment, and i was getting the error during container App environment creation, Please find the below screenshot for your reference.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment