Azure Service Fabric 中的 DNS 服务DNS Service in Azure Service Fabric

DNS 服务是可选的系统服务,可以在群集中启用,用于发现使用 DNS 协议的其他服务。The DNS Service is an optional system service that you can enable in your cluster to discover other services using the DNS protocol.

许多服务(特别是容器化服务)可以通过现存的 URL 来寻址。Many services, especially containerized services, are addressable through a pre-existing URL. 能够使用标准 DNS 协议(而不是 Service Fabric 命名服务协议)解析这些名称是很有必要的。Being able to resolve these services using the standard DNS protocol, rather than the Service Fabric Naming Service protocol, is desirable. 借助 DNS 服务,可将 DNS 名称映射到服务名称,进而解析终结点 IP 地址。The DNS service enables you to map DNS names to a service name and hence resolve endpoint IP addresses. 此类功能可在不同的平台之间保持容器化服务的可移植性,并更方便地利用“直接迁移”方案,因为它可以让你使用现有的服务 URL,而无需重新编写代码来利用命名服务。Such functionality maintains the portability of containerized services across different platforms and can make "lift and shift" scenarios easier, by letting you use existing service URLs rather than having to rewrite code to leverage the Naming Service.

DNS 服务将 DNS 名称映射到服务名称,命名服务将服务名称进行解析并将其发送回服务终结点。The DNS service maps DNS names to service names, which in turn are resolved by the Naming Service to return the service endpoint. 在创建时提供服务的 DNS 名称。The DNS name for the service is provided at the time of creation. 下图显示了如何对无状态服务运行 DNS 服务。The following diagram shows how the DNS service works for stateless services.

服务终结点

从 Service Fabric 版本 6.3 开始,Service Fabric DNS 协议经过扩展,现在包含用于寻址已分区的有状态服务的方案。Beginning with Service Fabric version 6.3, the Service Fabric DNS protocol has been extended to include a scheme for addressing partitioned stateful services. 使用这些扩展可以通过有状态服务 DNS 名称和分区名称的组合来解析特定的分区 IP 地址。These extensions make it possible to resolve specific partition IP addresses using a combination of stateful service DNS name and the partition name. 支持所有三种分区方案:All three partitioning schemes are supported:

  • 命名分区Named partitioning
  • 按范围分区Ranged partitioning
  • 单一实例分区Singleton partitioning

下图显示了如何分区的有状态服务运行 DNS 服务。The following diagram shows how the DNS service works for partitioned stateful services.

有状态服务终结点

DNS 服务不支持动态端口。Dynamic ports are not supported by the DNS service. 若要解析动态端口上公开的服务,请使用反向代理服务To resolve services exposed on dynamic ports, use the reverse proxy service.

启用 DNS 服务Enabling the DNS service

备注

在 Linux 上尚不支持用于 Service Fabric 服务的 DNS 服务。DNS service for Service Fabric services is not yet supported on Linux.

使用门户创建群集时,默认情况下,在“群集配置”菜单的“包括 DNS 服务”复选框中启用 DNS 服务 :When you create a cluster using the portal, the DNS service is enabled by default in the Include DNS service check box on the Cluster configuration menu:

通过门户启用 DNS 服务

如果不使用门户创建群集或者要更新现有群集,则需要在模板中启用 DNS 服务:If you're not using the portal to create your cluster or if you're updating an existing cluster, you'll need to enable the DNS service in a template:

  • 若要部署新的群集,可以使用示例模板或创建自己的资源管理器模板。To deploy a new cluster, you can either use the sample templates or create your own Resource Manager template.
  • 若要更新现有群集,可以导航到门户的群集资源组并单击“自动化脚本”,使用反映群集和组中其他资源当前状态的模板 。To update an existing cluster, you can navigate to the cluster's resource group on the portal and click Automation Script to work with a template that reflects the current state of the cluster and other resources in the group. 若要了解详细信息,请参阅从资源组导出模板To learn more, see Export the template from resource group.

