为 VM 创建服务器和站点相关性规则Create server and site affinity rules for VMs

适用于 Azure Stack HCI 版本 20H2Applies to Azure Stack HCI version 20H2

使用 Windows PowerShell,可为群集中的虚拟机 (VM) 轻松创建相关性和反相关性规则。Using Windows PowerShell, you can easily create affinity and anti-affinity rules for your virtual machines (VMs) in a cluster.

相关性规则是在两个或两个以上的资源组或角色(例如虚拟机 (VM) )之间建立关系,将它们一起保存在 Azure Stack HCI 群集中。Affinity is a rule that establishes a relationship between two or more resource groups or roles, such as virtual machines (VMs), to keep them together in an Azure Stack HCI cluster. 反相关性则相反,它用于将指定的资源组彼此分开。Anti-affinity is the opposite in that it is used to keep the specified resource groups apart from each other.

使用相关性和反相关性规则,任何群集 VM 要么保留在同一群集节点中,要么被阻止位于同一群集节点中。Using affinity and anti-affinity rules, any clustered VM would either stay in the same cluster node or be prevented from being together in the same cluster node. 这样一来,将 VM 移出节点的唯一方法是手动移出。In this way, the only way to move a VM out of a node would be to do it manually. 还可将 VM 与其 VHDX 所在的群集共享卷 (CSV) 保存在一起。You can also keep VMs together with the Cluster Shared Volume (CSV) that its VHDX resides on.

结合使用相关性和反相关性规则,还可跨两个站点(容错域)配置一个延伸群集,并将 VM 保存在需要的站点中。Combining affinity and anti-affinity rules, you can also configure a stretched cluster across two sites (fault domains) and keep your VMs in the site they need to be in.

通常,可以从远程计算机(而不是群集中的主机服务器)设置规则。Typically, you set up your rules from a remote computer, rather than on a host server in a cluster. 此远程计算机称为管理计算机。This remote computer is called the management computer.

备注

从管理计算机运行 PowerShell 命令时,请将 -Name 或 -Cluster 参数与所管理群集的名称配合使用。When running PowerShell commands from a management computer, include the -Name or -Cluster parameter with the name of the cluster you are managing. 如果适用,在将 -ComputerName 参数用于服务器节点时,还需要指定完全限定的域名 (FQDN)If applicable, you will also need to specify the fully qualified domain name (FQDN) when using the -ComputerName parameter for a server node

新 PowerShell cmdletNew PowerShell cmdlets

若要为 Azure Stack HCI 群集创建相关性规则,请使用以下 PowerShell cmdlet。To create affinity rules for Azure Stack HCI clusters, use the following PowerShell cmdlets.

New-ClusterAffinityRuleNew-ClusterAffinityRule

New-ClusterAffinityRule cmdlet 用于创建新规则。The New-ClusterAffinityRule cmdlet is used to create new rules. 通过此命令,可指定规则的名称和规则类型,其中:With this command you would specify the name of the rule as well as the type of rule it is, where:

-Name 为规则名称-Name is the name of the rule

-RuleType 值为 SameFaultDomain | SameNode | DifferentFaultDomain | DifferentNode-RuleType values are SameFaultDomain | SameNode | DifferentFaultDomain | DifferentNode

示例:Example:

New-ClusterAffinityRule -Name -RuleType SameFaultDomain -Cluster Cluster1

Set-ClusterAffinityRuleSet-ClusterAffinityRule

Set-ClusterAffinityRule cmdlet 用于启用或禁用规则,其中:The Set-ClusterAffinityRule cmdlet is used to enable or disable a rule, where:

-Name 为要启用或禁用的规则名称-Name is the name of the rule to enable or disable

-Enabled | Disabled 启用或禁用规则-Enabled | Disabled enables or disables the rule

示例:Example:

Set-ClusterAffinityRule -Name -Enabled -Cluster Cluster1

Get-ClusterAffinityRuleGet-ClusterAffinityRule

