Pular para o conteúdo principal

Azure App Service with several instances

A worked installation of the Server on Azure App Service with two or more instances, from an empty resource group to a console you can sign in to. It needs Server 1.32.0 or later, the first version whose instances can reach each other. Read Azure App Service first: this page follows its order, points back to it where a step is the same, and says only what several instances add or change.

What was run. This guide was written from a first deployment of Server 1.32.0 with two instances on a B1 plan, an Azure Managed Redis and a database in an elastic pool of an existing Azure SQL server. That deployment started, both instances registered and connected to Redis, and the console answered on a custom domain. It had no Workstation connected yet, so no call had crossed from one instance to the other there, no signature had been made, and no load was measured. It had also run for less than a day, so the renewal of the token the app signs in to Redis with, which lasts 24 hours, had not been seen. Treat your own first deployment as a test, and read What the first deployment met before you start.

What you end up with​

ResourceExample nameNotes
Resource grouptb-prodEverything below, unless you place the database elsewhere
App Service plantb-prod-planLinux, two or more workers, never Free
App Servicetb-prodThe Server, one container per worker
Azure Managed Redistb-prod-redisHow the instances reach each other. It holds no state
Azure SQL databaseTrustBridgeOn a server of its own, or on one you already run
Managed certificateyour custom domainIssued and renewed by App Service
Application Insights and its workspacetb-prod-insights, tb-prod-logsOptional, and the only retained copy of the log

The app signs in to the database, to the registry and to Redis with its own managed identity. There is no database password, no registry password and no Redis key anywhere in this deployment.

Before you start​

  • The Azure CLI, signed in with rights to create resources in the group, and its redisenterprise extension, which is how the CLI manages Azure Managed Redis: az extension add --name redisenterprise.
  • The image, trustbridge-server:1.32.0 or later, in a registry the app can pull from (step 3 of the single-instance page).
  • The PKI SDK licence, the Signer's address and API key (or the three REST PKI Core values), and the first Administrator's e-mail.
  • A Key Ring Protector, minted as the installation page describes. Every instance uses the same one.
  • For a database on an existing Azure SQL server: to be its Entra administrator, or a member of the group that is, and a firewall rule that lets your own machine through, because you create the database user from it.

Commands below use these placeholders: <rg>, <plan>, <app>, <cache>, <region>, <sqlserver>, <database> and <registry>, and a few more that are named where they appear. Pass --subscription on every command when the subscription you are deploying into is not your CLI's default; it is safer than switching the default under your other terminals.

1. The plan, with its instance count​

az appservice plan create -g <rg> -n <plan> -l <region> --is-linux --sku P1V3 --number-of-workers 2

--number-of-workers is the instance count. A new deployment can be created with it, as here, because the app does not start until step 8, by which time it names its Redis. An existing single-instance deployment is different: add the Redis and the two settings of step 6 first, and raise the count last.

Size each instance as the single-instance page says. Basic plans run the Server and were not measured; they have no zone redundancy, which needs a Premium plan. Some of the Server's bounds are each instance's, so a deployment of two has twice each: the contract throttle, the number of signing runs at once and the number of release downloads at once. The signature service has to accept the signing bound, Signing:SignerConcurrency, times the instance count.

2. The database​

On a server of its own, follow step 2 of the single-instance page.

On an Azure SQL server you already run, which may be in another resource group or another subscription of the same tenant, create only the database, optionally in one of its elastic pools:

az sql db create -g <sql-rg> -s <sqlserver> -n <database> \
--elastic-pool <pool> --backup-storage-redundancy Local
  • Always say the backup redundancy. Left out, the CLI stops to ask whether geo-redundant backups are meant, and a script has nobody to answer. Geo copies the backups to the region's pair, which may be in another country; Local and Zone keep them in the database's region.
  • The server needs public network access and "Allow Azure services", as on the single-instance page, or a private endpoint with VNet integration. On a shared server, check them rather than change them: they are its owner's.
  • A pool's capacity is shared. A migration, or the upload of a Workstation Release of about 100 MB, competes with every other database in the pool.

Use --service-objective in place of --elastic-pool for a database outside a pool.

3 and 4. The image, the app and its identity​

Exactly as steps 3 and 4 of the single-instance page, with the 1.32.0 tag.

A registry in another subscription than the app's works, as long as both are of one tenant. The role assignment is scoped to the registry's id wherever it lives. What changes is the commands: the CLI finds a registry by name in one subscription only, so each az acr command takes --subscription with the registry's.

Do not restart the app yet. It cannot start usefully until it has its settings.

5. The instances' Redis​

az redisenterprise create -g <rg> --cluster-name <cache> -l <region> --sku Balanced_B1 \
--high-availability Enabled --access-keys-authentication Disabled \
--clustering-policy OSSCluster --public-network-access Enabled

