I have a app service in Azure that has been scaled out to 5 instances. I can list the instances via the following az cli command
az webapp list-instances
But how can i tell which instance has frozen or is not responding and restart it ?
Here is some powershell to call the REST API to fetch a list of instances, and call a health probe on each one:
## 1. define environment
$subscriptionId = "redacted";
$resourceGroup = "redacted";
$webAppName = "redacted";
$staging = $false
# 2. get a list of instances:
$token = Get-AzAccessToken;
$RequestHeader = @{
Authorization = "Bearer $($token.Token)";
'Content-Type' = "application/json";
}
if ($staging) {
$instancesUrl = "https://management.azure.com/subscriptions/$($subscriptionId)/resourcegroups/$($resourceGroup)/providers/Microsoft.Web/sites/$($webAppName)/slots/staging/instances?api-version=2022-03-01";
} else {
$instancesUrl = "https://management.azure.com/subscriptions/$($subscriptionId)/resourcegroups/$($resourceGroup)/providers/Microsoft.Web/sites/$($webAppName)/instances?api-version=2022-03-01";
}
Invoke-RestMethod -Headers $RequestHeader -Uri $instancesUrl | Select-Object -ExpandProperty value | Select-Object -ExpandProperty properties;
# 2. do a health-check on each instance:
$instances = Invoke-RestMethod -Headers $RequestHeader -Uri $instancesUrl | Select-Object -ExpandProperty value | Select-Object -ExpandProperty properties;
$webAppDomain = "$($webAppName).azurewebsites.net";
$webAppUrl = "https://$($webAppDomain)/healthprobe.html";
foreach ($instance in $instances)
{
$instanceId = $instance.Name
"Going to check healthprobe URL on instance $($instanceId)";
$s = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$c = New-Object System.Net.Cookie('ARRAffinity',$instanceId,'/',$webAppDomain)
$s.Cookies.Add($c)
Invoke-WebRequest -UseBasicParsing -Method Get -Uri $webAppUrl -WebSession $s
}