Get-ClusterAffinityRule cmdlet 用于显示指定的规则及其类型。The Get-ClusterAffinityRule cmdlet is used to display the specified rule and what type it is. 如果未指定 -Name,则将列出所有规则。If -Name is not specified, it will list all rules.

示例:Example:

Get-ClusterAffinityRule -Name -Cluster Cluster1

Add-ClusterGroupToAffinityRuleAdd-ClusterGroupToAffinityRule

Add-ClusterGroupToAffinityRule cmdlet 用于将 VM 角色或组名称添加到特定的相关性规则,其中:The Add-ClusterGroupToAffinityRule cmdlet is used to add a VM role or group name to a specific affinity rule, where:

-Groups 为要添加到规则的组或角色的名称-Groups is the name of the group or role to add to the rule

-Name 为要添加的规则名称-Name is the name of the rule to add to

示例:Example:

Add-ClusterGroupToAffinityRule -Groups -Name -Cluster Cluster1

Add-ClusterSharedVolumeToAffinityRuleAdd-ClusterSharedVolumeToAffinityRule

通过 Add-ClusterSharedVolumeToAffinityRule,你的 VM 可与 VHDX 所在的群集共享卷保存在一起,其中:The Add-ClusterSharedVolumeToAffinityRule allows your VMs to stay together with the Cluster Shared Volume the VHDX resides on, where:

-ClusterSharedVolumes 为希望添加到规则的 CSV 磁盘-ClusterSharedVolumes is the CSV disk(s) you wish to add to the rule

-Name 为要添加的规则名称-Name is the name of rule to add to

示例:Example:

Add-ClusterSharedVolumeToAffinityRule  -ClusterSharedVolumes -Name -Cluster Cluster1

Remove-ClusterAffinityRuleRemove-ClusterAffinityRule

Remove-ClusterAffinityRule 删除指定的规则,其中 -Name 是规则名称 。The Remove-ClusterAffinityRule deletes the specified rule, where -Name is the name of the rule.

示例:Example:

Remove-ClusterAffinityRule -Name -Cluster Cluster1

Remove-ClusterGroupFromAffinityRuleRemove-ClusterGroupFromAffinityRule

Remove-ClusterGroupFromAffinityRule 将从特定规则中删除 VM 组或角色,但不会禁用或删除该规则,其中:The Remove-ClusterGroupFromAffinityRule removes a VM group or role from a specific rule but does not disable or delete the rule, where:

-Name 为规则名称-Name is the name of the rule

-Groups 为希望从规则中删除的组或角色-Groups are the groups or roles you wish to remove from the rule

示例:Example:

Remove-ClusterGroupFromAffinityRule -Name -Groups -Cluster Cluster1

Remove-ClusterSharedVolumeFromAffinityRuleRemove-ClusterSharedVolumeFromAffinityRule

Remove-ClusterSharedVolumeFromAffinityRule cmdlet 用于从特定规则中删除群集共享卷,但不会禁用或删除该规则,其中:The Remove-ClusterSharedVolumeFromAffinityRule cmdlet is used to remove the Cluster Shared Volumes from a specific rule but does not disable or delete the rule, where:

-ClusterSharedVolumes 为要从规则中删除的 CSV 磁盘-ClusterSharedVolumes is the CSV disk(s) you want to remove from the rule

-Name 为要添加的规则名称-Name is the name of the rule to add to

示例:Example:

Remove-ClusterSharedVolumeFromAffinityRule -ClusterSharedVolumes -Name -Cluster Cluster1

现有的 PowerShell cmdletExisting PowerShell cmdlets

随着新 cmdlet 的出现,我们还向一些现有的 cmdlet 添加了其他新开关。With the advent of the new cmdlets, we also added additional new switches to a few existing cmdlets.

Move-ClusterGroupMove-ClusterGroup

-IgnoreAffinityRule 开关将忽略规则并将群集资源组移动到另一个群集节点。The new -IgnoreAffinityRule switch ignores the rule and moves the clustered resource group to another cluster node. 有关此 cmdlet 的详细信息,请参阅 Move-ClusterGroupFor more information on this cmdlet, see Move-ClusterGroup.

