使用 PowerShell 升级 Service Fabric 应用程序Service Fabric application upgrade using PowerShell


最常用的推荐升级方法是受监控的滚动升级。The most frequently used and recommended upgrade approach is the monitored rolling upgrade. Azure Service Fabric 基于一组运行状况策略监视正在升级的应用程序的运行状况。Azure Service Fabric monitors the health of the application being upgraded based on a set of health policies. 升级某个更新域 (UD) 之后,Service Fabric 将评估应用程序运行状况,根据运行状况策略继续升级下一个更新域,或者使升级失败。Once an update domain (UD) is upgraded, Service Fabric evaluates the application health and either proceeds to the next update domain or fails the upgrade depending on the health policies.

可使用托管或本机 API、PowerShell、Azure CLI、Java 或 REST 执行受监视的应用程序升级。A monitored application upgrade can be performed using the managed or native APIs, PowerShell, Azure CLI, Java, or REST. 有关使用 Visual Studio 执行升级的说明,请参阅使用 Visual Studio 升级应用程序For instructions on performing an upgrade using Visual Studio, see Upgrading your application using Visual Studio.

使用 Service Fabric 监视的滚动升级,应用程序管理员可以配置 Service Fabric 用于确定应用程序运行状况是否正常的运行状况评估策略。With Service Fabric monitored rolling upgrades, the application administrator can configure the health evaluation policy that Service Fabric uses to determine if the application is healthy. 此外,管理员还可以配置当运行状况评估失败时要采取的措施(例如,执行自动回滚)。本部分演练使用 PowerShell 对其中一个 SDK 示例进行受监视的升级。In addition, the administrator can configure the action to be taken when the health evaluation fails (for example, doing an automatic rollback.) This section walks through a monitored upgrade for one of the SDK samples that uses PowerShell.

备注

在应用程序升级期间,不会保留 ApplicationParametersApplicationParameters are not preserved across an application upgrade. 为了保留当前的应用程序参数,用户应首先获取参数,然后将它们传递到升级 API 调用中,如下所示:In order to preserve current application parameters, the user should get the parameters first and pass them into the upgrade API call like below:

$myApplication = Get-ServiceFabricApplication -ApplicationName fabric:/myApplication
$appParamCollection = $myApplication.ApplicationParameters

$applicationParameterMap = @{}
foreach ($pair in $appParamCollection)
{
    $applicationParameterMap.Add($pair.Name, $pair.Value);
}

Start-ServiceFabricApplicationUpgrade -ApplicationName fabric:/myApplication -ApplicationTypeVersion 2.0.0 -ApplicationParameter $applicationParameterMap -Monitored -FailureAction Rollback

步骤 1:构建和部署 Visual Objects 示例Step 1: Build and deploy the Visual Objects sample

单击右键应用程序项目 VisualObjectsApplication,并选择“发布”命令生成并发布应用程序。Build and publish the application by right-clicking on the application project, VisualObjectsApplication, and selecting the Publish command. 有关详细信息,请参阅 Service Fabric 应用程序升级教程For more information, see Service Fabric application upgrade tutorial. 或者,也可以使用 PowerShell 来部署应用程序。Alternatively, you can use PowerShell to deploy your application.

备注

要在 PowerShell 中使用任何 Service Fabric 命令,必须先使用 Connect-ServiceFabricCluster cmdlet 连接到群集。Before any of the Service Fabric commands may be used in PowerShell, you first need to connect to the cluster by using the Connect-ServiceFabricCluster cmdlet. 同样,假设已在本地计算机上设置了群集。Similarly, it is assumed that the Cluster has already been set up on your local machine. 请参阅设置 Service Fabric 部署环境上的文章。See the article on setting up your Service Fabric development environment.

