建立 Vm 的伺服器和網站親和性規則Create server and site affinity rules for VMs

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

使用 Windows Admin Center 或 Windows PowerShell,您可以輕鬆地為叢集中 (Vm) 的虛擬機器建立親和性和反親和性規則。Using either Windows Admin Center or Windows PowerShell, you can easily create affinity and anti-affinity rules for your virtual machines (VMs) in a cluster.

相似性是一種規則,可建立兩個或多個資源群組或角色之間的關聯性,例如 (Vm) 的虛擬機器,讓它們在相同的伺服器、叢集或網站上保持在一起。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 on the same server, cluster, or site. 反親和性的相反之處是,它是用來將指定的 Vm 或資源群組彼此分開,例如放置在不同伺服器上的兩個網域控制站,或個別的網站以進行嚴重損壞修復。Anti-affinity is the opposite in that it is used to keep the specified VMs or resource groups apart from each other, such as two domain controllers placed on separate servers or in separate sites for disaster recovery.

親和性和反親和性規則的使用方式與 Azure 使用可用性區域的方式相似。Affinity and anti-affinity rules are used similarly to the way Azure uses Availability Zones. 在 Azure 中,您可以將可用性區域設定為將 Vm 保留在不同的區域中,並將其放在彼此的不同區域中。In Azure, you can configure Availability Zones to keep VMs in separate zones and away from each other or in the same zone with 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 its own storage, such as 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 and keep your VMs in the site they need to be in.

使用 Windows Admin CenterUsing Windows Admin Center

您可以使用 Windows Admin Center 建立基本親和性和反親和性規則。You can create basic affinity and anti-affinity rules using Windows Admin Center.

虛擬機器畫面

  1. 在 Windows Admin Center 首頁] 的 [ 所有連線] 下,選取您要為其建立 VM 規則的伺服器或叢集。In Windows Admin Center home, under All connections, select the server or cluster you want to create the VM rule for.
  2. 在 [ 工具] 底下,選取 [ 設定]。Under Tools, select Settings.
  3. 在 [設定] 底下,選取 [相似性規則],然後選取 [相似性規則] 下的 [建立規則Under Settings, select Affinity rules, then select Create rule under Affinity rules.
  4. 在 [ 規則名稱] 下,輸入規則的名稱。Under Rule name, enter a name for your rule.
  5. 在 [規則類型] 下,選取 [ **一起] (相同的伺服器) ** 或 ** (不同的伺服器) ** 將 vm 放在相同的伺服器或不同的伺服器上。Under Rule type, select either Together (same server) or Apart (different servers) to place your VMs on the same server or on different servers.
  6. 在 [ 套用至] 下方,選取將套用此規則的 vm。Under Applies to, select the VMs that this rule will apply to. 使用 [ 新增 ] 按鈕,將更多 vm 新增至規則。Use the Add button to add more VMs to the rule.
  7. 完成時,按一下 [ 建立規則]。When finished, click Create rule.
  8. 若要刪除規則,只要選取規則,然後按一下 [ 刪除規則]。To delete a rule, simply select it and click Delete rule.

使用 Windows PowerShellUsing Windows PowerShell

您可以使用 Windows PowerShell 建立比使用 Windows Admin Center 更複雜的規則。You can create more complex rules using Windows PowerShell than using Windows Admin Center. 一般來說,您是從遠端電腦設定規則,而不是在叢集中的主機伺服器上設定。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.

從管理電腦執行 Windows PowerShell 命令時,請將 -Name 或參數包含您要管理的叢集 -Cluster 名稱。When running Windows PowerShell commands from a management computer, include the -Name or -Cluster parameter with the name of the cluster you are managing. 如果適用,您也必須在使用伺服器節點的參數時,指定 (FQDN) 的完整功能變數名稱 -ComputerNameIf 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

若要建立叢集的親和性規則,請使用下列新的 PowerShell Cmdlet:To create affinity rules for clusters, use the following new 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 磁片 (s) -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 磁片 (s) -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 的詳細資訊,請參閱 移動->get-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 的詳細資訊,請參閱 >get-clustergroupFor more information on this cmdlet, see Start-ClusterGroup.

範例:Example:

Start-ClusterGroup -IgnoreAffinityRule -Cluster Cluster1

相似性規則範例Affinity rule examples

親和性規則會「一起」規則,將資源保留在相同的伺服器、叢集或網站上。Affinity rules are "together" rules that keep resources on the same server, cluster, or site. 以下是一些設定親和性規則的常見案例。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

反親和性規則範例Anti-affinity rule examples

反親和性規則是「分離」的規則,可分隔資源並將其放在不同的伺服器、叢集或網站上。Anti-affinity rules are "apart" rules that separate resources and place them on different servers, clusters, or sites.

實例 1Scenario 1

您有兩個 Vm,每個 Vm 都在相同的 Azure Stack HCI 多網站叢集上執行 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. 如果兩個端點都在相同的節點上,這可能會導致一或兩個競爭的效能問題,因為它們競爭記憶體、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

合併的規則範例Combined rule examples

結合親和性和反親和性規則,您可以輕鬆地跨多網站叢集設定各種 VM 組合。Combining affinity and anti-affinity rules, you can easily configure various VM combinations across a multi-site cluster. 在此案例中,每個網站都有三個 Vm: SQL Server (SQL) 、網頁伺服器 (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

若要查看規則及其設定方式,請在不使用 Get-ClusterAffinityRule 參數的情況下使用 Cmdlet, -Name 您可以看到所有建立的規則及其輸出。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

您也可以在相同的叢集節點上,將 VM 和其 VHDX 保留在叢集共用磁碟區的 (CSV) 上。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

若要查看這些規則及其設定方式,請使用 Get-ClusterAffinityRule 不含-Name 參數的 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.