示例:Example:

Move-ClusterGroup -IgnoreAffinityRule -Cluster Cluster1

备注

如果移动规则有效(受支持),所有受影响的组和角色也将移动。If a move rule is valid (supported), all groups and roles that are affected will also move. 如果 VM 移动需要故意临时违反一个规则一次,则可使用 -IgnoreAffinityRule 开关来允许此移动。If a VM move will knowingly violate a rule yet it is needed on a one-time temporary basis, use the -IgnoreAffinityRule switch to allow the move to occur. 在这种情况下,将显示 VM 的违规警告。In this case, a violation warning for the VM will be displayed. 之后,可根据需要重新启用该规则。You can then enable the rule back as necessary.

Start-ClusterGroupStart-ClusterGroup

-IgnoreAffinityRule 开关将忽略规则并使群集资源组在其当前位置处于联机状态。The new -IgnoreAffinityRule switch ignores the rule and brings the clustered resource group online in its current location. 有关此 cmdlet 的详细信息,请参阅 Start-ClusterGroupFor more information on this cmdlet, see Start-ClusterGroup.

示例:Example:

Start-ClusterGroup -IgnoreAffinityRule -Cluster Cluster1

反相关性规则Anti-affinity rules

方案 1Scenario 1

在同一 Azure Stack HCI 多站点群集上有两个 VM 都运行 SQL Server。You have two VMs each running SQL Server on the same Azure Stack HCI multi-site cluster. 每个 VM 都利用大量内存、CPU 和存储资源。Each VM utilizes a lot of memory, CPU, and storage resources. 如果这两个 VM 最终位于同一节点上,这可能会导致其中一个或两个都出现性能问题,因为它们会争用内存、CPU 和存储周期。If the two end up on the same node, this can cause performance issues with one or both as they compete for memory, CPU, and storage cycles. 使用以 DifferentNode 为规则类型的反相关性规则时,这些 VM 将始终保留在不同的群集节点上。Using an anti-affinity rule with DifferentNode as the rule type, these VMs will always stay on different cluster nodes.

此操作的示例命令如下:The example commands for this would be:

New-ClusterAffinityRule -Name SQL -Ruletype DifferentNode -Cluster Cluster1

Add-ClusterGroupToAffinityRule -Groups SQL1,SQL2 –Name SQL -Cluster Cluster1

Set-ClusterAffinityRule -Name SQL -Enabled 1 -Cluster Cluster1

若要查看规则及其配置方式,请使用 Get-ClusterAffinityRule cmdlet 查看输出:To see the rule and how it is configured, use the Get-ClusterAffinityRule cmdlet to see the output:

Get-ClusterAffinityRule -Name SQL -Cluster Cluster1

Name    RuleType        Groups        Enabled
----    -----------     -------       -------
SQL     DifferentNode   {SQL1, SQL2}     1

方案 2Scenario 2

假设有一个 Azure Stack HCI 延伸群集,其中有两个站点(容错域)。Let's say you have an Azure Stack HCI stretched cluster with two sites (fault domains). 你有两个域控制器,希望将它们保留在不同站点中。You have two domain controllers you wish to keep in separate sites. 使用以 DifferentFaultDomain 为规则类型的反相关性规则时,这些域控制器将始终保留在不同站点中。Using an anti-affinity rule with DifferentFaultDomain as a rule type, these domain controllers will always stay in different sites. 此操作的示例命令如下:The example commands for this would be:

New-ClusterAffinityRule -Name DC -Ruletype DifferentFaultDomain -Cluster Cluster1

Add-ClusterGroupToAffinityRule -Groups DC1,DC2 –Name DC -Cluster Cluster1

Set-ClusterAffinityRule -Name DC -Enabled 1 -Cluster Cluster1

若要查看此规则及其配置方式,请使用 Get-ClusterAffinityRule cmdlet 查看输出:To see this rule and how it is configured, use the Get-ClusterAffinityRule cmdlet to see the output:

Get-ClusterAffinityRule -Name DC -Cluster Cluster1

Name    RuleType                Groups        Enabled
----    --------                -------       -------
DC      DifferentFaultDomain    {DC1, DC2}       1

相关性规则Affinity rules

下面是设置相关性规则的几个常见场景。Here are a few common scenarios for setting up affinity rules.

方案 1Scenario 1

假设你有一个 SQL Server VM 和一个 Web 服务器 VM。Suppose you have a SQL Server VM and a Web Server VM. 这两个 VM 需要始终保留在同一站点中,但不必位于站点中的同一群集节点上。These two VMs need to always remain in the same site but do not necessarily need to be on the same cluster node in the site. 可使用 SameFaultDomain,如下所示:Using SameFaultDomain, this is possible, as shown below:

New-ClusterAffinityRule -Name WebData -Ruletype SameFaultDomain -Cluster Cluster1

Add-ClusterGroupToAffinityRule -Groups SQL1,WEB1 –Name WebData -Cluster Cluster1

Set-ClusterAffinityRule -Name WebData -Enabled 1 -Cluster Cluster1

若要查看此规则及其配置方式,请使用 Get-ClusterAffinityRule cmdlet 查看输出:To see this rule and how it is configured, use the Get-ClusterAffinityRule cmdlet to see the output:

Get-ClusterAffinityRule -Name WebData -Cluster Cluster1

Name        RuleType          Groups        Enabled
----        ---------         ------        -------
WebData     SameFaultDomain   {SQL1, WEB1}     1

方案 2Scenario 2

接下来使用上述同一场景,只是指定 VM 必须位于同一群集节点上,但不必在同一站点中。Let's use the same scenario above except specify that the VMs must reside on the same cluster node but not necessarily in the same site. 可使用 SameNode 按如下所示进行设置:Using SameNode, you can set this as follows:

New-ClusterAffinityRule -Name WebData1 -Ruletype SameNode -Cluster Cluster1

Add-ClusterGroupToAffinityRule -Groups SQL1,WEB1 –Name WebData1 -Cluster Cluster1

Set-ClusterAffinityRule -Name WebData1 -Enabled 1 -Cluster Cluster1

若要查看规则及其配置方式,请使用 Get-ClusterAffinityRule cmdlet 查看输出:To see the rule and how it is configured, use the Get-ClusterAffinityRule cmdlet to see the output:

Get-ClusterAffinityRule -Name WebData1 -Cluster Cluster1

Name    RuleType    Groups        Enabled
----    --------    ------        -------
DC      SameNode    {SQL1, WEB1}     1

合并规则Combined rules

结合使用相关性和反相关性规则,可在多站点群集中轻松配置各种 VM 组合。Combining affinity and anti-affinity rules, you can easily configure various VM combinations across a multi-site cluster. 在此场景中,每个站点都有三个 VM:SQL Server (SQL)、Web 服务器 (WEB) 和域控制器 (DC)。In this scenario, each site has three VMs: SQL Server (SQL), Web Server (WEB), and domain controller (DC). 对于每个组合,可使用相关性规则和 SameFaultDomain 将它们全部放在同一站点中。For each of the combinations, you can use affinity rules with SameFaultDomain to keep them all in the same site. 还可使用反相关性规则和 DifferentFaultDomain 为每个站点设置域控制器,将这些域控制器 VM 保存在不同站点中,如下所示:You can also set the domain controllers for each site with anti-affinity rules and DifferentFaultDomain to keep the domain controller VMs in separate sites as shown below:

New-ClusterAffinityRule -Name Site1Trio -Ruletype SameFaultDomain -Cluster Cluster1

New-ClusterAffinityRule -Name Site2Trio -Ruletype SameFaultDomain -Cluster Cluster1

New-ClusterAffinityRule -Name TrioApart -Ruletype DifferentFaultDomain -Cluster Cluster1

Add-ClusterGroupToAffinityRule -Groups SQL1,WEB1,DC1 –Name Site1Trio -Cluster Cluster1

