在 Windows 上将 Service Fabric 的 Reliable Services 和 Reliable Actors 打包为容器

Service Fabric 支持容器化 Service Fabric 微服务(Reliable Services 和基于 Reliable Actor 的服务)。 有关详细信息,请参阅 服务结构容器

本文档提供了有关使服务在 Windows 容器中运行的指导。

注释

目前此功能仅适用于 Windows。 若要运行容器,群集必须在安装了 Docker 的 Windows Server 上运行。 请参阅 安装 Mirantis 运行时

容器化 Service Fabric 应用程序的步骤

  1. 在 Visual Studio 中打开 Service Fabric 应用程序。

  2. 将类 SFBinaryLoader.cs 添加到项目。 此类中的代码是一种帮助程序,用于在容器中运行时正确加载应用程序中的 Service Fabric 运行时二进制文件。

  3. 对于要容器化的每个代码包,请在程序入口点初始化加载程序。 将以下代码片段中显示的静态构造函数添加到程序入口点文件中。

    namespace MyApplication
    {
       internal static class Program
       {
           static Program()
           {
               SFBinaryLoader.Initialize();
           }
    
           /// <summary>
           /// This is the entry point of the service host process.
           /// </summary>
           private static void Main()
           {
    
  4. 生成和 打包 项目。 若要生成和创建包,请在解决方案资源管理器中右键单击应用程序项目,然后选择 “包 ”命令。

  5. 对于需要容器化的每个代码包,请运行 PowerShell 脚本 CreateDockerPackage.ps1。 用法如下所示:

    完整 .NET

      $codePackagePath = 'Path to the code package to containerize.'
      $dockerPackageOutputDirectoryPath = 'Output path for the generated docker folder.'
      $applicationExeName = 'Name of the Code package executable.'
      CreateDockerPackage.ps1 -CodePackageDirectoryPath $codePackagePath -DockerPackageOutputDirectoryPath $dockerPackageOutputDirectoryPath -ApplicationExeName $applicationExeName
    

    .NET 核心

      $codePackagePath = 'Path to the code package to containerize.'
      $dockerPackageOutputDirectoryPath = 'Output path for the generated docker folder.'
      $dotnetCoreDllName = 'Name of the Code package dotnet Core Dll.'
      CreateDockerPackage.ps1 -CodePackageDirectoryPath $codePackagePath -DockerPackageOutputDirectoryPath $dockerPackageOutputDirectoryPath -DotnetCoreDllName $dotnetCoreDllName
    

    该脚本在$dockerPackageOutputDirectoryPath创建包含 Docker 项目的文件夹。 根据需求,将生成的 Dockerfile 修改为 expose 任何端口、运行安装脚本等。

  6. 接下来,需要 生成 Docker 容器包并将其 推送 到存储库。

  7. 修改 ApplicationManifest.xml 和 ServiceManifest.xml 以添加容器映像、存储库信息、注册表身份验证和端口到主机映射。 有关修改清单的信息,请参阅 “创建 Azure Service Fabric 容器应用程序”。 服务清单中的代码包定义需要替换为相应的容器映像。 请确保将 EntryPoint 更改为 ContainerHost 类型。

    <!-- Code package is your service executable. -->
    
    <CodePackage Name="Code" Version="1.0.0">
    <EntryPoint>
     <!-- Follow this link for more information about deploying Windows containers to Service Fabric: https://aka.ms/sfguestcontainers -->
    
     <ContainerHost>
       <ImageName>myregistry.azurecr.cn/samples/helloworldapp</ImageName>
     </ContainerHost>
    </EntryPoint>
    <!-- Pass environment variables to your container: -->
    
    </CodePackage>
    
  8. 为您的复制器和服务端点添加端口到主机映射。 由于 Service Fabric 在运行时分配了这两个端口,因此 ContainerPort 设置为零,以使用分配的端口进行映射。

    <Policies>
    <ContainerHostPolicies CodePackageRef="Code">
     <PortBinding ContainerPort="0" EndpointRef="ServiceEndpoint"/>
     <PortBinding ContainerPort="0" EndpointRef="ReplicatorEndpoint"/>
    </ContainerHostPolicies>
    </Policies>
    
  9. 有关配置容器隔离模式,请参阅 “配置隔离模式”。 Windows 支持容器的两种隔离模式:进程和 Hyper-V。 以下代码片段演示如何在应用程序清单文件中指定隔离模式。

    <Policies>
    <ContainerHostPolicies CodePackageRef="Code" Isolation="process">
    ...
    </ContainerHostPolicies>
    </Policies>
    
    <Policies>
    <ContainerHostPolicies CodePackageRef="Code" Isolation="hyperv">
    ...
    </ContainerHostPolicies>
    </Policies>
    
  10. (可选)为服务之间的手动通信配置证书。 默认情况下,需要主群集证书才能在不同的容器化服务之间进行手动通信。 此证书未安装在容器中,而是安装在节点本身上。 解决此问题的一种方法是将 .pfx 文件与 PowerShell 脚本一起包含在容器中,以在容器中安装证书。 另一种方法是配置群集,以便不需要证书。 详细了解 Service Fabric 群集安全方案

  11. (可选)将 Ktl 记录器配置为使用用户模式。 有状态服务在容器中运行时可能会遇到错误,因为文件系统存在差异。 此错误将显示在 Service Fabric Explorer 中,如下所示:

    System.Runtime.InteropServices.COMException (-2147024463)
    A device which does not exist was specified. (0x800701B1)
    ...
    

    通过将群集清单更新为在 TransactionalReplicator 节下将 UseUserModeKtlLogger 设置为 true,可以修复此错误。 遵循在这篇文章中所述的步骤。

      {
       "name": "TransactionalReplicator",
       "parameters": [
         {
           "name": "UseUserModeKtlLogger",
           "value": "true"
         }
       ]
      }
    
  12. 若要测试此应用程序,需要将其部署到运行版本 5.7 或更高版本的群集。 对于运行时版本 6.1 或更高版本,需要编辑和更新群集设置才能启用此预览功能。 按照 本文 中的步骤添加下一步所示的设置。

      {
        "name": "Hosting",
        "parameters": [
          {
            "name": "FabricContainerAppsEnabled",
            "value": "true"
          }
        ]
      }
    
  13. 接下来,将编辑的应用程序包部署到此群集。

现在,应该有一个运行群集的容器化 Service Fabric 应用程序。

后续步骤