是否有每隔一段时间重新启动 Azure 经典云服务角色?

Is there anyway to restart an Azure classic cloud service role every interval?

我正在使用具有多个角色进程的 Azure 云服务(经典)。其中一个是一周后变得有点不稳定的工人,所以我想每隔几天重新启动一次。最终工作者角色将变得稳定,但与此同时,如果可能的话,每隔几天自动重启它会很好。

有没有办法每天重启一个 Azure 经典云服务工作者角色?以编程方式还是通过配置?

绝对是的,有两种方法可以通过按间隔以编程方式触发来重启 Azure 经典云服务角色实例。

  1. 在编程中使用 crontab 触发器调用 REST API Reboot Role Instance
  2. 您可以通过在编程中调用REST API Virtual Machines - Restart 或直接使用Azure SDK 的相同功能API 编程语言来重启角色的这些虚拟机。

我在 Azure 论坛和 Reddit 上问过这个问题。

第一反应是在 Azure 论坛上, Marcin 说:

You can use for this purpose Azure Automation

https://docs.microsoft.com/en-us/azure/cloud-services/automation-manage-cloud-services

https://gallery.technet.microsoft.com/scriptcenter/Reboot-Cloud-Service-PaaS-b337a06d

然后 Reddit, quentech said:

You can do it with a PowerShell Workflow Runbook:

workflow ResetRoleClassic
{
    Param
    (
        [Parameter (Mandatory = $true)]
        [string]$serviceName,
        [Parameter (Mandatory = $true)]
        [string]$slot,
        [Parameter (Mandatory = $true)]
        [string]$instanceName
    )  

    $ConnectionAssetName = "AzureClassicRunAsConnection"

    # Get the connection
    $connection = Get-AutomationConnection -Name $connectionAssetName        

    # Authenticate to Azure with certificate
    Write-Verbose "Get connection asset: $ConnectionAssetName" -Verbose
    $Conn = Get-AutomationConnection -Name $ConnectionAssetName
    if ($Conn -eq $null)
    {
        throw "Could not retrieve connection asset: $ConnectionAssetName. Assure that this asset exists in the Automation account."
    }

    $CertificateAssetName = $Conn.CertificateAssetName
    Write-Verbose "Getting the certificate: $CertificateAssetName" -Verbose
    $AzureCert = Get-AutomationCertificate -Name $CertificateAssetName

    if ($AzureCert -eq $null)
    {
        throw "Could not retrieve certificate asset: $CertificateAssetName. Assure that this asset exists in the Automation account."
    }

    Write-Verbose "Authenticating to Azure with certificate." -Verbose    
    Set-AzureSubscription -SubscriptionName $Conn.SubscriptionName -SubscriptionId $Conn.SubscriptionID -Certificate $AzureCert 

    Select-AzureSubscription -SubscriptionId $Conn.SubscriptionID

    Write-Verbose "Getting $serviceName Role." -Verbose

    $results = Get-AzureRole -ServiceName $serviceName -InstanceDetails
    Write-Output $results

    Write-Verbose "Resetting Role Instance $instanceName" -Verbose

    $results = Reset-AzureRoleInstance -ServiceName $serviceName -Slot $slot -InstanceName $instanceName -Reboot    
    Write-Output $results
}

我对参数做了一些小改动,并删除了外括号。因此能够在大多数情况下按原样使用脚本。