有了模板后,可以通过以下步骤启用 DNS 服务:After you have a template, you can enable the DNS service with the following steps:

  1. 检查 Microsoft.ServiceFabric/clusters 资源的 apiversion 是否设置为 2017-07-01-preview 或更高,如果不是,请按以下示例所示进行更新:Check that the apiversion is set to 2017-07-01-preview or later for the Microsoft.ServiceFabric/clusters resource, and, if not, update it as shown in the following example:

    {
        "apiVersion": "2017-07-01-preview",
        "type": "Microsoft.ServiceFabric/clusters",
        "name": "[parameters('clusterName')]",
        "location": "[parameters('clusterLocation')]",
        ...
    }
    
  2. 现在,通过以下方式之一启用 DNS 服务:Now enable the DNS service in one of the following ways:

    • 若要启用采用默认设置的 DNS 服务,请将其添加到 properties 节中的 addonFeatures 节,如以下示例所示:To enable the DNS service with default settings, add it to the addonFeatures section inside the properties section as shown in the following example:

      "properties": {
        ...
      
        "addonFeatures": [
          "DnsService"
        ],
        ...
      }
      
    • 若要启用采用非默认设置的服务,请将 DnsService 节添加到 properties 节中的 fabricSettings 节。To enable the service with other than default settings, add a DnsService section to the fabricSettings section inside the properties section. 在这种情况下,不需要将 DnsService 添加到 addonFeaturesIn this case, you don't need to add the DnsService to addonFeatures. 若要详细了解可为 DNS 服务设置的属性,请参阅 DNS 服务设置To learn more about the properties that can be set for the DNS Service, see DNS Service settings.

         "properties": {
           ...  
           "fabricSettings": [
             ...
             {
               "name": "DnsService",
               "parameters": [
                 {
                   "name": "IsEnabled",
                   "value": "true"
                 },
                 {
                   "name": "PartitionSuffix",
                   "value": "--"
                 },
                 {
                   "name": "PartitionPrefix",
                   "value": "--"
                 }
               ]
             },
             ...
            ]
          }
      
  3. 通过这些更改更新群集模板后,请应用更改并等待升级完成。Once you have updated the cluster template with your changes, apply them and let the upgrade complete. 完成升级后,DNS 系统服务将开始在群集中运行。When the upgrade completes, the DNS system service starts running in your cluster. 服务名称是 fabric:/System/DnsService,可以在 Service Fabric Explorer 的“系统”服务部分下找到它 。The service name is fabric:/System/DnsService, and you can find it under the System service section in Service Fabric explorer.

备注

将 DNS 从禁用升级到启用时,Service Fabric Explorer 可能未反映新状态。When upgrading DNS from disabled to enabled, Service Fabric Explorer may not reflect the new state. 若要解决问题,请重启节点,方法是:在 Azure 资源管理器模板中修改 UpgradePolicy。To solve, restart the nodes by modifying the UpgradePolicy in your Azure Resource Manager template. 有关详细信息,请参阅 Service Fabric 模板参考See the Service Fabric Template Reference for more.

备注

在本地计算机上进行开发时,如果启用 DNS 服务,则会替代某些 DNS 设置。Enabling DNS service when developing on a local machine will override some DNS settings. 如果在连接到 Internet 时遇到问题,请检查 DNS 设置。If you experience issues connecting to the internet, check your DNS settings.

设置服务的 DNS 名称Setting the DNS name for your service

可以在 ApplicationManifest.xml 文件中或者通过 PowerShell 命令,以声明方式为默认服务设置 DNS 名称。You can set a DNS name for your services either declaratively for default services in the ApplicationManifest.xml file or through PowerShell commands.

服务的 DNS 名称可在整个群集中解析,因此,请务必确保 DNS 名称在整个群集中的唯一性。The DNS name for your service is resolvable throughout the cluster so it is important to ensure the uniqueness of the DNS name across the cluster.

强烈建议使用 <ServiceDnsName>.<AppInstanceName> 命名方案;例如 service1.application1It is highly recommended that you use a naming scheme of <ServiceDnsName>.<AppInstanceName>; for example, service1.application1. 如果使用 Docker Compose 部署应用程序,服务会自动分配使用此命名方案的 DNS 名称。If an application is deployed using Docker compose, services are automatically assigned DNS names using this naming scheme.

在 ApplicationManifest.xml 中为默认服务设置 DNS 名称Setting the DNS name for a default service in the ApplicationManifest.xml

在 Visual Studio 中或所选的编辑器中打开项目,并打开 ApplicationManifest.xml 文件。Open your project in Visual Studio, or your favorite editor, and open the ApplicationManifest.xml file. 转到默认服务部分,为每个服务添加 ServiceDnsName 属性。Go to the default services section, and for each service add the ServiceDnsName attribute. 以下示例说明如何将服务的 DNS 名称设置为 service1.application1The following example shows how to set the DNS name of the service to service1.application1

<Service Name="Stateless1" ServiceDnsName="service1.application1">
  <StatelessService ServiceTypeName="Stateless1Type" InstanceCount="[Stateless1_InstanceCount]">
    <SingletonPartition />
  </StatelessService>