Add-ClusterGroupToAffinityRule -Groups SQL2,WEB2,DC2 –Name Site2Trio -Cluster Cluster1

Add-ClusterGroupToAffinityRule -Groups DC1,DC2 –Name TrioApart -Cluster Cluster1

Set-ClusterAffinityRule -Name Site1Trio -Enabled 1 -Cluster Cluster1

Set-ClusterAffinityRule -Name Site2Trio -Enabled 1 -Cluster Cluster1

Set-ClusterAffinityRule -Name TrioApart -Enabled 1 -Cluster Cluster1

若要查看规则及其配置方式,请使用不带 -Name 开关的 Get-ClusterAffinityRule cmdlet,你可看到创建的所有规则及其输出。To see the rules and how they are configured, use the Get-ClusterAffinityRule cmdlet without the -Name switch and you can see all rules created and their output.

Get-ClusterAffinityRule -Cluster Cluster1

Name        RuleType               Groups            Enabled
----        --------               ------            -------
Site1Trio   SameFaultDomain        {SQL1, WEB1, DC1}    1
Site2Trio   SameFaultDomain        {SQL2, WEB2, DC2}    1
TrioApart   DifferentFaultDomain   {DC1, DC2}           1

存储相关性规则Storage affinity rules

还可在同一群集节点上的群集共享卷 (CSV) 上保留 VM 及其 VHDX。You can also keep a VM and its VHDX on a Cluster Shared Volume (CSV) on the same cluster node. 这可以防止发生 CSV 重定向,这种重定向可能会降低 VM 的启动或停止速度。This would keep CSV redirection from occurring, which can slow down the starting or stopping of a VM. 考虑到前面合并的相关性和反相关性场景,可将 SQL VM 和群集共享卷保留在同一群集节点上。Taking into account the combined affinity and anti-affinity scenario previously, you can keep the SQL VM and the Cluster Shared Volume on the same cluster node. 为此,请使用以下命令:To do that, use the following commands:

New-ClusterAffinityRule -Name SQL1CSV1 -Ruletype SameNode -Cluster Cluster1

New-ClusterAffinityRule -Name SQL2CSV2 -Ruletype SameNode -Cluster Cluster1

Add-ClusterGroupToAffinityRule -Groups SQL1 –Name SQL1CSV1 -Cluster Cluster1

Add-ClusterGroupToAffinityRule -Groups SQL2 –Name SQL2CSV2 -Cluster Cluster1

Add-ClusterSharedVolumeToAffinityRule -ClusterSharedVolumes CSV1 -Name SQL1CSV1 -Cluster Cluster1

Add-ClusterSharedVolumeToAffinityRule -ClusterSharedVolumes CSV2 -Name SQL2CSV2 -Cluster Cluster1

Set-ClusterAffinityRule -Name SQL1CSV1 -Enabled 1 -Cluster Cluster1

Set-ClusterAffinityRule -Name SQL2CSV2 -Enabled 1 -Cluster Cluster1

若要查看这些规则以及它们的配置方式,请使用不带 -Name 开关的 Get-ClusterAffinityRule cmdlet 并查看输出。To see these rules and how they are configured, use the Get-ClusterAffinityRule cmdlet without the -Name switch and view the output.

Get-ClusterAffinityRule -Cluster Cluster1

Name        RuleType               Groups            Enabled
----        --------               ------            -------
Site1Trio   SameFaultDomain        {SQL1, WEB1, DC1}    1
Site2Trio   SameFaultDomain        {SQL2, WEB2, DC2}    1
TrioApart   DifferentFaultDomain   {DC1, DC2}           1
SQL1CSV1    SameNode               {SQL1, <CSV1-GUID>}  1
SQL2CSV2    SameNode               {SQL2, <CSV2-GUID>}  1

后续步骤Next steps

了解如何管理 VM。Learn how to manage your VMs. 请参阅使用 Windows Admin Center 管理 Azure Stack HCI 上的 VMSee Manage VMs on Azure Stack HCI using Windows Admin Center.