使用 Azure Service Fabric CLI (sfctl) 管理 Azure Service Fabric 应用程序Manage an Azure Service Fabric application by using Azure Service Fabric CLI (sfctl)

了解如何创建和删除在 Azure Service Fabric 群集中运行的应用程序。Learn how to create and delete applications that are running in an Azure Service Fabric cluster.

先决条件Prerequisites

概述Overview

若要部署新应用程序,请完成以下步骤:To deploy a new application, complete these steps:

  1. 将应用程序包上传到 Service Fabric 映像存储中。Upload an application package to the Service Fabric image store.
  2. 预配应用程序类型。Provision an application type.
  3. 删除映像存储内容。Delete the image store content.
  4. 指定并创建应用程序。Specify and create an application.
  5. 指定并创建服务。Specify and create services.

若要删除现有应用程序,请完成以下步骤:To remove an existing application, complete these steps:

  1. 删除应用程序。Delete the application.
  2. 取消预配关联的应用程序类型。Unprovision the associated application type.

部署新应用程序Deploy a new application

要部署新应用程序,请完成以下任务:To deploy a new application, complete the following tasks:

将新应用程序包上传到映像存储区Upload a new application package to the image store

在创建应用程序之前,先将应用程序包上传到 Service Fabric 映像存储。Before you create an application, upload the application package to the Service Fabric image store.

例如,如果应用程序包在 app_package_dir 目录中,请使用以下命令上传该目录:For example, if your application package is in the app_package_dir directory, use the following commands to upload the directory:

sfctl application upload --path ~/app_package_dir

对于大型应用程序包,可以指定 --show-progress 选项来显示上传进度。For large application packages, you can specify the --show-progress option to display the progress of the upload.

预配应用程序类型Provision the application type

完成上传后,预配应用程序。When the upload is finished, provision the application. 若要预配应用程序,请运行以下命令:To provision the application, use the following command:

sfctl application provision --application-type-build-path app_package_dir

application-type-build-path 的值是将应用程序包上传到的目录的名称。The value for application-type-build-path is the name of the directory where you uploaded your application package.

删除应用程序包Delete the application package

建议在成功注册应用程序后删除应用程序包。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.

若要从映像存储中删除应用程序包,请使用以下命令:To delete the application package from the image store, use the following command:

sfctl store delete --content-path app_package_dir

content-path 必须是创建应用程序时上传的目录的名称。content-path must be the name of the directory that you uploaded when you created the application.

基于应用程序类型创建应用程序Create an application from an application type

预配应用程序后,使用以下命令来命名并创建应用程序:After you provision the application, use the following command to name and create your application:

sfctl application create --app-name fabric:/TestApp --app-type TestAppType --app-version 1.0

app-name 是要用于应用程序实例的名称。app-name is the name that you want to use for the application instance. 可以从前面预配的应用程序清单获取其他参数。You can get additional parameters from the previously provisioned application manifest.

应用程序名称必须以 fabric:/ 前缀开头。The application name must start with the prefix fabric:/.

为新应用程序创建服务Create services for the new application

创建应用程序后,通过该应用程序创建服务。After you have created an application, create services from the application. 在以下示例中,我们通过自己的应用程序创建新的无状态服务。In the following example, we create a new stateless service from our application. 在前面预配的应用程序包中的服务清单内,定义可通过应用程序创建的服务。The services that you can create from an application are defined in a service manifest in the previously provisioned application package.

sfctl service create --app-id TestApp --name fabric:/TestApp/TestSvc --service-type TestServiceType \
--stateless --instance-count 1 --singleton-scheme

验证应用程序部署和运行状况Verify application deployment and health

要验证一切运行正常,请使用以下运行状况命令:To verify everything is healthy, use the following health commands:

sfctl application list
sfctl service list --application-id TestApp

若要验证服务是否正常运行,请使用类似的命令检索服务和应用程序的运行状况:To verify that the service is healthy, use similar commands to retrieve the health of both the service and the application:

sfctl application health --application-id TestApp
sfctl service health --service-id TestApp/TestSvc

正常的服务和应用程序的 HealthState 值为 OkHealthy services and applications have a HealthState value of Ok.

删除现有应用程序Remove an existing application

要删除应用程序,请完成以下任务:To remove an application, complete the following tasks:

删除应用程序Delete the application

若要删除应用程序,请使用以下命令:To delete the application, use the following command:

sfctl application delete --application-id TestEdApp

取消预配应用程序类型Unprovision the application type

删除应用程序后,如果不再需要它,可以取消预配应用程序类型。After you delete the application, you can unprovision the application type if you no longer need it. 若要取消预配应用程序类型,请使用以下命令:To unprovision the application type, use the following command:

sfctl application unprovision --application-type-name TestAppType --application-type-version 1.0

类型名称和类型版本必须与前面预配的应用程序清单中的名称和版本匹配。The type name and type version must match the name and version in the previously provisioned application manifest.

升级应用程序Upgrade application

创建应用程序后,可重复一组相同的步骤来预配应用程序的第二个版本。After creating your application, you can repeat the same set of steps to provision a second version of your application. 然后,可通过 Service Fabric 应用程序升级转换为运行第二个版本的应用程序。Then, with a Service Fabric application upgrade you can transition to running the second version of the application. 有关详细信息,请参阅有关 Service Fabric 应用程序升级的文档。For more information, see the documentation on Service Fabric application upgrades.

要执行升级,请首先使用与之前相同的命令预配应用程序的下一个版本:To perform an upgrade, first provision the next version of the application using the same commands as before:

sfctl application upload --path ~/app_package_dir_2
sfctl application provision --application-type-build-path app_package_dir_2
sfctl store delete --content-path app_package_dir_2

建议随后执行受监视的自动升级,通过运行以下命令启动升级:It is recommended then to perform a monitored automatic upgrade, launch the upgrade by running the following command:

sfctl application upgrade --app-id TestApp --app-version 2.0.0 --parameters "{\"test\":\"value\"}" --mode Monitored

升级可通过指定的任何设置覆盖现有参数。Upgrades override existing parameters with whatever set is specified. 如有必要,应用程序参数应作为参数传递到升级命令。Application parameters should be passed as arguments to the upgrade command, if necessary. 应用程序参数应编码为 JSON 对象。Application parameters should be encoded as a JSON object.

要检索以前指定的任何参数,可使用 sfctl application info 命令。To retrieve any parameters previously specified, you can use the sfctl application info command.

在应用程序升级过程时,可使用 sfctl application upgrade-status 命令检索状态。When an application upgrade is in progress, the status can be retrieved using the sfctl application upgrade-status command.

最后,如果正在进行升级但需要取消此次升级,可使用 sfctl application upgrade-rollback 回滚升级。Finally, if an upgrade is in progress and needs to be canceled, you can use the sfctl application upgrade-rollback to roll back the upgrade.

后续步骤Next steps