Observação
O acesso a essa página exige autorização. Você pode tentar entrar ou alterar diretórios.
O acesso a essa página exige autorização. Você pode tentar alterar os diretórios.
Service Fabric 支持容器化 Service Fabric 微服务(Reliable Services 和基于 Reliable Actor 的服务)。 有关详细信息,请参阅 服务结构容器。
本文档提供了有关使服务在 Windows 容器中运行的指导。
注释
目前此功能仅适用于 Windows。 若要运行容器,群集必须在安装了 Docker 的 Windows Server 上运行。 请参阅 安装 Mirantis 运行时。
容器化 Service Fabric 应用程序的步骤
在 Visual Studio 中打开 Service Fabric 应用程序。
将类 SFBinaryLoader.cs 添加到项目。 此类中的代码是一种帮助程序,用于在容器中运行时正确加载应用程序中的 Service Fabric 运行时二进制文件。
对于要容器化的每个代码包,请在程序入口点初始化加载程序。 将新的 Main 函数添加到Program.cs,并将旧的 Main 重命名为 RealMain,如以下示例所示。 这需要延迟加载任何 Service Fabric 依赖项,直到初始化 SFBinaryLoader。
namespace MyApplication { internal static class Program { private static void Main() { SFBinaryLoader.Initialize(); RealMain(); } /// <summary> /// This is the entry point of the service host process. /// </summary> private static void RealMain() {生成和 打包 项目。 若要生成和创建包,请在解决方案资源管理器中右键单击应用程序项目,然后选择 “包 ”命令。
对于需要容器化的每个代码包,请运行 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任何端口、运行安装脚本等。修改 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>为您的复制器和服务端点添加端口到主机映射。 由于 Service Fabric 在运行时分配了这两个端口,因此 ContainerPort 设置为零,以使用分配的端口进行映射。
<Policies> <ContainerHostPolicies CodePackageRef="Code"> <PortBinding ContainerPort="0" EndpointRef="ServiceEndpoint"/> <PortBinding ContainerPort="0" EndpointRef="ReplicatorEndpoint"/> </ContainerHostPolicies> </Policies>有关配置容器隔离模式,请参阅 “配置隔离模式”。 Windows 支持容器的两种隔离模式:进程和 Hyper-V。 以下代码片段演示如何在应用程序清单文件中指定隔离模式。
<Policies> <ContainerHostPolicies CodePackageRef="Code" Isolation="process"> ... </ContainerHostPolicies> </Policies><Policies> <ContainerHostPolicies CodePackageRef="Code" Isolation="hyperv"> ... </ContainerHostPolicies> </Policies>(可选)为服务之间的手动通信配置证书。 默认情况下,需要主群集证书才能在不同的容器化服务之间进行手动通信。 此证书未安装在容器中,而是安装在节点本身上。 解决此问题的一种方法是将 .pfx 文件与 PowerShell 脚本一起包含在容器中,以在容器中安装证书。 另一种方法是配置群集,以便不需要证书。 详细了解 Service Fabric 群集安全方案。
(可选)将 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" } ] }若要测试此应用程序,需要将其部署到运行版本 5.7 或更高版本的群集。 对于运行时版本 6.1 或更高版本,需要编辑和更新群集设置才能启用此预览功能。 按照 本文 中的步骤添加下一步所示的设置。
{ "name": "Hosting", "parameters": [ { "name": "FabricContainerAppsEnabled", "value": "true" } ] }接下来,将编辑的应用程序包部署到此群集。
注释
Service Fabric 群集是设计中的单租户,托管的应用程序被视为 受信任。 如果考虑托管 不受信任的容器应用程序,请考虑将它们部署为 来宾容器 ,并查看 Service Fabric 群集中托管不受信任的应用程序。
现在,应该有一个运行群集的容器化 Service Fabric 应用程序。
后续步骤
- 详细了解如何运行 Service Fabric 上的容器。
- 了解 Service Fabric 应用程序生命周期。