配置一个或多个 Always On 可用性组侦听器 - Resource ManagerConfigure one or more Always On availability group listeners - Resource Manager

适用于:是Azure VM 上的 SQL ServerAPPLIES TO: yesSQL Server on Azure VM

本文档演示如何使用 PowerShell 执行以下任务之一:This document shows you how to use PowerShell to do one of the following tasks:

  • 创建负载均衡器create a load balancer
  • 将 IP 地址添加到 SQL Server 可用性组的现有负载均衡器。add IP addresses to an existing load balancer for SQL Server availability groups.

可用性组侦听器是客户端为了访问数据库而连接的虚拟网络名称。An availability group listener is a virtual network name that clients connect to for database access. 在 Azure 虚拟机上,负载均衡器持有侦听程序的 IP 地址。On Azure Virtual Machines, a load balancer holds the IP address for the listener. 负载均衡器将流量路由到侦听探测端口的 SQL Server 实例。The load balancer routes traffic to the instance of SQL Server that is listening on the probe port. 通常情况下,可用性组使用内部负载均衡器。Usually, an availability group uses an internal load balancer. Azure 内部负载均衡器可以托管一个或多个 IP 地址。An Azure internal load balancer can host one or many IP addresses. 每个 IP 地址使用特定的探测端口。Each IP address uses a specific probe port.

将多个 IP 地址分配到内部负载均衡器是 Azure 的一项新增功能,只能在资源管理器模型中使用。The ability to assign multiple IP addresses to an internal load balancer is new to Azure and is only available in the Resource Manager model. 若要完成此任务,需要在资源管理器模型中的 Azure 虚拟机上部署 SQL Server 可用性组。To complete this task, you need to have a SQL Server availability group deployed on Azure Virtual Machines in the Resource Manager model. 这两个 SQL Server 虚拟机必须属于同一个可用性集。Both SQL Server virtual machines must belong to the same availability set. 可以手动配置 Always On 可用性组You can manually configure an Always On availability group.

若要完成本文中的步骤,需已配置可用性组。To complete the steps in this article, your availability groups need to be already configured.

相关主题包括:Related topics include:

备注

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

启动 PowerShell 会话Start your PowerShell session

运行 Connect-Az Account cmdlet,然后便会看到可输入凭据的登录屏幕。Run the Connect-Az Account cmdlet and you will be presented with a sign-in screen to enter your credentials. 使用与登录 Azure 门户相同的凭据。Use the same credentials that you use to sign in to the Azure portal.

Connect-AzAccount -Environment AzureChinaCloud

如果有多个订阅,请使用 Set-AzContext cmdlet 选择 PowerShell 会话应使用的订阅。If you have multiple subscriptions use the Set-AzContext cmdlet to select which subscription your PowerShell session should use. 若要查看当前 PowerShell 会话正在使用哪个订阅,请运行 Get-AzContextTo see what subscription the current PowerShell session is using, run Get-AzContext. 若要查看所有订阅,请运行 Get-AzSubscriptionTo see all your subscriptions, run Get-AzSubscription.

Set-AzContext -SubscriptionId '4cac86b0-1e56-bbbb-aaaa-000000000000'

验证 PowerShell 版本Verify PowerShell version

本文中的示例是使用 Azure PowerShell 模块版本 5.4.1 测试的。The examples in this article are tested using Azure PowerShell module version 5.4.1.

请验证你的 PowerShell 模块是否为 5.4.1 或更高版本。Verify that your PowerShell module is 5.4.1 or later.

请参阅安装 Azure PowerShell 模块See Install the Azure PowerShell module.

配置 Windows 防火墙Configure the Windows Firewall

配置 Windows 防火墙以允许 SQL Server 访问。Configure the Windows Firewall to allow SQL Server access. 防火墙规则允许到端口(由 SQL Server 实例使用)的 TCP 连接,并且允许侦听器探测。The firewall rules allow TCP connections to the ports use by the SQL Server instance, and the listener probe. 有关详细的说明,请参阅为数据库引擎访问配置 Windows 防火墙For detailed instructions, see Configure a Windows Firewall for Database Engine Access. 为 SQL Server 端口和探测端口创建入站规则。Create an inbound rule for the SQL Server port and for the probe port.

