升级 Service Fabric 应用程序Upgrade a Service Fabric application

此示例脚本可将正在运行的 Service Fabric 应用程序实例升级为版本 1.3.0。This sample script upgrades a running Service Fabric application instance to version 1.3.0. 该脚本会将新应用程序包复制到群集映像存储区,注册应用程序类型并删除不必要的应用程序包。The script copies the new application package to the cluster image store, registers the application type, and removes the unnecessary application package. 该脚本会启动受监控的升级,并持续检查升级状态,直到升级完成或回滚。The script starts a monitored upgrade and continuously checks the upgrade status until the upgrade completes or rolls back. 根据需要自定义参数。Customize the parameters as needed.

必要时,使用 Service Fabric SDK 安装 Service Fabric PowerShell 模块。If needed, install the Service Fabric PowerShell module with the Service Fabric SDK.

示例脚本Sample script

## Variables
$ApplicationPackagePath = "C:\Users\sfuser\documents\visual studio 2017\Projects\Voting\Voting\pkg\Debug"
$ApplicationName = "fabric:/Voting"
$ApplicationTypeName = "VotingType"
$ApplicationTypeVersion = "1.3.0"
$imageStoreConnectionString = "fabric:ImageStore"
$CopyPackageTimeoutSec = 600
$CompressPackage = $false

## Check existence of the application
$oldApplication = Get-ServiceFabricApplication -ApplicationName $ApplicationName

if (!$oldApplication)
{
    $errMsg = "Application '$ApplicationName' doesn't exist in cluster."
    throw $errMsg
}
else
{
    ## Check upgrade status
    $upgradeStatus = Get-ServiceFabricApplicationUpgrade -ApplicationName $ApplicationName
    if ($upgradeStatus.UpgradeState -ne "RollingBackCompleted" -and $upgradeStatus.UpgradeState -ne "RollingForwardCompleted" -and $upgradeStatus.UpgradeState -ne "Failed")
    {
        $errMsg = "An upgrade for the application '$ApplicationTypeName' is already in progress."
        throw $errMsg
    }

    $reg = Get-ServiceFabricApplicationType -ApplicationTypeName $ApplicationTypeName | Where-Object  { $_.ApplicationTypeVersion -eq $ApplicationTypeVersion }
    if ($reg)
    {
        Write-Host 'Application Type '$ApplicationTypeName' and Version '$ApplicationTypeVersion' was already registered with Cluster, unregistering it...'
        $reg | Unregister-ServiceFabricApplicationType -Force
    }

    ## Copy application package to image store
    $applicationPackagePathInImageStore = $ApplicationTypeName
    Write-Host "Copying application package to image store..."
    Copy-ServiceFabricApplicationPackage -ApplicationPackagePath $ApplicationPackagePath -ImageStoreConnectionString $imageStoreConnectionString -ApplicationPackagePathInImageStore $applicationPackagePathInImageStore -TimeOutSec $CopyPackageTimeoutSec -CompressPackage:$CompressPackage 
    if(!$?)
    {
        throw "Copying of application package to image store failed. Cannot continue with registering the application."
    }

    ## Register application type
    Write-Host "Registering application type..."
    Register-ServiceFabricApplicationType -ApplicationPathInImageStore $applicationPackagePathInImageStore
    if(!$?)
    {
        throw "Registration of application type failed."
    }

    # Remove the application package to free system resources.
    Remove-ServiceFabricApplicationPackage -ImageStoreConnectionString $imageStoreConnectionString -ApplicationPackagePathInImageStore $applicationPackagePathInImageStore
    if(!$?)
    {
        Write-Host "Removing the application package failed."
    }

    ## Start monitored application upgrade
    try
    {
        Write-Host "Start upgrading application..." 
        Start-ServiceFabricApplicationUpgrade -ApplicationName $ApplicationName -ApplicationTypeVersion $ApplicationTypeVersion -HealthCheckStableDurationSec 60 -UpgradeDomainTimeoutSec 1200 -UpgradeTimeout 3000 -FailureAction Rollback -Monitored
    }
    catch
    {
        Write-Host ("Error starting upgrade. " + $_)

        Write-Host "Unregister application type '$ApplicationTypeName' and version '$ApplicationTypeVersion' ..."
        Unregister-ServiceFabricApplicationType -ApplicationTypeName $ApplicationTypeName -ApplicationTypeVersion $ApplicationTypeVersion -Force
        throw
    }

    do
    {
        Write-Host "Waiting for upgrade..."
        Start-Sleep -Seconds 3
        $upgradeStatus = Get-ServiceFabricApplicationUpgrade -ApplicationName $ApplicationName
    } while ($upgradeStatus.UpgradeState -ne "RollingBackCompleted" -and $upgradeStatus.UpgradeState -ne "RollingForwardCompleted" -and $upgradeStatus.UpgradeState -ne "Failed")

    if($upgradeStatus.UpgradeState -eq "RollingForwardCompleted")
    {
        Write-Host "Upgrade completed successfully."
    }
    elseif($upgradeStatus.UpgradeState -eq "RollingBackCompleted")
    {
        Write-Error "Upgrade was Rolled back."
    }
    elseif($upgradeStatus.UpgradeState -eq "Failed")
    {
        Write-Error "Upgrade Failed."
    }
}

脚本说明Script explanation

此脚本使用以下命令。This script uses the following commands. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

CommandCommand 说明Notes
Get-ServiceFabricApplicationGet-ServiceFabricApplication 获取 Service Fabric 群集中的所有应用程序或特定应用程序。Gets all the applications in the Service Fabric cluster or a specific application.
Get-ServiceFabricApplicationUpgradeGet-ServiceFabricApplicationUpgrade 获取 Service Fabric 应用程序的升级状态。Gets the status of a Service Fabric application upgrade.
Get-ServiceFabricApplicationTypeGet-ServiceFabricApplicationType 获取在 Service Fabric 群集上注册的 Service Fabric 应用程序类型。Gets the Service Fabric application types registered on the Service Fabric cluster.
Unregister-ServiceFabricApplicationTypeUnregister-ServiceFabricApplicationType 注销 Service Fabric 应用程序类型。Unregisters a Service Fabric application type.
Copy-ServiceFabricApplicationPackageCopy-ServiceFabricApplicationPackage 将 Service Fabric 应用程序包复制到映像存储。Copies a Service Fabric application package to the image store.
Register-ServiceFabricApplicationTypeRegister-ServiceFabricApplicationType 注册 Service Fabric 应用程序类型。Registers a Service Fabric application type.
Start-ServiceFabricApplicationUpgradeStart-ServiceFabricApplicationUpgrade 将 Service Fabric 应用程序升级为指定的应用程序类型版本。Upgrades a Service Fabric application to the specified application type version.
Remove-ServiceFabricApplicationPackageRemove-ServiceFabricApplicationPackage 从映像存储区中删除 Service Fabric 应用程序包。Removes a Service Fabric application package from the image store.

后续步骤Next steps

有关 Service Fabric PowerShell 模块的详细信息,请参阅 Azure PowerShell 文档For more information on the Service Fabric PowerShell module, see Azure PowerShell documentation.

可以在 Azure PowerShell 示例中找到 Azure Service Fabric 的其他 Powershell 示例。Additional Powershell samples for Azure Service Fabric can be found in the Azure PowerShell samples.