</Service>

部署应用程序后,Service Fabric Explorer 中的服务实例会显示此实例的 DNS 名称,如下图所示:Once the application is deployed, the service instance in the Service Fabric explorer shows the DNS name for this instance, as shown in the following figure:

服务终结点

以下示例将有状态服务的 DNS 名称设置为 statefulsvc.appThe following example sets the DNS name for a stateful service to statefulsvc.app. 该服务使用命名分区方案。The service uses a named partitioning scheme. 请注意分区名称均为小写。Notice that the partition names are lower-case. 这是在 DNS 查询中用作目标的分区的一项要求;有关详细信息,请参阅针对有状态服务分区发出 DNS 查询This is a requirement for partitions that will be targeted in DNS queries; for more information, see Making DNS queries on a stateful service partition.

<Service Name="Stateful1" ServiceDnsName="statefulsvc.app" />
  <StatefulService ServiceTypeName="ProcessingType" TargetReplicaSetSize="2" MinReplicaSetSize="2">
    <NamedPartition>
     <Partition Name="partition1" />
     <Partition Name="partition2" />
    </NamedPartition>
  </StatefulService>
</Service>

使用 Powershell 设置服务的 DNS 名称Setting the DNS name for a service using Powershell

创建服务时,可以使用 New-ServiceFabricService Powershell 命令设置服务的 DNS 名称。You can set the DNS name for a service when creating it using the New-ServiceFabricService Powershell command. 以下示例创建一个 DNS 名称为 service1.application1 的新的无状态服务The following example creates a new stateless service with the DNS name service1.application1

New-ServiceFabricService `
    -Stateless `
    -PartitionSchemeSingleton `
    -ApplicationName `fabric:/application1 `
    -ServiceName fabric:/application1/service1 `
    -ServiceTypeName Service1Type `
    -InstanceCount 1 `
    -ServiceDnsName service1.application1

[预览版] 针对有状态服务分区发出 DNS 查询[Preview] Making DNS queries on a stateful service partition

从 Service Fabric 版本 6.3 开始,Service Fabric DNS 服务支持针对服务分区的查询。Beginning with Service Fabric version 6.3, the Service Fabric DNS service supports queries for service partitions.

对于 DNS 查询中使用的分区,适用以下命名限制:For partitions that will be used in DNS queries, the following naming restrictions apply:

  • 分区名称应符合 DNS 规范。Partition names should be DNS-compliant.
  • 不应使用多标签分区名称(名称中包括句点“.”)。Multi-label partition names (that include dot, '.', in the name) should not be used.
  • 分区名称应为小写。Partition names should be lower-case.

针对分区的 DNS 查询的格式如下:DNS queries that target a partition are formatted as follows:

<First-Label-Of-Partitioned-Service-DNSName><PartitionPrefix><Target-Partition-Name>< PartitionSuffix>.<Remaining- Partitioned-Service-DNSName>

其中:Where:

  • First-Label-Of-Partitioned-Service-DNSName 是服务 DNS 名称的第一个部分。First-Label-Of-Partitioned-Service-DNSName is the first part of your service DNS name.
  • PartitionPrefix 是可以在群集清单的 DnsService 节中设置的,或者通过群集资源管理器模板设置的值。PartitionPrefix is a value that can be set in the DnsService section of the cluster manifest or through the cluster's Resource Manager template. 默认值为“--”。The default value is "--". 有关详细信息,请参阅 DNS 服务设置To learn more, see DNS Service settings.
  • Target-Partition-Name 是分区的名称。Target-Partition-Name is the name of the partition.
  • PartitionSuffix 是可以在群集清单的 DnsService 节中设置的,或者通过群集资源管理器模板设置的值。PartitionSuffix is a value that can be set in the DnsService section of the cluster manifest or through the cluster's Resource Manager template. 默认值为空字符串。The default value is empty string. 有关详细信息,请参阅 DNS 服务设置To learn more, see DNS Service settings.
  • Remaining-Partitioned-Service-DNSName 是服务 DNS 名称的剩余部分。Remaining-Partitioned-Service-DNSName is the remaining part of your service DNS name.