如果要限制 Azure 网络安全组的访问权限,请确保“允许”规则包含后端 SQL Server VM IP 地址、可用性组侦听器的负载均衡器浮动 IP 地址、群集核心 IP 地址(如适用)。If you are restricting access with an Azure Network Security Group, ensure that the allow rules include the backend SQL Server VM IP addresses, and the load balancer floating IP addresses for the AG listener and the cluster core IP address, if applicable.

确定所需的负载均衡器 SKUDetermine the load balancer SKU required

Azure 负载均衡器以两种 SKU 提供:“基本”和“标准”。Azure load balancer is available in two SKUs: Basic & Standard. 建议使用标准负载均衡器。The standard load balancer is recommended. 如果虚拟机在可用性集中,则可以使用基本负载均衡器。If the virtual machines are in an availability set, basic load balancer is permitted. 标准负载均衡器要求所有 VM IP 地址都使用标准 IP 地址。Standard load balancer requires that all VM IP addresses use standard IP addresses.

可用性组的当前 Microsoft 模板使用具有基本 IP 地址的基本负载均衡器。The current Microsoft template for an availability group uses a basic load balancer with basic IP addresses.

备注

如果将标准负载均衡器和 Azure 存储用于云见证,则需要配置服务终结点You will need to configure a service endpoint if you use a standard load balancer and Azure Storage for the cloud witness.

本文中的示例指定了一个标准负载均衡器。The examples in this article specify a standard load balancer. 在示例中,脚本包括了 -sku StandardIn the examples, the script includes -sku Standard.

$ILB= New-AzLoadBalancer -Location $Location -Name $ILBName -ResourceGroupName $ResourceGroupName -FrontendIpConfiguration $FEConfig -BackendAddressPool $BEConfig -LoadBalancingRule $ILBRule -Probe $SQLHealthProbe -sku Standard

若要创建基本负载均衡器,请从创建负载均衡器的行中删除 -sku StandardTo create a basic load balancer, remove -sku Standard from the line that creates the load balancer. 例如:For example:

$ILB= New-AzLoadBalancer -Location $Location -Name $ILBName -ResourceGroupName $ResourceGroupName -FrontendIpConfiguration $FEConfig -BackendAddressPool $BEConfig -LoadBalancingRule $ILBRule -Probe $SQLHealthProbe

示例脚本:使用 PowerShell 创建内部负载均衡器Example Script: Create an internal load balancer with PowerShell

以下 PowerShell 脚本创建内部负载均衡器、配置负载均衡规则,并设置负载均衡器的 IP 地址。The following PowerShell script creates an internal load balancer, configures the load-balancing rules, and sets an IP address for the load balancer. 要运行该脚本,请打开 Windows PowerShell ISE,然后将脚本粘贴到“脚本”窗格中。To run the script, open Windows PowerShell ISE, and then paste the script in the Script pane. 使用 Connect-AzAccount -Environment AzureChinaCloud 登录到 PowerShell。Use Connect-AzAccount -Environment AzureChinaCloud to log in to PowerShell. 如果有多个 Azure 订阅,请使用 Select-AzSubscription 设置订阅。If you have multiple Azure subscriptions, use Select-AzSubscription to set the subscription.

# Connect-AzAccount -Environment AzureChinaCloud
# Select-AzSubscription -SubscriptionId <xxxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx>

$ResourceGroupName = "<Resource Group Name>" # Resource group name
$VNetName = "<Virtual Network Name>"         # Virtual network name
$SubnetName = "<Subnet Name>"                # Subnet name
$ILBName = "<Load Balancer Name>"            # ILB name
$Location = "<Azure Region>"                 # Azure location
$VMNames = "<VM1>","<VM2>"                   # Virtual machine names

$ILBIP = "<n.n.n.n>"                         # IP address
[int]$ListenerPort = "<nnnn>"                # AG listener port
[int]$ProbePort = "<nnnn>"                   # Probe port

$LBProbeName ="ILBPROBE_$ListenerPort"       # The Load balancer Probe Object Name              
$LBConfigRuleName = "ILBCR_$ListenerPort"    # The Load Balancer Rule Object Name

$FrontEndConfigurationName = "FE_SQLAGILB_1" # Object name for the front-end configuration 
$BackEndConfigurationName ="BE_SQLAGILB_1"   # Object name for the back-end configuration

$VNet = Get-AzVirtualNetwork -Name $VNetName -ResourceGroupName $ResourceGroupName 

