Service Fabric 应用程序资源模型是什么?What is the Service Fabric application resource model?

建议通过 Azure 资源管理器将 Service Fabric 应用程序部署到 Service Fabric 群集。It is recommended that Service Fabric applications are deployed onto your Service Fabric cluster via Azure Resource Manager. 使用此方法可以在 JSON 中描述应用程序和服务,并将其部署到群集所在的同一资源管理器模板中。This method makes it possible to describe applications and services in JSON and deploy them in the same Resource Manager template as your cluster. 与通过 PowerShell 或 Azure CLI 部署和管理应用程序相反,无需等待群集准备就绪。As opposed to deploying and managing applications via PowerShell or Azure CLI, there is no need to wait for the cluster to be ready. 只需执行一步操作,即可完成注册、预配和部署应用程序的整个过程。The process of application registration, provisioning, and deployment can all happen in one step. 这是在群集中管理应用程序生命周期的最佳做法。This is the best practice to manage application life cycle in your cluster. 有关详细信息,请查看最佳做法For more information, look at best practices.

在适当情况下,将应用程序作为资源管理器资源进行管理可改进:When applicable, manage your applications as Resource Manager resources to improve:

  • 审核线索:资源管理器审核所有操作,并记录详细的活动日志 ,有助于跟踪对这些应用程序和群集做出的任何更改。Audit trail: Resource Manager audits every operation and keeps a detailed Activity Log that can help you trace any changes made to these applications and your cluster.
  • 基于角色的访问控制:可通过同一个资源管理器模板,管理对群集及其上部署的应用程序的访问。Role-based access control: Managing access to clusters as well as applications deployed on the cluster can be done via the same Resource Manager template.
  • Azure 资源管理器(通过 Azure 门户)成为管理群集和关键应用程序部署的一站式平台。Azure Resource Manager (via the Azure portal) becomes a one-stop-shop for managing your cluster and critical application deployments.

使用 Azure 资源管理器管理 Service Fabric 应用程序生命周期Service Fabric application life cycle with Azure Resource Manager

本文档将介绍以下操作:In this document, you will learn how to:

  • 使用 Azure 资源管理器部署应用程序资源Deploy application resources using Azure Resource Manager
  • 使用 Azure 资源管理器升级应用程序资源Upgrade application resources using Azure Resource Manager
  • 删除应用程序资源Delete application resources

使用 Azure 资源管理器部署应用程序资源Deploy application resources using Azure Resource Manager

若要使用 Azure 资源管理器应用程序资源模型部署应用程序及其服务,需要打包应用程序代码,上传该包,然后在 Azure 资源管理器模板中以应用程序资源的形式引用该包的位置。To deploy an application and its services using the Azure Resource Manager application resource model, you need to package application code, upload the package, and then reference the location of package in an Azure Resource Manager template as an application resource. 有关详细信息,请查看将应用程序打包For more information, view Package an application.

然后创建 Azure 资源管理器模板,使用应用程序详细信息更新参数文件,并将其部署到 Service Fabric 群集。Then, create an Azure Resource Manager template, update the parameters file with application details, and deploy it on the Service Fabric cluster. 请参阅此处的示例。Refer to samples here.

创建存储帐户Create a Storage account

从资源管理器模板部署应用程序需要使用一个存储帐户来暂存应用程序映像。Deploying an application from a Resource Manager template requires a storage account to stage the application image. 可以重复使用现有的存储帐户,或者创建新的存储帐户来暂存应用程序。You can re-use an existing storage account or create a new storage account to stage your applications. 如果使用现有的存储帐户,则可以跳过此步骤。If you would like to use an existing storage account, you can skip this step.

创建存储帐户

配置存储帐户Configure Storage account

创建存储帐户后,需要创建一个可在其中暂存应用程序的 Blob 容器。Once the storage account has been created, you need to create a blob container where the applications can be staged. 在 Azure 门户中,导航到用于存储应用程序的存储帐户。In the Azure portal, navigate to the storage account that you would like to store your applications. 选择“Blob”边栏选项卡,单击“添加容器”按钮。 Select the Blobs blade, and click the Add Container button. 添加使用 Blob 公共访问级别的新容器。Add a new container with Blob Public access level.

创建 Blob

在存储帐户中暂存应用程序Stage application in a Storage account

在部署应用程序之前,必须先将其暂存在 Blob 存储中。Before the application can be deployed, it must be staged in blob storage. 在本教程中,我们将手动创建应用程序包,不过也可以将此步骤自动化。In this tutorial we will create the application package manually, however this step can be automated. 有关详细信息,请查看将应用程序打包For more information, view Package an application. 下面的步骤将使用 Voting 示例应用程序In the following steps the Voting sample application will be used.

  1. 在 Visual Studio 中右键单击 Voting 项目并选择“打包”。In Visual Studio right-click on the Voting project and select package.
    将应用程序打包
  2. 打开刚刚创建的 .\service-fabric-dotnet-quickstart\Voting\pkg\Debug 目录,将内容压缩成名为 Voting.zip 的文件,并将 ApplicationManifest.xml 置于该 zip 文件的根目录。Open the .\service-fabric-dotnet-quickstart\Voting\pkg\Debug directory that was just create, and zip the contents into a file called Voting.zip such that the ApplicationManifest.xml is at the root of the zip file.
    压缩应用程序Zip Application
  3. 将该文件的扩展名从 .zip 重命名为 .sfpkgRename the extension of the file from .zip to .sfpkg.
  4. 在 Azure 门户上你的存储帐户的 apps 容器中,单击“上传”并上传 Voting.sfpkgIn the Azure portal, in the apps container of your storage account, click Upload and upload Voting.sfpkg.
    上传应用包Upload App Package