以下示例显示了针对某个群集(该群集对 PartitionPrefixPartitionSuffix 采用默认设置)上运行的分区服务发出的 DNS 查询:The following examples show DNS queries for partitioned services running on a cluster that has default settings for PartitionPrefix and PartitionSuffix:

  • 若要解析具有 DNS 名称 backendrangedschemesvc.application(使用按范围分区方案)的服务的分区“0”,请使用 backendrangedschemesvc-0.applicationTo resolve partition "0" of a service with DNS name backendrangedschemesvc.application that uses a ranged partitioning scheme, use backendrangedschemesvc-0.application.
  • 若要解析具有 DNS 名称 backendnamedschemesvc.application(使用命名分区方案)的服务的分区“first”,请使用 backendnamedschemesvc-first.applicationTo resolve partition "first" of a service with DNS name backendnamedschemesvc.application that uses a named partitioning scheme, use backendnamedschemesvc-first.application.

DNS 服务将返回该分区的主要副本的 IP 地址。The DNS service returns the IP address of the primary replica of the partition. 如果未指定分区,服务将返回随机选择的分区的主要副本的 IP 地址。If no partition is specified, the service returns the IP address of the primary replica of a randomly selected partition.

在服务中使用 DNSUsing DNS in your services

如果部署多个服务,可以使用 DNS 名称找到用于通信的其他服务的终结点。If you deploy more than one service, you can find the endpoints of other services to communicate with by using a DNS name. DNS 服务适用于无状态服务,在 Service Fabric 版本 6.3 和更高版本中,它也适用于有状态服务。The DNS service works for stateless services, and, in Service Fabric version 6.3 and later, for stateful services. 对于运行低于 Service Fabric 6.3 版本的 有状态服务,可以使用 HTTP 调用的内置反向代理服务调用特定的服务分区。For stateful services running on versions of Service Fabric prior to 6.3, you can use the built-in reverse proxy service for http calls to call a particular service partition.

DNS 服务不支持动态端口。Dynamic ports are not supported by the DNS service. 可以通过反向代理服务解析使用动态端口的服务。You can use the reverse proxy service to resolve services that use dynamic ports.

以下代码演示如何通过 DNS 调用无状态服务。The following code shows how to call a stateless service through DNS. 它只是一个普通的 http 调用,其中提供了 DNS 名称、端口和任一可选路径作为 URL 的一部分。It is simply a regular http call where you provide the DNS name, the port, and any optional path as part of the URL.

public class ValuesController : Controller
{
    // GET api
    [HttpGet]
    public async Task<string> Get()
    {
        string result = "";
        try
        {
            Uri uri = new Uri("http://service1.application1:8080/api/values");
            HttpClient client = new HttpClient();
            var response = await client.GetAsync(uri);
            result = await response.Content.ReadAsStringAsync();

        }
        catch (Exception e)
        {
            Console.Write(e.Message);
        }

        return result;
    }
}

以下代码演示如何调用有状态服务的特定分区。The following code shows a call on a specific partition of a stateful service. 在本例中,DNS 名称包含分区名称 (partition1)。In this case, the DNS name contains the partition name (partition1). 该调用假设群集对PartitionPrefixPartitionSuffix 使用默认值。The call assumes a cluster with default values for PartitionPrefix and PartitionSuffix.

public class ValuesController : Controller
{
    // GET api
    [HttpGet]
    public async Task<string> Get()
    {
        string result = "";
        try
        {
            Uri uri = new Uri("http://service2-partition1.application1:8080/api/values");
            HttpClient client = new HttpClient();
            var response = await client.GetAsync(uri);
            result = await response.Content.ReadAsStringAsync();

        }
        catch (Exception e)
        {
            Console.Write(e.Message);
        }

        return result;
    }
}

已知问题Known Issues

  • 对于 Service Fabric 版本 6.3 及更高版本,对于 DNS 名称中包含连字符的服务名称,DNS 查找存在问题。For Service Fabric versions 6.3 and higher, there is a problem with DNS lookups for service names containing a hyphen in the DNS name. 有关此问题的详细信息,请跟踪以下 GitHub 问题For more information on this issue, please track the following GitHub Issue. 此问题的修补程序将在接下来的 6.3 更新中提供。A fix for this is coming in the next 6.3 update.

  • 在 Linux 上尚不支持用于 Service Fabric 服务的 DNS 服务。DNS service for Service Fabric services is not yet supported on Linux. Linux 上的容器支持 DNS 服务。DNS service is supported for containers on Linux. 使用 Fabric 客户端/ServicePartitionResolver 进行手动解析是另一种选择。Manual resolution using Fabric Client/ServicePartitionResolver is the available alternative.

后续步骤Next steps

通过连接服务并与服务进行通信,了解有关群集内服务通信的详细信息Learn more about service communication within the cluster with connect and communicate with services