$Subnet = Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $VNet -Name $SubnetName 

$FEConfig = New-AzLoadBalancerFrontendIpConfig -Name $FrontEndConfigurationName -PrivateIpAddress $ILBIP -SubnetId $Subnet.id

$BEConfig = New-AzLoadBalancerBackendAddressPoolConfig -Name $BackEndConfigurationName 

$SQLHealthProbe = New-AzLoadBalancerProbeConfig -Name $LBProbeName -Protocol tcp -Port $ProbePort -IntervalInSeconds 15 -ProbeCount 2

$ILBRule = New-AzLoadBalancerRuleConfig -Name $LBConfigRuleName -FrontendIpConfiguration $FEConfig -BackendAddressPool $BEConfig -Probe $SQLHealthProbe -Protocol tcp -FrontendPort $ListenerPort -BackendPort $ListenerPort -LoadDistribution Default -EnableFloatingIP 

$ILB= New-AzLoadBalancer -Location $Location -Name $ILBName -ResourceGroupName $ResourceGroupName -FrontendIpConfiguration $FEConfig -BackendAddressPool $BEConfig -LoadBalancingRule $ILBRule -Probe $SQLHealthProbe 

$bepool = Get-AzLoadBalancerBackendAddressPoolConfig -Name $BackEndConfigurationName -LoadBalancer $ILB 

foreach($VMName in $VMNames)
    {
        $VM = Get-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName 
        $NICName = ($vm.NetworkProfile.NetworkInterfaces.Id.split('/') | select -last 1)
        $NIC = Get-AzNetworkInterface -name $NICName -ResourceGroupName $ResourceGroupName
        $NIC.IpConfigurations[0].LoadBalancerBackendAddressPools = $BEPool
        Set-AzNetworkInterface -NetworkInterface $NIC
        start-AzVM -ResourceGroupName $ResourceGroupName -Name $VM.Name 
    }

示例脚本:使用 PowerShell 将 IP 地址添加到现有负载均衡器Example script: Add an IP address to an existing load balancer with PowerShell

要使用多个可用性组,请将附加的 IP 地址添加到负载均衡器。To use more than one availability group, add an additional IP address to the load balancer. 每个 IP 地址都需要有自身的负载均衡规则、探测端口和前端端口。Each IP address requires its own load-balancing rule, probe port, and front port.

前端端口是应用程序用来连接到 SQL Server 实例的端口。The front-end port is the port that applications use to connect to the SQL Server instance. 不同可用性组的 IP 地址可以使用相同的前端端口。IP addresses for different availability groups can use the same front-end port.

备注

对于 SQL Server 可用性组,每个 IP 地址需要一个特定的探测端口。For SQL Server availability groups, each IP address requires a specific probe port. 例如,如果负载均衡器上有一个 IP 地址使用探测端口 59999,该负载均衡器上的其他任何 IP 地址就不能使用探测端口 59999。For example, if one IP address on a load balancer uses probe port 59999, no other IP addresses on that load balancer can use probe port 59999.

以下脚本将新的 IP 地址添加到现有负载均衡器。The following script adds a new IP address to an existing load balancer. ILB 使用侦听程序端口作为负载均衡前端端口。The ILB uses the listener port for the load-balancing front-end port. 此端口可以是 SQL Server 正在侦听的端口。This port can be the port that SQL Server is listening on. 对于 SQL Server 的默认实例,此端口为 1433。For default instances of SQL Server, the port is 1433. 可用性组的负载均衡规则需要浮动 IP(直接服务器返回),因此后端端口与前端端口相同。The load-balancing rule for an availability group requires a floating IP (direct server return) so the back-end port is the same as the front-end port. 请更新环境的变量。Update the variables for your environment.

# Connect-AzAccount -Environment AzureChinaCloud
# Select-AzSubscription -SubscriptionId <xxxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx>

$ResourceGroupName = "<ResourceGroup>"          # Resource group name
$VNetName = "<VirtualNetwork>"                  # Virtual network name
$SubnetName = "<Subnet>"                        # Subnet name
$ILBName = "<ILBName>"                          # ILB name                      

$ILBIP = "<n.n.n.n>"                            # IP address
[int]$ListenerPort = "<nnnn>"                   # AG listener port
[int]$ProbePort = "<nnnnn>"                     # Probe port 