该应用程序随即已暂存。The application is now staged. 现在,我们可以开始创建 Azure 资源管理器模板来部署该应用程序。We are now ready to create the Azure Resource Manager template to deploy the application.

创建 Azure 资源管理器模板Create the Azure Resource Manager template

示例应用程序包含可用于部署应用程序的 Azure 资源管理器模板The sample application contains Azure Resource Manager Templates that can be used to deploy the application. 模板文件名为 UserApp.jsonUserApp.Parameters.jsonThe template files are named UserApp.json and UserApp.Parameters.json.

Note

必须使用群集名称更新 UserApp.Parameters.json 文件。The UserApp.Parameters.json file must be updated with the name of your cluster.

参数Parameter 说明Description 示例Example 注释Comments
clusterNameclusterName 要部署到的群集的名称The name of the cluster you're deploying to sf-cluster123sf-cluster123
applicationapplication 应用程序的名称The name of the application VotingVoting
applicationTypeNameapplicationTypeName 应用程序的类型名称The type name of the application VotingTypeVotingType 必须与 ApplicationManifest.xml 中的内容匹配Must match what's in ApplicationManifest.xml
applicationTypeVersionapplicationTypeVersion 应用程序类型的版本The version of the application type 1.0.01.0.0 必须与 ApplicationManifest.xml 中的内容匹配Must match what's in ApplicationManifest.xml
serviceNameserviceName 服务的名称The name of the service the service Voting~VotingWebVoting~VotingWeb 必须采用 ApplicationName~ServiceType 格式Must be in the format ApplicationName~ServiceType
serviceTypeNameserviceTypeName 服务的类型名称The type name of the service VotingWebVotingWeb 必须与 ServiceManifest.xml 中的内容匹配Must match what's in the ServiceManifest.xml
appPackageUrlappPackageUrl 应用程序的 Blob 存储 URLThe blob storage URL of the application https://servicefabricapps.blob.core.chinacloudapi.cn/apps/Voting.sfpkg Blob 存储中应用程序包的 URL(下面介绍了设置过程)The URL of the application package in blob storage (the procedure to set this is described below)
{
    "apiVersion": "2019-03-01",
    "type": "Microsoft.ServiceFabric/clusters/applications",
    "name": "[concat(parameters('clusterName'), '/', parameters('applicationName'))]",
    "location": "[variables('clusterLocation')]",
},
{
    "apiVersion": "2019-03-01",
    "type": "Microsoft.ServiceFabric/clusters/applicationTypes",
    "name": "[concat(parameters('clusterName'), '/', parameters('applicationTypeName'))]",
    "location": "[variables('clusterLocation')]",
},
{
    "apiVersion": "2019-03-01",
    "type": "Microsoft.ServiceFabric/clusters/applicationTypes/versions",
    "name": "[concat(parameters('clusterName'), '/', parameters('applicationTypeName'), '/', parameters('applicationTypeVersion'))]",
    "location": "[variables('clusterLocation')]",
},
{
    "apiVersion": "2019-03-01",
    "type": "Microsoft.ServiceFabric/clusters/applications/services",
    "name": "[concat(parameters('clusterName'), '/', parameters('applicationName'), '/', parameters('serviceName'))]",
    "location": "[variables('clusterLocation')]"
}

部署应用程序Deploy the application

若要部署该应用程序,请运行 New-AzResourceGroupDeployment 部署到包含你的群集的资源组。To deploy the application, run the New-AzResourceGroupDeployment to deploy to the resource group which contains your cluster.

New-AzResourceGroupDeployment -ResourceGroupName "sf-cluster-rg" -TemplateParameterFile ".\UserApp.Parameters.json" -TemplateFile ".\UserApp.json" -Verbose

使用 Azure 资源管理器升级 Service Fabric 应用程序Upgrade Service Fabric application using Azure Resource Manager

已部署到 Service Fabric 群集的应用程序将出于以下原因而升级:Applications already deployed to a Service Fabric cluster will be upgraded for the following reasons:

  1. 已将一个新服务添加到应用程序。A new service is added to the application. 必须将服务定义添加到 service-manifest.xml 和 application-manifest.xml 文件中。A service definition must be added to service-manifest.xml and application-manifest.xml file. 然后,若要反映应用程序的新版本,需在 UserApp.parameters.json 中将应用程序类型版本从 1.0.0 更新为 1.0.1。Then to reflect new version of application, you need to update the application type version from 1.0.0 to 1.0.1 UserApp.parameters.json.

    "applicationTypeVersion": {
        "value": "1.0.1"
    },
    "serviceName2": {
        "value": "Voting~VotingData"
    },
    "serviceTypeName2": {
        "value": "VotingDataType"
    }
    
  2. 现有服务的新版本将添加到应用程序中。A new version of an existing service is added to the application. 这涉及到更改应用程序代码以及更新应用类型版本和名称。This involves application code changes and updates to app type version and name.

     "applicationTypeVersion": {
        "value": "1.0.1"
    },
    

删除应用程序资源Delete application resources

可通过以下步骤从群集中删除使用应用程序资源模型在 Azure 资源管理器中部署的应用程序Applications deployed using the application resource model in Azure Resource Manager can be deleted from cluster using below steps

  1. 使用 Get-AzResource 命令获取应用程序的资源 IDGet resource id for application using Get-AzResource command

    获取应用程序的资源 IDGet Resource ID for application

    Get-AzResource  -Name <String> | f1
    
  2. 使用 Remove-AzResource 删除应用程序资源Delete the application resources using Remove-AzResource

    使用应用程序资源的 ID 删除该资源Delete application resource using its Resource ID

    Remove-AzResource  -ResourceId <String> [-Force] [-ApiVersion <String>]
    

后续步骤Next steps

获取有关应用程序资源模型的信息:Get information about the application resource model:

另请参阅See Also