在 Visual Studio 中构建项目后,可以使用 PowerShell 命令 Copy-ServiceFabricApplicationPackage 将应用程序包复制到 ImageStore。After building the project in Visual Studio, you can use the PowerShell command Copy-ServiceFabricApplicationPackage to copy the application package to the ImageStore. 如果要在本地验证应用包,请使用 Test-ServiceFabricApplicationPackage cmdlet。If you want to verify the app package locally, use the Test-ServiceFabricApplicationPackage cmdlet. 下一个步骤是使用 Register-ServiceFabricApplicationType cmdlet 将应用程序注册到 Service Fabric 运行时。The next step is to register the application to the Service Fabric runtime using the Register-ServiceFabricApplicationType cmdlet. 接下来的一步骤是使用 New-ServiceFabricApplication cmdlet 启动应用程序实例。The following step is to start an instance of the application by using the New-ServiceFabricApplication cmdlet. 这三个步骤类似于使用 Visual Studio 中的“部署”菜单项。These three steps are analogous to using the Deploy menu item in Visual Studio. 完成预配后,应从映像存储区中清除复制的应用程序包以减少占用的资源。Once provisioning is completed, you should clean up the copied application package from the image store in order to reduce the resources consumed. 如果不再需要某一应用程序类型,它应出于同样的原因将其注销。If an application type is no longer required, it should be unregistered for the same reason. 有关详细信息,请按照使用 PowerShell 部署和删除应用程序See Deploy and remove applications using PowerShell for more information.

现在可以使用 Service Fabric Explorer 查看群集和应用程序Now, you can use Service Fabric Explorer to view the cluster and the application. 应用程序具有一个 Web 服务,可通过在 Internet Explorer 地址栏中键入 http://localhost:8081/visualobjects 导航到该 Web 服务。The application has a web service that can be navigated to in Internet Explorer by typing http://localhost:8081/visualobjects in the address bar. 应在屏幕上看到一些四处移动的浮动视觉对象。You should see some floating visual objects moving around in the screen. 此外,可使用 Get-ServiceFabricApplication 检查应用程序状态。Additionally, you can use Get-ServiceFabricApplication to check the application status.

步骤 2:更新视觉对象示例Step 2: Update the Visual Objects sample

你可能会注意到,使用步骤 1 中部署的版本,视觉对象不会旋转。You might notice that with the version that was deployed in Step 1, the visual objects do not rotate. 让我们将此应用程序升级到视觉对象也会旋转的版本。Let's upgrade this application to one where the visual objects also rotate.

选择 VisualObjects 解决方案中的 VisualObjects.ActorService 项目,并打开该项目中的 StatefulVisualObjectActor.cs 文件。Select the VisualObjects.ActorService project within the VisualObjects solution, and open the StatefulVisualObjectActor.cs file. 在该文件内,导航到 MoveObject 方法,注释掉 this.State.Move(),并取消注释 this.State.Move(true)Within that file, navigate to the method MoveObject, comment out this.State.Move(), and uncomment this.State.Move(true). 此项更改可在升级服务后旋转对象。This change rotates the objects after the service is upgraded.

我们还需要更新 VisualObjects.ActorService 项目的 ServiceManifest.xml 文件(位于 PackageRoot 下)。We also need to update the ServiceManifest.xml file (under PackageRoot) of the project VisualObjects.ActorService. CodePackage 和服务版本更新到 2.0,以及 ServiceManifest.xml 文件中的相应行。Update the CodePackage and the service version to 2.0, and the corresponding lines in the ServiceManifest.xml file. 可以在右键单击解决方案之后,使用 Visual Studio 中的“编辑清单文件”选项来进行清单文件更改。You can use the Visual Studio Edit Manifest Files option after you right-click on the solution to make the manifest file changes.

完成更改后,清单应该如下所示(突出显示的部分即为所做的更改):After the changes are made, the manifest should look like the following (highlighted portions show the changes):

<ServiceManifestName="VisualObjects.ActorService" Version="2.0" xmlns="http://schemas.microsoft.com/2011/01/fabric" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance">

<CodePackageName="Code" Version="2.0">

现在,ApplicationManifest.xml 文件(位于 VisualObjects 解决方案下的 VisualObjects 项目下)已更新为 VisualObjects.ActorService 项目的 2.0 版。Now the ApplicationManifest.xml file (found under the VisualObjects project under the VisualObjects solution) is updated to version 2.0 of the VisualObjects.ActorService project. 此外,应用程序版本已从 1.0.0.0 更新为 2.0.0.0。In addition, the Application version is updated to 2.0.0.0 from 1.0.0.0. ApplicationManifest.xml 应类似于以下代码片段:The ApplicationManifest.xml should look like the following snippet:

<ApplicationManifestxmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" ApplicationTypeName="VisualObjects" ApplicationTypeVersion="2.0.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric">

 <ServiceManifestRefServiceManifestName="VisualObjects.ActorService" ServiceManifestVersion="2.0" />