$ILB = Get-AzLoadBalancer -Name $ILBName -ResourceGroupName $ResourceGroupName 

$count = $ILB.FrontendIpConfigurations.Count+1
$FrontEndConfigurationName ="FE_SQLAGILB_$count"  

$LBProbeName = "ILBPROBE_$count"
$LBConfigrulename = "ILBCR_$count"

$VNet = Get-AzVirtualNetwork -Name $VNetName -ResourceGroupName $ResourceGroupName 
$Subnet = Get-AzVirtualNetworkSubnetConfig -VirtualNetwork $VNet -Name $SubnetName

$ILB | Add-AzLoadBalancerFrontendIpConfig -Name $FrontEndConfigurationName -PrivateIpAddress $ILBIP -SubnetId $Subnet.Id 

$ILB | Add-AzLoadBalancerProbeConfig -Name $LBProbeName  -Protocol Tcp -Port $Probeport -ProbeCount 2 -IntervalInSeconds 15  | Set-AzLoadBalancer 

$ILB = Get-AzLoadBalancer -Name $ILBname -ResourceGroupName $ResourceGroupName

$FEConfig = get-AzLoadBalancerFrontendIpConfig -Name $FrontEndConfigurationName -LoadBalancer $ILB

$SQLHealthProbe  = Get-AzLoadBalancerProbeConfig -Name $LBProbeName -LoadBalancer $ILB

$BEConfig = Get-AzLoadBalancerBackendAddressPoolConfig -Name $ILB.BackendAddressPools[0].Name -LoadBalancer $ILB 

$ILB | Add-AzLoadBalancerRuleConfig -Name $LBConfigRuleName -FrontendIpConfiguration $FEConfig  -BackendAddressPool $BEConfig -Probe $SQLHealthProbe -Protocol tcp -FrontendPort  $ListenerPort -BackendPort $ListenerPort -LoadDistribution Default -EnableFloatingIP | Set-AzLoadBalancer   

配置侦听器Configure the listener

