升级 Service Fabric 应用程序
此示例脚本可将正在运行的 Service Fabric 应用程序实例升级为版本 1.3.0。 该脚本会将新应用程序包复制到群集映像存储区,注册应用程序类型并删除不必要的应用程序包。 该脚本会启动受监控的升级,并持续检查升级状态,直到升级完成或回滚。 根据需要自定义参数。
必要时,使用 Service Fabric SDK 安装 Service Fabric PowerShell 模块。
示例脚本
## 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."
}
}
脚本说明
此脚本使用以下命令。 表中的每条命令均链接到特定于命令的文档。
命令 | 说明 |
---|---|
Get-ServiceFabricApplication | 获取 Service Fabric 群集中的所有应用程序或特定应用程序。 |
Get-ServiceFabricApplicationUpgrade | 获取 Service Fabric 应用程序的升级状态。 |
Get-ServiceFabricApplicationType | 获取在 Service Fabric 群集上注册的 Service Fabric 应用程序类型。 |
Unregister-ServiceFabricApplicationType | 注销 Service Fabric 应用程序类型。 |
Copy-ServiceFabricApplicationPackage | 将 Service Fabric 应用程序包复制到映像存储。 |
Register-ServiceFabricApplicationType | 注册 Service Fabric 应用程序类型。 |
Start-ServiceFabricApplicationUpgrade | 将 Service Fabric 应用程序升级为指定的应用程序类型版本。 |
Remove-ServiceFabricApplicationPackage | 从映像存储区中删除 Service Fabric 应用程序包。 |
后续步骤
有关 Service Fabric PowerShell 模块的详细信息,请参阅 Azure PowerShell 文档。
可以在 Azure PowerShell 示例中找到 Azure Service Fabric 的其他 PowerShell 示例。