现在请生成项目,方法是选择 ActorService 项目,并单击右键并选择 Visual Studio 中的“生成”选项。Now, build the project by selecting just the ActorService project, and then right-clicking and selecting the Build option in Visual Studio. 如果选择“全部重新生成”,应该更新所有项目的版本,因为代码已更改。If you select Rebuild all, you should update the versions for all projects, since the code would have changed. 接下来,将更新的应用程序打包,方法是:右键单击“VisualObjectsApplication”,选择 Service Fabric 菜单,并选择“打包”Next, let's package the updated application by right-clicking on *VisualObjectsApplication_, selecting the Service Fabric Menu, and choosing _* Package**. 此操作创建可部署的应用程序包。This action creates an application package that can be deployed. 更新的应用程序已准备就绪,可供部署。Your updated application is ready to be deployed.

步骤 3:确定运行状况策略和升级参数Step 3: Decide on health policies and upgrade parameters

请熟悉应用程序升级参数升级过程,充分了解所应用的各种升级参数、超时和运行状况条件。Familiarize yourself with the application upgrade parameters and the upgrade process to get a good understanding of the various upgrade parameters, time-outs, and health criterion applied. 对于本演练,服务运行状况评估条件设置为默认值(即推荐值),这意味着在升级后所有服务和实例均应为 运行状况正常For this walkthrough, the service health evaluation criterion is set to the default (and recommended) values, which means that all services and instances should be healthy after the upgrade.

但是,让我们将 HealthCheckStableDuration 增加到 180 秒(这样该服务在进行下一个更新域的升级之前将至少保持 120 秒的运行状况正常)。However, let's increase the HealthCheckStableDuration to 180 seconds (so that the services are healthy for at least 120 seconds before the upgrade proceeds to the next update domain). 我们还将 UpgradeDomainTimeout 设置为 1200 秒,将 UpgradeTimeout 设置为 3000 秒。Let's also set the UpgradeDomainTimeout to be 1200 seconds and the UpgradeTimeout to be 3000 seconds.

最后,将 UpgradeFailureAction 设置为 rollback。Finally, let's also set the UpgradeFailureAction to rollback. 此选项要求 Service Fabric 在升级期间遇到任何问题时,将应用程序回退到前一版本。This option requires Service Fabric to roll back the application to the previous version if it encounters any issues during the upgrade. 因此在开始升级(步骤 4)时指定了以下参数:Thus, when starting the upgrade (in Step 4), the following parameters are specified:

FailureAction = RollbackFailureAction = Rollback

HealthCheckStableDurationSec = 180HealthCheckStableDurationSec = 180

UpgradeDomainTimeoutSec = 1200UpgradeDomainTimeoutSec = 1200

UpgradeTimeout = 3000UpgradeTimeout = 3000

步骤 4:为升级准备应用程序Step 4: Prepare application for upgrade

现在,应用程序已生成并准备好进行升级。Now the application is built and ready to be upgraded. 如果以管理员身份打开 PowerShell 窗口并键入 Get-ServiceFabricApplication,会看到已部署好的 1.0.0.0 应用程序类型的名为 VisualObjects 的应用程序。If you open up a PowerShell window as an administrator and type Get-ServiceFabricApplication, it should let you know that it is application type 1.0.0.0 of VisualObjects that's been deployed.

应用程序包存储在解压 Service Fabric SDK 的位置,其相对路径为 Samples\Services\Stateful\VisualObjects\VisualObjects\obj\x64\DebugThe application package is stored under the following relative path where you uncompressed the Service Fabric SDK - Samples\Services\Stateful\VisualObjects\VisualObjects\obj\x64\Debug. 该目录中应会出现一个“Package”文件夹,这是存储应用程序包的位置。You should find a "Package" folder in that directory, where the application package is stored. 请检查时间戳,确保它是最新版本(可能还需要相应地修改路径)。Check the timestamps to ensure that it is the latest build (you may need to modify the paths appropriately as well).

现在,让我们将更新后的应用程序包复制到 Service Fabric ImageStore(Service Fabric 存储应用程序包的位置)。Now let's copy the updated application package to the Service Fabric ImageStore (where the application packages are stored by Service Fabric). 参数 ApplicationPackagePathInImageStore 告知 Service Fabric 可在何处找到应用程序包。The parameter ApplicationPackagePathInImageStore informs Service Fabric where it can find the application package. 我们可以使用以下命令将更新的应用程序放入“VisualObjects_V2”(可能需要再次相应地修改路径)。We have put the updated application in "VisualObjects_V2" with the following command (you may need to modify paths again appropriately).