可用性组侦听器是 SQL Server 可用性组侦听的 IP 地址和网络名称。The availability group listener is an IP address and network name that the SQL Server availability group listens on. 若要创建可用性组侦听器,请执行以下操作:To create the availability group listener, do the following:

  1. 获取群集网络资源的名称。Get the name of the cluster network resource.

    a.a. 使用 RDP 连接到托管主副本的 Azure 虚拟机。Use RDP to connect to the Azure virtual machine that hosts the primary replica.

    b.b. 打开故障转移群集管理器。Open Failover Cluster Manager.

    c.c. 选择“网络”节点,并记下群集网络名称。 Select the Networks node, and note the cluster network name. 在 PowerShell 脚本的 $ClusterNetworkName 变量中使用此名称。Use this name in the $ClusterNetworkName variable in the PowerShell script. 在下图中,群集网络名称为“群集网络 1”: In the following image the cluster network name is Cluster Network 1:

    群集网络名称 Cluster Network Name

  2. 添加客户端接入点。Add the client access point.
    客户端接入点是应用程序用来连接到可用性组中的数据库的网络名称。The client access point is the network name that applications use to connect to the databases in an availability group. 可在故障转移群集管理器中创建客户端接入点。Create the client access point in Failover Cluster Manager.

    a.a. 展开群集名称,并单击“角色”。 Expand the cluster name, and then click Roles.

    b.b. 在“角色”窗格中,右键单击可用性组名称,并选择“添加资源” > “客户端接入点”。 In the Roles pane, right-click the availability group name, and then select Add Resource > Client Access Point.

    客户端接入点

    c.c. 在“名称”框中,创建新侦听器的名称。 In the Name box, create a name for this new listener. 新侦听器的名称是应用程序用来连接 SQL Server 可用性组中数据库的网络名称。The name for the new listener is the network name that applications use to connect to databases in the SQL Server availability group.

    d.d. 如果要完成创建侦听器,请单击“下一步”两次,并单击“完成”。 To finish creating the listener, click Next twice, and then click Finish. 不要在此时使侦听器或资源联机。Do not bring the listener or resource online at this point.

  3. 使可用性组群集角色脱机。Take the availability group cluster role offline. 在“故障转移群集管理器” 的“角色” 下,右键单击角色,然后选择“停止角色” 。In Failover Cluster Manager under Roles, right-click the role, and select Stop Role.

  4. 配置可用性组的 IP 资源。Configure the IP resource for the availability group.

    a.a. 单击“资源”选项卡,并展开创建的客户端接入点。 Click the Resources tab, and then expand the client access point you created.
    客户端接入点处于脱机状态。The client access point is offline.

    客户端接入点

    b.b. 右键单击 IP 资源,并单击“属性”。Right-click the IP resource, and then click properties. 记下 IP 地址的名称,将其用在 PowerShell 脚本的 $IPResourceName 变量中。Note the name of the IP address, and use it in the $IPResourceName variable in the PowerShell script.

    c.c. 在“IP 地址”下面,单击“静态 IP 地址”。 Under IP Address, click Static IP Address. 将 IP 地址设置为在 Azure 门户中设置负载均衡器地址时所用的同一地址。Set the IP address as the same address that you used when you set the load balancer address on the Azure portal.

    IP 资源

    1. 使 SQL Server 可用性组资源依赖于客户端接入点。1. Make the SQL Server availability group resource dependent on the client access point.

    a.a. 在故障转移群集管理器中,单击“角色” ,然后单击可用性组。In Failover Cluster Manager, click Roles, and then click your availability group.

    b.b. 在“资源” 选项卡的“其他资源”下,右键单击可用性资源组,然后单击“属性” 。On the Resources tab, under Other Resources, right-click the availability resource group, and then click Properties.

    c.c. 在依赖关系选项卡上,添加客户端接入点(侦听器)资源的名称。On the dependencies tab, add the name of the client access point (the listener) resource.

    IP 资源

    d.d. 单击 “确定”Click OK.

  5. 使客户端接入点资源依赖于 IP 地址。Make the client access point resource dependent on the IP address.

    a.a. 在故障转移群集管理器中,单击“角色” ,然后单击可用性组。In Failover Cluster Manager, click Roles, and then click your availability group.

    b.b. 在“资源”选项卡中,右键单击“服务器名称”下面的客户端接入点资源,并单击“属性”。 On the Resources tab, right-click the client access point resource under Server Name, and then click Properties.

    IP 资源

    c.c. 选择“依赖项”选项卡。 验证 IP 地址是否为依赖项。Click the Dependencies tab. Verify that the IP address is a dependency. 如果不是,则设置 IP 地址的依赖项。If it is not, set a dependency on the IP address. 如果有多个资源列出,请验证 IP 地址具有 OR 而不是 AND 依赖项。If there are multiple resources listed, verify that the IP addresses have OR, not AND, dependencies. 单击 “确定”Click OK.

    IP 资源

    提示

    可以验证是否正确配置了依赖项。You can validate that the dependencies are correctly configured. 在故障转移群集管理器中,转到“角色”,右键单击可用性组,单击“更多操作” ,然后单击“显示依赖项报告” 。In Failover Cluster Manager, go to Roles, right-click the availability group, click More Actions, and then click Show Dependency Report. 正确配置依赖项后,可用性组将依赖于网络名称,网络名称将依赖于 IP 地址。When the dependencies are correctly configured, the availability group is dependent on the network name, and the network name is dependent on the IP address.

  6. 在 PowerShell 中设置群集参数。Set the cluster parameters in PowerShell.

    a.a. 将以下 PowerShell 脚本复制到某个 SQL Server 实例。Copy the following PowerShell script to one of your SQL Server instances. 请更新环境的变量。Update the variables for your environment.

    • $ListenerILBIP 是在 Azure 负载均衡器上为可用性组侦听程序创建的 IP 地址。$ListenerILBIP is the IP address that you created on the Azure load balancer for the availability group listener.

    • $ListenerProbePort 是在 Azure 负载均衡器上为可用性组侦听程序配置的端口。$ListenerProbePort is the port you configured on the Azure load balancer for the availability group listener.

    $ClusterNetworkName = "<MyClusterNetworkName>" # the cluster network name (Use Get-ClusterNetwork on Windows Server 2012 of higher to find the name)
    $IPResourceName = "<IPResourceName>" # the IP Address resource name
    $ListenerILBIP = "<n.n.n.n>" # the IP Address of the Internal Load Balancer (ILB). This is the static IP address for the load balancer you configured in the Azure portal.
    [int]$ListenerProbePort = <nnnnn>
    
    Import-Module FailoverClusters
    
    Get-ClusterResource $IPResourceName | Set-ClusterParameter -Multiple @{"Address"="$ListenerILBIP";"ProbePort"=$ListenerProbePort;"SubnetMask"="255.255.255.255";"Network"="$ClusterNetworkName";"EnableDhcp"=0}
    

    b.b. 通过在某个群集节点上运行 PowerShell 脚本设置群集参数。Set the cluster parameters by running the PowerShell script on one of the cluster nodes.

    备注

    如果 SQL Server 实例位于不同的区域,则需要运行 PowerShell 脚本两次。If your SQL Server instances are in separate regions, you need to run the PowerShell script twice. 第一次运行时,请从第一个区域中选择 $ListenerILBIP$ListenerProbePortThe first time, use the $ListenerILBIP and $ListenerProbePort from the first region. 第二次运行时,请从第二个区域中选择 $ListenerILBIP$ListenerProbePortThe second time, use the $ListenerILBIP and $ListenerProbePort from the second region. 每个区域的群集网络名称和群集 IP 资源名称也不同。The cluster network name and the cluster IP resource name are also different for each region.

  7. 使可用性组群集角色联机。Bring the availability group cluster role online. 在“故障转移群集管理器” 的“角色” 下,右键单击角色,然后选择“启动角色” 。In Failover Cluster Manager under Roles, right click the role, and select Start Role.

