基础结构即代码Infrastructure as code

在生产方案中,使用资源管理器模板创建 Azure Service Fabric 群集。In a production scenario, create Azure Service Fabric clusters using Resource Manager templates. 资源管理器模板可以更好地控制资源属性,并确保你具有一致的资源模型。Resource Manager templates provide greater control of resource properties and ensure that you have a consistent resource model.

GitHub 上的 Azure 示例中提供了适用于 Windows 和 Linux 的示例资源管理器模板。Sample Resource Manager templates are available for Windows and Linux in the Azure samples on GitHub. 这些模板可用作群集模板的起点。These templates can be used as a starting point for your cluster template. 下载 azuredeploy.jsonazuredeploy.parameters.json 并编辑它们以满足你的自定义要求。Download azuredeploy.json and azuredeploy.parameters.json and edit them to meet your custom requirements.

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

备注

必须修改从 GitHub 存储库“Azure-Samples”下载或引用的模板,使之适应 Azure 中国云环境。Templates you downloaded or referenced from the GitHub Repo "Azure-Samples" must be modified in order to fit in the Azure China Cloud Environment. 例如,替换某些终结点(将“blob.core.windows.net”替换为“blob.core.chinacloudapi.cn”,将“cloudapp.azure.com”替换为“chinacloudapp.cn”);必要时更改某些不受支持的 VM 映像、VM 大小、SKU 以及资源提供程序的 API 版本。For example, replace some endpoints -- "blob.core.windows.net" by "blob.core.chinacloudapi.cn", "cloudapp.azure.com" by "chinacloudapp.cn"; change some unsupported VM images, VM sizes, SKU and resource-provider's API Version when necessary.

备注

例如,当我们尝试在 Azure 中安装 5 节点安全 Windows Service Fabric 群集时。For example, When we try to install the 5 Node secure Windows Service Fabric Cluster in Azure. 在成功下载相应的模板文件后,我们应当替换以下配置来满足 Azure 中国环境:We should replace the following configurations to meet Azure China Environment after the corresponding template files have been downloaded successfully:

若要部署你在上面下载的 azuredeploy.jsonazuredeploy.parameters.json 模板,请使用以下 Azure CLI 命令:To deploy the azuredeploy.json and azuredeploy.parameters.json templates you downloaded above, use the following Azure CLI commands:

ResourceGroupName="sfclustergroup"
Location="chinanorth"

az group create --name $ResourceGroupName --location $Location 
az group deployment create --name $DeploymentName --resource-group $ResourceGroupName  --template-file azuredeploy.json --parameters @azuredeploy.parameters.json

使用 PowerShell 创建资源Creating a resource using Powershell

$ResourceGroupName="sfclustergroup"
$Location="chinanorth"
$Template="azuredeploy.json"
$Parameters="azuredeploy.parameters.json"

New-AzResourceGroup -Name $ResourceGroupName -Location $Location
New-AzResourceGroupDeployment -Name $DeploymentName -ResourceGroupName $ResourceGroupName -TemplateFile $Template -TemplateParameterFile $Parameters

Azure Service Fabric 资源Azure Service Fabric resources

可以通过 Azure 资源管理器,将应用程序和服务部署到 Service Fabric 群集。You can deploy applications and services onto your Service Fabric cluster via Azure Resource Manager. 有关详细信息,请参阅将应用程序和服务作为 Azure 资源管理器资源进行管理See Manage applications and services as Azure Resource Manager resources for details. 下面是要在资源管理器模板资源中包括的最佳做法 Service Fabric 应用程序特定资源。The following are best practice Service Fabric application specific resources to include in your Resource Manager template resources.

{
    "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",
    "name": "[concat(parameters('clusterName'), '/', parameters('applicationName'))]",
    "location": "[variables('clusterLocation')]",
},
{
    "apiVersion": "2019-03-01",
    "type": "Microsoft.ServiceFabric/clusters/applications/services",
    "name": "[concat(parameters('clusterName'), '/', parameters('applicationName'), '/', parameters('serviceName'))]",
    "location": "[variables('clusterLocation')]"
}

