为 Service Fabric 群集打开端口Open ports for a Service Fabric cluster

使用 Azure Service Fabric 群集部署的负载均衡器将流量定向到在节点上运行的应用。The load balancer deployed with your Azure Service Fabric cluster directs traffic to your app running on a node. 如果将应用更改为使用另一端口,必须在 Azure 负载均衡器中公开该端口(或路由另一端口)。If you change your app to use a different port, you must expose that port (or route a different port) in the Azure Load Balancer.

将 Service Fabric 群集部署到 Azure 后,系统会自动创建负载均衡器。When you deployed your Service Fabric cluster to Azure, a load balancer was automatically created for you. 如果没有负载均衡器,请参阅配置面向 Internet 的负载均衡器If you do not have a load balancer, see Configure an Internet-facing load balancer.

备注

本文进行了更新,以便使用新的 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.

配置 Service FabricConfigure service fabric

Service Fabric 应用程序 ServiceManifest.xml 配置文件定义应用程序应使用的终结点 。Your Service Fabric application ServiceManifest.xml config file defines the endpoints your application expects to use. 更新配置文件以定义终结点后,必须更新负载均衡器,公开该(或其他)端口。After the config file has been updated to define an endpoint, the load balancer must be updated to expose that (or a different) port. 有关如何创建 Service Fabric 终结点的详细信息,请参阅设置终结点For more information on how to create the service fabric endpoint, see Setup an Endpoint.

创建负载均衡器规则Create a load balancer rule

负载均衡器规则可打开面向 Internet 的端口,并将流量转发至应用程序所用的内部节点的端口。A Load Balancer rule opens up an internet-facing port and forwards traffic to the internal node's port used by your application. 如果没有负载均衡器,请参阅配置面向 Internet 的负载均衡器If you do not have a load balancer, see Configure an Internet-facing load balancer.

要创建负载均衡器规则,需要收集以下信息:To create a Load Balancer rule, you need to collect the following information:

  • 负载均衡器名称。Load balancer name.
  • 负载均衡器和 Service Fabric 群集的资源组。Resource group of the load balancer and service fabric cluster.
  • 外部端口。External port.
  • 内部端口。Internal port.

Azure CLIAzure CLI

使用单个命令即可使用 Azure CLI 创建负载均衡器规则 。It only takes a single command to create a load balancer rule with the Azure CLI. 你只需知道用于创建新规则的负载均衡器和资源组的名称。You just need to know both the name of the load balancer and resource group to create a new rule.

备注

如果需要确定负载均衡器的名称,可使用此命令来快速获取所有负载均衡器和关联资源组的列表。If you need to determine the name of the load balancer, use this command to quickly get a list of all load balancers and the associated resource groups.

az network lb list --query "[].{ResourceGroup: resourceGroup, Name: name}"

az network lb rule create --backend-port 40000 --frontend-port 39999 --protocol Tcp --lb-name LB-svcfab3 -g svcfab_cli -n my-app-rule

Azure CLI 命令具有下表中所述的几个参数:The Azure CLI command has a few parameters that are described in the following table:

参数Parameter 说明Description
--backend-port Service Fabric 应用程序正在侦听的端口。The port the Service Fabric application is listening to.
--frontend-port 负载均衡器针对外部连接公开的端口。The port the load balancer exposes for external connections.
-lb-name 要更改的负载均衡器的名称。The name of the load balancer to change.
-g 同时包含负载均衡器与 Service Fabric 群集的资源组。The resource group that has both the load balancer and Service Fabric cluster.
-n 所需的规则名称。The desired name of the rule.

备注

有关如何使用 Azure CLI 创建负载均衡器的详细信息,请参阅使用 Azure CLI 创建负载均衡器For more information on how to create a load balancer with the Azure CLI, see Create a load balancer with the Azure CLI.

PowerShellPowerShell

PowerShell 比 Azure CLI 稍显复杂。PowerShell is a little more complicated than the Azure CLI. 请按照这些概念步骤操作,创建规则:Follow these conceptual steps to create a rule:

  1. 从 Azure 中获取负载均衡器。Get the load balancer from Azure.
  2. 创建规则。Create a rule.
  3. 将规则添加到负载均衡器。Add the rule to the load balancer.
  4. 更新负载均衡器。Update the load balancer.

备注

如果需要确定负载均衡器的名称,可使用此命令来快速获取所有负载均衡器和关联资源组的列表。If you need to determine the name of the load balancer, use this command to quickly get a list of all load balancers and associated resource groups.

Get-AzLoadBalancer | Select Name, ResourceGroupName

# Get the load balancer
$lb = Get-AzLoadBalancer -Name LB-svcfab3 -ResourceGroupName svcfab_cli

# Create the rule based on information from the load balancer.
$lbrule = New-AzLoadBalancerRuleConfig -Name my-app-rule7 -Protocol Tcp -FrontendPort 39990 -BackendPort 40009 `
                                            -FrontendIpConfiguration $lb.FrontendIpConfigurations[0] `
                                            -BackendAddressPool  $lb.BackendAddressPools[0] `
                                            -Probe $lb.Probes[0]

# Add the rule to the load balancer
$lb.LoadBalancingRules.Add($lbrule)

# Update the load balancer on Azure
$lb | Set-AzLoadBalancer

对于 New-AzLoadBalancerRuleConfig 命令,-FrontendPort 表示负载均衡器针对外部连接公开的端口,而 -BackendPort 表示 Service Fabric 应用正在侦听的端口。Regarding the New-AzLoadBalancerRuleConfig command, the -FrontendPort represents the port the load balancer exposes for external connections, and the -BackendPort represents the port the service fabric app is listening to.

备注

有关如何使用 PowerShell 创建负载均衡器的详细信息,请参阅使用 PowerShell 创建负载均衡器For more information on how to create a load balancer with PowerShell, see Create a load balancer with PowerShell.

后续步骤Next steps

详细了解 Service Fabric 中的网络Learn more about networking in Service Fabric.