如有必要,重复上述步骤以设置 WSFC 群集 IP 地址的群集参数。If necessary, repeat the steps above to set the cluster parameters for the WSFC cluster IP address.

  1. 获取 WSFC 群集 IP 地址的 IP 地址名称。Get the IP address name of the WSFC Cluster IP address. 在“故障转移群集管理器” 中的“群集核心资源” 下,找到“服务器名称” 。In Failover Cluster Manager under Cluster Core Resources, locate Server Name.

  2. 右键单击“IP 地址” ,并选择“属性” 。Right-click IP Address, and select Properties.

  3. 记下 IP 地址的名称Copy the Name of the IP address. 该项可能为 Cluster IP AddressIt may be Cluster IP Address.

  4. 在 PowerShell 中设置群集参数。Set the cluster parameters in PowerShell.

    a.a. 将以下 PowerShell 脚本复制到某个 SQL Server 实例。Copy the following PowerShell script to one of your SQL Server instances. 请更新环境的变量。Update the variables for your environment.

    • $ClusterCoreIP 是在 Azure 负载均衡器上为 WSFC 核心群集资源创建的 IP 地址。$ClusterCoreIP is the IP address that you created on the Azure load balancer for the WSFC core cluster resource. 它不同于可用性组侦听程序的 IP 地址。It is different from the IP address for the availability group listener.

    • $ClusterProbePort 是在 Azure 负载均衡器上为 WSFC 运行状况探测配置的端口。$ClusterProbePort is the port you configured on the Azure load balancer for the WSFC health probe. 它不同于可用性组侦听程序的探测。It is different from the probe for the availability group listener.

    $ClusterNetworkName = "<MyClusterNetworkName>" # the cluster network name (Use Get-ClusterNetwork on Windows Server 2012 of higher to find the name)
    $IPResourceName = "<ClusterIPResourceName>" # the IP Address resource name
    $ClusterCoreIP = "<n.n.n.n>" # the IP Address of the Cluster IP resource. This is the static IP address for the load balancer you configured in the Azure portal.
    [int]$ClusterProbePort = <nnnnn> # The probe port from the WSFCEndPointprobe in the Azure portal. This port must be different from the probe port for the availability group listener probe port.
    
    Import-Module FailoverClusters
    
    Get-ClusterResource $IPResourceName | Set-ClusterParameter -Multiple @{"Address"="$ClusterCoreIP";"ProbePort"=$ClusterProbePort;"SubnetMask"="255.255.255.255";"Network"="$ClusterNetworkName";"EnableDhcp"=0}
    

    b.b. 通过在某个群集节点上运行 PowerShell 脚本设置群集参数。Set the cluster parameters by running the PowerShell script on one of the cluster nodes.

警告