az redisenterprise database access-policy-assignment create -g <rg> --cluster-name <cache> \
--database-name default --access-policy-assignment-name <app-without-hyphens> \
--access-policy-name default \
--object-id $(az webapp identity show -g <rg> -n <app> --query principalId -o tsv)

az redisenterprise show -g <rg> --cluster-name <cache> --query hostName -o tsv
az redisenterprise database show -g <rg> --cluster-name <cache> --query port -o tsv # 10000
  • Read the host with show. The answer of create did not carry it on the deployment this guide was written from; show did.
  • High availability is the point, not an extra. A Redis outage costs every call that crosses from one instance to another until it is back. It costs nothing else: no security bound depends on Redis, and a call that stays on one instance is unaffected.
  • The access keys stay disabled. Who can connect is whoever holds an access policy on the cache, which is the app's identity and nobody else. The connection string carries no password.
  • The cache is reached on its public endpoint, as Azure SQL is, unless you use VNet integration and a private endpoint. What the instances send each other is sealed under the deployment's key ring, so reading the cache's traffic shows nothing.
  • OSSCluster is chosen at creation and does not change afterwards.
  • Balanced_B1 is a starting point, not a measured figure. Balanced_B0 is the smallest size and is what the deployment this guide was written from runs. The instances send each other small messages, one for each call that crosses and one acknowledgement.

6. The database user and the settings​

Create the app's identity as a database user exactly as step 5 of the single-instance page says. On a shared server it is a user of your database alone.

Then the settings. They are those of step 6, plus two:

az webapp config appsettings set -g <rg> -n <app> --settings \
Redis__ConnectionString="<cache>.<region>.redis.azure.net:10000,ssl=true" \
Redis__UseManagedIdentity=true
SettingWhy
Redis__ConnectionStringThe host and port step 5 read back, with ssl=true. Naming a Redis is what makes several instances one deployment. A plan above one worker with no Redis named is the mistake to avoid: nothing refuses it, each instance writes one Critical line and shows a banner on every admin page once it has seen another beside it for longer than a redeploy takes (ten minutes by default), and until it is fixed a session fails against a machine connected to the other instance.
Redis__UseManagedIdentity=trueThe Server signs in to the cache as the app's identity.

A Redis that cannot be reached never refuses the start: the Server says so once at Critical, shows a banner on every admin page, keeps serving the calls that stay on one instance, and keeps trying.

7. Pin the platform​

az webapp log config -g <rg> -n <app> --docker-container-logging filesystem
az webapp update -g <rg> -n <app> --client-affinity-enabled true --https-only true
az webapp config set -g <rg> -n <app> --always-on true \
--generic-configurations '{"healthCheckPath": "/healthz"}'

Two of these mean something else once there are several instances.

  • Client affinity is a requirement, not a convenience. A Workstation's negotiate request and the connection that follows it must reach the same instance, and so must an Administrator's console. With affinity off, a machine's connect is answered 404 by the instance that did not negotiate it, and the machine never connects. Never turn it off on a deployment of several.
  • Health Check goes on /healthz, where one instance keeps it on /. With somewhere to reroute to, App Service takes an instance that cannot reach the database out of rotation and puts it back when it answers again. /healthz does not ask Redis: a Redis that is down is down for every instance, and taking instances out of rotation cannot help. With the database down every instance is unhealthy; App Service excludes none of them, and still replaces an instance that has been unhealthy for an hour.

8. Start it and check both instances​

az webapp restart -g <rg> -n <app>
curl -s https://<app>.azurewebsites.net/healthz # Healthy
az webapp list-instances -g <rg> -n <app> -o table # one row per worker
az webapp log download -g <rg> -n <app> --log-file logs.zip

The first start of an empty database builds the whole schema before anything answers; allow a few minutes. The downloaded archive has one container log per instance. Read each one, since /healthz is answered by whichever instance the request reaches. Besides the lines the single-instance page lists, each instance says:

  • This Server instance registered as <id> (build 1.32.0).
  • Connected to the Redis Redis:ConnectionString names.
  • This Server instance (<id>) listens for the other instances' requests.

In the console, the Server log screen then names the instance each line came from.

Then sign in, set a real password, enroll the second factor and remove the two Admin__Bootstrap__* settings, as on the single-instance page.

9. A custom domain and its certificate​

Workstations and Administrators should use a name of yours rather than the Azure one.

  1. In your DNS zone, create a CNAME from the name to <app>.azurewebsites.net, and a TXT record asuid.<name> holding the app's verification id (az webapp show -g <rg> -n <app> --query customDomainVerificationId -o tsv).
  2. Add the hostname, issue a managed certificate and bind it:
az webapp config hostname add -g <rg> --webapp-name <app> --hostname <name>
az webapp config ssl create -g <rg> -n <app> --hostname <name>
az webapp config ssl bind -g <rg> -n <app> --ssl-type SNI \
--certificate-thumbprint <the thumbprint ssl create answered>

The certificate renews itself while the CNAME stays in place. A binding takes a few minutes to settle: for about four minutes after it, roughly one request in twelve to the custom name failed its TLS check, the certificate not matching the name, and then none did. Wait before you conclude anything from one failed request, and give Workstations the custom name as their Server address once it has settled.

10. Certificate Sign-In​

Optional. Two settings turn it on, and the app restarts:

az webapp config appsettings set -g <rg> -n <app> --settings \
Admin__CertificateSignIn__Enabled=true \
Admin__CertificateSignIn__WebPkiLicense="<the Web PKI licence, one-line format>"
  • The licence has to name the address people type. One issued for your custom domain does not cover <app>.azurewebsites.net, and the certificate page says so as a licence problem when it is opened there.
  • Nobody signs in with a certificate until somebody has set their Bound CPF, and nobody sets their own: a second Administrator does it on the accounts screen.
  • Leave Admin__CertificateSignIn__Required off until an Administrator has signed in with a certificate. See Admin:CertificateSignIn:Enabled and Certificate Sign-In.

A challenge minted on one instance is honoured by whichever instance receives the answer, so nothing here depends on the instance count.

11. Application Insights​

Optional, and worth having: without it the log is kept for a few days on the App Service filesystem and for as long as each container lives.

az extension add --name application-insights # once per machine
az provider register -n Microsoft.OperationalInsights --wait # once per subscription
az provider register -n Microsoft.Insights --wait

az monitor log-analytics workspace create -g <rg> -n <workspace> -l <region> --retention-time 30
az monitor app-insights component create --app <insights> -g <rg> -l <region> \
--workspace <the workspace's resource id> --application-type web
for table in AppTraces AppExceptions; do
az monitor log-analytics workspace table update -g <rg> --workspace-name <workspace> \
-n $table --retention-time 30 --total-retention-time 30
done
az webapp config appsettings set -g <rg> -n <app> --settings \
Logging__ApplicationInsights__ConnectionString="<the component's connection string>"
  • Thirty days is the shortest a workspace keeps. Application Insights tables default to ninety whatever the workspace says, so the two the Server's lines land in are set on their own.
  • Decide the log level before you pay for the volume. At the image's default every database command is a log line. Logging__LogLevel__Default=Warning with Logging__LogLevel__TrustBridge=Information keeps the Server's own lines and drops the rest. Every destination sees the same lines, the container's console output included (Logging:ApplicationInsights:ConnectionString). With those levels Application started is no longer logged, being the framework's line and not the Server's. What then proves a start is the Server's own lines: the protector, the licence, the schema, the vault, and the three instance lines of step 8.
  • Each line carries the container it came from, so the two instances can be told apart in a query.

Operating it​

  • The instance count follows the Redis. Once a Redis is named, raise or lower the plan's worker count as load asks, by hand or by an autoscale rule. An instance that starts registers and serves; one that stops ends what it held as it goes, and its machines reconnect to the others.
  • What a restart of one instance costs. Its machines reconnect, to any instance. Sessions holding a Vault Certificate's file password end as Stranded if that instance held them, and only those; the others carry on, signing through any instance.
  • Upgrades are two cases. A version with no migration replaces the instances while old and new serve side by side. A version with a migration has the new build stand the older instances down before it migrates: each exits, App Service starts it again on the new image, and for about a minute nothing serves. Neither needs a step from you.
  • A slot or a test instance never shares this database. From 1.32.0 a newer build started against a production database stands every production instance down. Give a slot a database of its own, or set Database__MigrateOnStart=false on it, which makes it refuse its own start instead.
  • An instance that cannot renew its registration for fifteen seconds is taken for dead by the others, which end what it held. If it was only held up, it finds its registration gone, says so at Critical and registers again. See the next section for when that was seen.

What the first deployment met​

Each of these happened on the deployment this guide was written from, with Server 1.32.0.

  • Two instances starting together on an empty database both tried to create the first Administrator. One created the roles and the account. The other logged, at Error, "The admin account bootstrap did not complete" with DuplicateRoleName. The database ended complete, and the account signed in. If you see that line on a first start, look for "Created the first admin account" in the other instance's log before you worry.
  • As App Service replaced the containers on a settings change, one new instance's database connection stalled for about 35 seconds. It renewed nothing for fifteen, the other instance took it for dead, and it logged one Critical line and registered again. With no Workstation connected nothing was lost; on a deployment in use its machines would have reconnected.
  • az sql db create and az redisenterprise create each needed the care steps 2 and 5 describe.
View as Markdown