Copy-ServiceFabricApplicationPackage -ApplicationPackagePath .\Samples\Services\Stateful\VisualObjects\VisualObjects\obj\x64\Debug\Package -ApplicationPackagePathInImageStore "VisualObjects\_V2"

下一步是将此应用程序注册到 Service Fabric,这可以使用 Register-ServiceFabricApplicationType 命令来执行:The next step is to register this application with Service Fabric, which can be performed using the Register-ServiceFabricApplicationType command:

Register-ServiceFabricApplicationType -ApplicationPathInImageStore "VisualObjects\_V2"

如果上面的命令未成功,可能需要重新生成所有服务。If the preceding command doesn't succeed, it is likely that you need a rebuild of all services. 如步骤 2 中所述,可能还需要更新 WebService 版本。As mentioned in Step 2, you may have to update your WebService version as well.

建议在成功注册应用程序后删除应用程序包。It's recommended that you remove the application package after the application is successfully registered. 从映像存储区中删除应用程序包可以释放系统资源。Deleting application packages from the image store frees up system resources. 保留未使用的应用程序包会占用磁盘存储空间,导致应用程序出现性能问题。Keeping unused application packages consumes disk storage and leads to application performance issues.

Remove-ServiceFabricApplicationPackage -ApplicationPackagePathInImageStore "VisualObjects\_V2" -ImageStoreConnectionString fabric:ImageStore

步骤 5:启动应用程序升级Step 5: Start the application upgrade

现在可以使用 Start-ServiceFabricApplicationUpgrade 命令开始升级应用程序:Now, we're all set to start the application upgrade by using the Start-ServiceFabricApplicationUpgrade command:

Start-ServiceFabricApplicationUpgrade -ApplicationName fabric:/VisualObjects -ApplicationTypeVersion 2.0.0.0 -HealthCheckStableDurationSec 60 -UpgradeDomainTimeoutSec 1200 -UpgradeTimeout 3000   -FailureAction Rollback -Monitored

应用程序名称与 ApplicationManifest.xml 文件中所述的相同。The application name is the same as it was described in the ApplicationManifest.xml file. Service Fabric 使用此名称来确定升级的应用程序。Service Fabric uses this name to identify which application is getting upgraded. 如果设置的超时太短,则可能遇到一条说明该问题的失败消息。If you set the time-outs to be too short, you may encounter a failure message that states the problem. 请参阅故障排除部分,或增加超时值。Refer to the troubleshooting section, or increase the time-outs.

现在,在应用程序升级进行时,可以使用 Service Fabric Explorer 或 PowerShell 命令 Get-ServiceFabricApplicationUpgrade 对其进行监视:Now, as the application upgrade proceeds, you can monitor it using Service Fabric Explorer, or by using the Get-ServiceFabricApplicationUpgrade PowerShell command:

Get-ServiceFabricApplicationUpgrade fabric:/VisualObjects

几分钟后,使用上述 PowerShell 命令获得的状态应说明所有更新域均已升级(已完成)。In a few minutes, the status that you got by using the preceding PowerShell command, should state that all update domains were upgraded (completed). 此外,浏览器窗口中的视觉对象已开始旋转!And you should find that the visual objects in your browser window have started rotating!

可以练习从版本 2 升级到版本 3,或者从版本 2 升级到版本 1。You can try upgrading from version 2 to version 3, or from version 2 to version 1 as an exercise. 从版本 2 过渡到版本 1 也被视为升级。Moving from version 2 to version 1 is also considered an upgrade. 尝试练习使用超时和运行状况策略,以便加深对它的熟悉。Play with time-outs and health policies to make yourself familiar with them. 部署到 Azure 群集时,需要正确设置参数。When you are deploying to an Azure cluster, the parameters need to be set appropriately. 最好保守地设置超时。It is good to set the time-outs conservatively.

后续步骤Next steps

使用 Visual Studio 升级应用程序逐步讲解了如何使用 Visual Studio 进行应用程序升级。Upgrading your application using Visual Studio walks you through an application upgrade using Visual Studio.

使用升级参数来控制应用程序的升级方式。Control how your application upgrades by using upgrade parameters.

了解如何使用数据序列化,使应用程序在升级后保持兼容。Make your application upgrades compatible by learning how to use data serialization.

参考高级主题,了解如何在升级应用程序时使用高级功能。Learn how to use advanced functionality while upgrading your application by referring to Advanced topics.

参考对应用程序升级进行故障排除中的步骤来解决应用程序升级时的常见问题。Fix common problems in application upgrades by referring to the steps in Troubleshooting application upgrades.