建立 VM 的伺服器和網站親和性規則
適用於:Azure Stack HCI 版本 23H2 和 22H2
您可以使用 Windows Admin Center 或 Windows PowerShell,輕鬆地為叢集中的虛擬機器 (VM) 建立親和性和反親和性規則。
注意
在 Azure Stack HCI 23H2 上建立和管理 VM 的建議方式是使用 Azure Arc 控制平面。 只有在您需要 Azure Arc VM 中無法使用的功能時,才能使用下列機制來管理 VM。
親和性是一項規則,可建立兩個或多個資源群組或角色 (例如虛擬機器 (VM)) 之間的關聯性,以將其保留在相同的伺服器、叢集或站台上。 反親和性是相反的,在於它用來讓指定的 VM 或資源群組彼此分開,例如位於不同伺服器或個別月臺的兩個域控制器,以進行災害復原。
親和性和反親和性規則的使用方式與 Azure 使用可用性區域的方式類似。 在 Azure 中,您可以設定可用性區域,將 VM 彼此分開保留在不同的區域中,或將其保留在相同的區域中。
使用親和性和反親和性規則時,任何叢集 VM 都會保留在相同的叢集節點中,或避免保留在相同的叢集節點中。 如此一來,將 VM 移出節點的唯一方法就是手動執行。 您也可以將 VM 及其本身的儲存體保留在一起,例如其 VHDX 所在的叢集共用磁碟區 (CSV)。
您也可以結合親和性和反親和性規則,跨兩個站台設定延展式叢集,並將您的 VM 保留在需要的站台中。
使用 Windows Admin Center
您可以使用 Windows Admin Center 建立基本親和性和反親和性規則。
- 在 Windows Admin Center 首頁的 [所有連線] 下,選取您要為其建立 VM 規則的伺服器或叢集。
- 在 [工具] 下,選取 [設定]。
- 在 [設定] 下,選取 [親和性規則],然後選取 [親和性規則] 下的 [建立規則]。
- 在 [規則名稱] 下,輸入您的規則名稱。
- 在 [規則類型] 下,選取 [Together (same server)] \(在一起 (相同的伺服器)\) 或 [Apart (different servers)] \(分開 (不同的伺服器)\),將您的 VM 放在相同的伺服器或不同的伺服器上。
- 在 [套用至] 底下,選取此規則適用的 VM。 使用 [新增] 按鈕,將更多 VM 新增至規則。
- 完成時,選取 [ 建立規則]。
- 若要刪除規則,只要選取規則並選取 [ 刪除規則]。
使用 Windows PowerShell
您可以使用 Windows PowerShell 建立比使用 Windows Admin Center 更複雜的規則。 您通常會從遠端電腦設定規則,而不是在叢集中的主機伺服器上設定。 這部遠端電腦稱為管理電腦。
從管理計算機執行 Windows PowerShell 命令時,請包含 -Name
或 -Cluster
參數,其中包含您要管理的叢集名稱。 如果適用,當您使用 -ComputerName
伺服器節點的參數時,也需要指定 FQDN (完整功能變數名稱)
新的 PowerShell Cmdlet
若要建立叢集的親和性規則,請使用下列新的 PowerShell Cmdlet:
New-ClusterAffinityRule
New-ClusterAffinityRule
Cmdlet 可用來建立新的規則。 使用此指令,您會指定規則的名稱及其規則的類型,其中:
-Name
是規則的名稱
-RuleType
值包括 SameFaultDomain
| SameNode
| DifferentFaultDomain
| DifferentNode
範例:
New-ClusterAffinityRule -Name Rule1 -RuleType SameFaultDomain
Set-ClusterAffinityRule
Set-ClusterAffinityRule
Cmdlet 可用來啟用或停用規則,其中:
-Name
是要啟用或停用的規則名稱
-Enabled
| Disabled
可啟用或停用規則
範例:
Set-ClusterAffinityRule -Name Rule1 -Enabled
Get-ClusterAffinityRule
Get-ClusterAffinityRule
Cmdlet 可用來顯示指定的規則及其類型。 如果未 -Name
指定,則會列出所有規則。
範例:
Get-ClusterAffinityRule -Name Rule1
Add-ClusterGroupToAffinityRule
Add-ClusterGroupToAffinityRule
Cmdlet 可用來將 VM 角色或群組名新增至特定親和性規則,其中:
-Groups
是要新增至規則的群組或角色名稱
-Name
是要新增的規則名稱
範例:
Add-ClusterGroupToAffinityRule -Groups Group1 -Name Rule1
Add-ClusterSharedVolumeToAffinityRule
Add-ClusterSharedVolumeToAffinityRule
可讓您的 VM 與 VHDX 所在的叢集共用磁碟區保留在一起,其中:
-ClusterSharedVolumes
是您想要新增至規則的 CSV 磁碟
-Name
是要新增的規則名稱
範例:
Add-ClusterSharedVolumeToAffinityRule -ClusterSharedVolumes CSV1 -Name Rule1
Remove-ClusterAffinityRule
Remove-ClusterAffinityRule
會刪除指定的規則,其中 -Name
是規則的名稱。
範例:
Remove-ClusterAffinityRule -Name Rule1
Remove-ClusterGroupFromAffinityRule
會 Remove-ClusterGroupFromAffinityRule
從特定規則移除 VM 群組或角色,但不會停用或刪除規則,其中:
-Name
是規則的名稱
-Groups
是您想要從規則中移除的群組或角色
範例:
Remove-ClusterGroupFromAffinityRule -Name Rule1 -Groups Group1
Remove-ClusterSharedVolumeFromAffinityRule
Cmdlet Remove-ClusterSharedVolumeFromAffinityRule
可用來從特定規則移除叢集共用磁碟區,但不會停用或刪除規則,其中:
-ClusterSharedVolumes
是您想要從規則中移除的 CSV 磁碟
-Name
是要新增的規則名稱
範例:
Remove-ClusterSharedVolumeFromAffinityRule -ClusterSharedVolumes CSV1 -Name Rule1
現有的 PowerShell Cmdlet
隨著新 Cmdlet 的出現,我們也將額外的參數新增至幾個現有的 Cmdlet。
Move-ClusterGroup
新的 -IgnoreAffinityRule
參數會忽略規則,並將叢集資源群組移至另一個叢集節點。 如需此 Cmdlet 的詳細資訊,請參閱 Move-ClusterGroup。
範例:
Move-ClusterGroup -IgnoreAffinityRule -Cluster Cluster1
注意
如果移動規則有效 (支援),則所有受影響的群組和角色也會移動。 如果 VM 移動已知違反某項規則,但暫時需要移動一次,請使用 -IgnoreAffinityRule
參數來允許進行移動。 在此情況下,將會顯示 VM 的違規警告。 然後,您可以視需要重新啟用規則。
Start-ClusterGroup
新的 -IgnoreAffinityRule
參數會忽略規則,並讓叢集資源群組在其目前位置上線。 如需此 Cmdlet 的詳細資訊,請參閱 Start-ClusterGroup。
範例:
Start-ClusterGroup -IgnoreAffinityRule -Cluster Cluster1
親和性規則範例
親和性規則是「在一起」的規則,會將資源保留在相同的伺服器、叢集或站台上。 以下是一些設定親和性規則的常見案例。
案例 1
假設您有一個 SQL Server VM 和一個網頁伺服器 VM。 這兩部 VM 必須一律保留在相同的月臺,但不一定需要在站台中的相同叢集節點上。 使用 SameFaultDomain
可能達成此目的,如下所示:
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 來查看輸出:
Get-ClusterAffinityRule -Name WebData -Cluster Cluster1
Name RuleType Groups Enabled
---- --------- ------ -------
WebData SameFaultDomain {SQL1, WEB1} 1
案例 2
讓我們使用上述相同的案例,但指定 VM 必須位於相同的叢集節點上。 您可以使用 SameNode
進行此設定,如下所示:
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 來檢視輸出:
Get-ClusterAffinityRule -Name WebData1 -Cluster Cluster1
Name RuleType Groups Enabled
---- -------- ------ -------
DC SameNode {SQL1, WEB1} 1
反親和性規則範例
反親和性規則是「分開」的規則,可分隔資源並將其放在不同的伺服器、叢集或站台上。
案例 1
您有兩個 VM,各在相同的 Azure Stack HCI 多站台叢集上執行 SQL Server。 每個 VM 都使用了大量的記憶體、CPU 和儲存體資源。 如果這兩者最後在相同的節點上,則可能會因為競爭記憶體、CPU 和儲存體週期,而導致其中之一或兩者發生效能問題。 使用反親和性規則並以 DifferentNode
作為規則類型時,這些 VM 一律會保留在不同的叢集節點上。
此案例的範例命令如下:
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 來檢視輸出:
Get-ClusterAffinityRule -Name SQL -Cluster Cluster1
Name RuleType Groups Enabled
---- ----------- ------- -------
SQL DifferentNode {SQL1, SQL2} 1
案例 2
假設您有一個 Azure Stack HCI 延展式叢集,其中包含兩個站台 (容錯網域)。 您有兩個要保留在不同站台中的網域控制站。 使用反親和性規則並以 DifferentFaultDomain
作為規則類型時,這些網域控制站一律會保留在不同的站台中。 此案例的範例命令如下:
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 來查看輸出:
Get-ClusterAffinityRule -Name DC -Cluster Cluster1
Name RuleType Groups Enabled
---- -------- ------- -------
DC DifferentFaultDomain {DC1, DC2} 1
組合規則範例
您可以結合親和性和反親和性規則,輕鬆地跨多站台叢集設定各種 VM 組合。 在此案例中,每個站台都有三個 VM: SQL Server (SQL)、網頁伺服器 (WEB) 和網域控制站 (DC)。 針對每個組合,您可以使用親和性規則搭配 SameFaultDomain
,將其全部保留在相同的站台中。 您也可以使用反親和性規則和 DifferentFaultDomain
來設定每個站台的網域控制站,將網域控制站 VM 保留在不同的站台中,如下所示:
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,而且您可以看到已建立的所有規則及其輸出。
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
儲存體親和性規則
您也可以將 VM 及其在叢集共用磁碟區的 (CSV) 上的 VHDX 保留在相同的叢集節點上。 這會導致無法進行 CSV 重新導向,而可能會使 VM 的啟動或停止變慢。 考慮到先前結合的親和性和反親和性案例,您可以將 SQL VM 和叢集共用磁碟區保留在相同的叢集節點上。 若要這樣做,請使用下列命令:
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 並檢視輸出。
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
後續步驟
了解如何管理您的 VM。 請參閱使用 Windows Admin Center 管理 Azure Stack HCI 上的 VM。