可用性组侦听程序运行状况探测端口必须不同于群集核心 IP 地址运行状况探测端口。The availability group listener health probe port has to be different from the cluster core IP address health probe port. 在这些示例中,侦听程序端口为 59999,群集核心 IP 地址为 58888。In these examples, the listener port is 59999 and the cluster core IP address is 58888. 这两个端口都要求允许入站防火墙规则。Both ports require an allow inbound firewall rule.

在 SQL Server Management Studio 中设置侦听器端口Set the listener port in SQL Server Management Studio

  1. 启动 SQL Server Management Studio 并连接到主副本。Launch SQL Server Management Studio and connect to the primary replica.

  2. 导航到“AlwaysOn 高可用性” > “可用性组” > “可用性组侦听器”。 Navigate to AlwaysOn High Availability > Availability Groups > Availability Group Listeners.

  3. 现在应看到在故障转移群集管理器中创建的侦听器名称。You should now see the listener name that you created in Failover Cluster Manager. 右键单击侦听程序名称,并选择“属性”。Right-click the listener name and select Properties.

  4. 在“端口”框中,通过使用先前使用的 $EndpointPort 为可用性组侦听程序指定端口号(默认值为 1433),并选择“确定”。In the Port box, specify the port number for the availability group listener by using the $EndpointPort you used earlier (1433 was the default), then select OK.

测试与侦听器的连接Test the connection to the listener

若要测试连接,请执行以下操作:To test the connection:

  1. 使用远程桌面协议 (RDP) 连接到同一虚拟网络中不拥有副本的 SQL Server。Use Remote Desktop Protocol (RDP) to connect to a SQL Server that is in the same virtual network, but does not own the replica. 这可能是群集中的其他 SQL Server。It might be the other SQL Server in the cluster.

  2. 使用 sqlcmd 实用工具测试连接。Use sqlcmd utility to test the connection. 例如,以下脚本通过侦听器与 Windows 身份验证来与主副本建立 sqlcmd 连接:For example, the following script establishes a sqlcmd connection to the primary replica through the listener with Windows authentication:

    sqlcmd -S <listenerName> -E
    

    如果侦听器使用的端口不是默认端口 (1433),请在连接字符串中指定该端口。If the listener is using a port other than the default port (1433), specify the port in the connection string. 例如,以下 sqlcmd 命令连接到位于端口 1435 的侦听器:For example, the following sqlcmd command connects to a listener at port 1435:

    sqlcmd -S <listenerName>,1435 -E
    

SQLCMD 连接自动连接到托管主副本的 SQL Server 实例。The SQLCMD connection automatically connects to whichever instance of SQL Server hosts the primary replica.

备注

确保指定的端口已在两个 SQL Server 的防火墙上打开。Make sure that the port you specify is open on the firewall of both SQL Servers. 这两个服务器需要所用 TCP 端口的入站规则。Both servers require an inbound rule for the TCP port that you use. 有关详细信息,请参阅 添加或编辑防火墙规则For more information, see Add or Edit Firewall Rule.

指导原则和限制Guidelines and limitations

请注意有关 Azure 中使用内部负载均衡器的可用性组侦听器的以下指导原则:Note the following guidelines on availability group listener in Azure using internal load balancer:

  • 使用内部负载均衡器只能从同一个虚拟网络中访问侦听器。With an internal load balancer, you only access the listener from within the same virtual network.

  • 如果要使用 Azure 网络安全组来限制访问权限,请确保允许规则包括:If you're restricting access with an Azure Network Security Group, ensure that the allow rules include:

    • 后端 SQL Server VM IP 地址The backend SQL Server VM IP addresses
    • 负载均衡器浮动 AG 侦听程序的 IP 地址The load balancer floating IP addresses for the AG listener
    • 群集核心 IP 地址(如果适用)。The cluster core IP address, if applicable.
  • 将标准负载均衡器与 Azure 存储一起用作云见证时,请创建服务终结点。Create a service endpoint when using a standard load balancer with Azure Storage for the cloud witness. 有关详细信息,请参阅从虚拟网络授予访问权限For more information, see Grant access from a virtual network.

更多信息For more information

有关详细信息,请参阅在 Azure VM 中手动配置 Always On 可用性组For more information, see Configure Always On availability group in Azure VM manually.

PowerShell cmdletPowerShell cmdlets

使用以下 PowerShell cmdlet 为 Azure 虚拟机创建内部负载均衡器。Use the following PowerShell cmdlets to create an internal load balancer for Azure Virtual Machines.