若要使用 Azure 资源管理器部署应用程序,必须首先创建一个 sfpkg Service Fabric 应用程序包。To deploy your application using Azure Resource Manager, you first must create a sfpkg Service Fabric Application package. 下面的 python 脚本是有关如何创建 sfpkg 的示例:The following python script is an example of how to create a sfpkg:

# Create SFPKG that needs to be uploaded to Azure Storage Blob Container
microservices_sfpkg = zipfile.ZipFile(
    self.microservices_app_package_name, 'w', zipfile.ZIP_DEFLATED)
package_length = len(self.microservices_app_package_path)

for root, dirs, files in os.walk(self.microservices_app_package_path):
    root_folder = root[package_length:]
    for file in files:
        microservices_sfpkg.write(os.path.join(
            root, file), os.path.join(root_folder, file))

microservices_sfpkg.close()

Azure 虚拟机操作系统自动升级配置Azure Virtual Machine Operating System Automatic Upgrade Configuration

升级虚拟机是用户启动的操作,建议使用虚拟机规模集操作系统自动升级进行 Azure Service Fabric 群集主机修补程序管理;修补业务流程应用程序是替代解决方案,适用于在 Azure 外部托管的情况。虽然 POA 可以在 Azure 中使用,但考虑到在 Azure 中托管 POA 的开销,通常会首选虚拟机操作系统自动升级而不是 POA。Upgrading your virtual machines is a user initiated operation, and it is recommended that you use Virtual Machine Scale Set Automatic Operating System upgrade for Azure Service Fabric clusters host patch management; Patch Orchestration Application is an alternative solution that is intended for when hosted outside of Azure, although POA can be used in Azure, with overhead of hosting POA in Azure being a common reason to prefer Virtual Machine Operating System Automatic Upgrade over POA. 下面是计算虚拟机规模集资源管理器模板属性,用于启用 OS 自动升级:The following are the Compute Virtual Machine Scale Set Resource Manager template properties to enable Auto OS upgrade:

"upgradePolicy": {
   "mode": "Automatic",
   "automaticOSUpgradePolicy": {
        "enableAutomaticOSUpgrade": true,
        "disableAutomaticRollback": false
    }
},

使用带 Service Fabric 的 OS 自动升级时,将推出新的 OS 映像(每次一个更新域),以维持 Service Fabric 中运行的服务的高可用性。When using Automatic OS Upgrades with Service Fabric, the new OS image is rolled out one Update Domain at a time to maintain high availability of the services running in Service Fabric. 若要利用 Service Fabric 中的自动 OS 升级,必须将群集配置为使用银级持久性层或更高层级。To utilize Automatic OS Upgrades in Service Fabric your cluster must be configured to use the Silver Durability Tier or higher.

确保将以下注册表项设置为 false,防止 Windows 主机启动不协调的更新:HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU。Ensure the following registry key is set to false to prevent your windows host machines from initiating uncoordinated updates: HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU.

下面是计算虚拟机规模集资源管理器模板属性,用于将 WindowsUpdate 注册表项设置为 false:The following are the Compute Virtual Machine Scale Set Resource Manager template properties to set the WindowsUpdate registry key to false:

"osProfile": {
        "computerNamePrefix": "{vmss-name}",
        "adminUsername": "{your-username}",
        "secrets": [],
        "windowsConfiguration": {
          "provisionVMAgent": true,
          "enableAutomaticUpdates": false
        }
      },

Azure Service Fabric 群集升级配置Azure Service Fabric Cluster Upgrade Configuration

下面是 Service Fabric 群集资源管理模板属性,用于启用自动升级:The following is the Service Fabric cluster Resource Manager template property to enable automatic upgrade:

"upgradeMode": "Automatic",

若要手动升级群集,请将 cab/deb 发行版下载到群集虚拟机,然后调用以下 PowerShell:To manually upgrade your cluster, download the cab/deb distribution to a cluster virtual machine, and then invoke the following PowerShell:

Copy-ServiceFabricClusterPackage -Code -CodePackagePath <"local_VM_path_to_msi"> -CodePackagePathInImageStore ServiceFabric.msi -ImageStoreConnectionString "fabric:ImageStore"
Register-ServiceFabricClusterPackage -Code -CodePackagePath "ServiceFabric.msi"
Start-ServiceFabricClusterUpgrade -Code -CodePackageVersion <"msi_code_version">

后续步骤Next steps