教學課程:在 Azure Stack HCI 和 Windows Server 上的 Azure Kubernetes Service 中部署工作負載叢集
Kubernetes 會提供容器化應用程式的分散式平台。 在本教學課程中的 3/7 部分,會說明如何在 Azure Stack HCI 和 Windows Server 上的 AKS 中,部署 Azure Stack HCI 和 Windows Server 上的 Azure Kubernetes Service (AKS) 叢集。 您將學習如何:
- 在 Azure Stack HCI 上部署 AKS 叢集
- 安裝 Kubernetes CLI (kubectl)
- 設定 kubectl 以連線至工作負載叢集
在之後的教學課程中,Azure Vote 應用程式會部署至叢集、進行調整並加以更新。
開始之前
在先前的教學課程中,已建立容器映像並上傳到 Azure Container Registry 執行個體。 如果您尚未完成這些步驟,請從教學課程 1 - 建立容器映像開始。
本教學課程會使用 AksHci PowerShell 模組。
如果您尚未安裝 AksHci PowerShell 模組,請執行下列命令來安裝模組。
重要
您必須關閉所有現有的 PowerShell 視窗,並開啟全新的系統管理工作階段,以安裝必要 PowerShell 套件和模組。
Install-Module -Name Az.Accounts -Repository PSGallery -RequiredVersion 2.2.4
Install-Module -Name Az.Resources -Repository PSGallery -RequiredVersion 3.2.0
Install-Module -Name AzureAD -Repository PSGallery -RequiredVersion 2.0.2.128
Install-Module -Name AksHci -Repository PSGallery
Import-Module Az.Accounts
Import-Module Az.Resources
Import-Module AzureAD
Import-Module AksHci
如要使用遠端 PowerShell,則必須使用 CredSSP。
重要
您必須再次關閉所有現有的 PowerShell 視窗,以確保重新整理已載入的模組。 在您關閉所有 PowerShell 視窗之前,請勿繼續進行下一個步驟。
驗證您的安裝。
重要
關閉所有 PowerShell 視窗,然後重新開啟新的系統管理工作階段,檢查是否擁有最新版的 PowerShell 模組。
Get-Command -Module AksHci
若要檢視 AksHci PowerShell 命令的完整清單,請參閱 AksHci PowerShell。
安裝 Azure Kubernetes Service 主機
首先,您必須設定註冊設定。
Set-AksHciRegistration -subscription mysubscription -resourceGroupName myresourcegroup
您必須根據 Azure 訂用帳戶和資源群組名稱來自訂這些值。
然後執行下列命令,確保您已符合每個實體節點上的所有需求,以便能在 Azure Stack HCI 上安裝 Azure Kubernetes Service。
Initialize-AksHciNode
接下來,我們會建立虛擬網路。 您將需要取得可用外部交換器的名稱:
Get-VMSwitch
範例輸出:
Name SwitchType NetAdapterInterfaceDescription
---- ---------- ------------------------------
extSwitch External Mellanox ConnectX-3 Pro Ethernet Adapter
執行下列命令,以建立具有靜態 IP 的虛擬網路。
$vnet = New-AksHciNetworkSetting -name myvnet -vSwitchName "extSwitch" -macPoolName myMacPool -k8sNodeIpPoolStart "172.16.10.0" -k8sNodeIpPoolEnd "172.16.10.255" -vipPoolStart "172.16.255.0" -vipPoolEnd "172.16.255.254" -ipAddressPrefix "172.16.0.0/16" -gateway "172.16.0.1" -dnsServers "172.16.0.1" -vlanId 9
然後,使用下列命令來設定部署。
Set-AksHciConfig -imageDir c:\clusterstorage\volume1\Images -cloudConfigLocation c:\clusterstorage\volume1\Config -vnet $vnet -cloudservicecidr "172.16.10.10/16"
現在,您可安裝 Azure Stack HCI 和 Windows Server 上的 AKS 主機。
Install-AksHCi
建立 Kubernetes 叢集
使用 New-AksHciCluster 命令建立 Kubernetes 叢集。 下列範例會建立名為 mycluster 的叢集,其中會有一個名為 linuxnodepool 的 Linux 節點集區,且節點計數為 1。
New-AksHciCluster -name mycluster -nodePoolName linuxnodepool -nodeCount 1
若要確認部署是否成功,請執行下列命令。
Get-AksHcicluster -name mycluster
輸出:
ProvisioningState : provisioned
KubernetesVersion : v1.20.7
NodePools : linuxnodepool
WindowsNodeCount : 0
LinuxNodeCount : 0
ControlPlaneNodeCount : 1
Name : mycluster
注意
如果您在 New-AksHciCluster
中使用新的參數集來部署叢集,然後執行 Get-AksHciCluster
以取得叢集資訊,則輸出中的 WindowsNodeCount
和 LinuxNodeCount
欄位會傳回 0
。 若要找出每個節點集區中準確的節點數目,請使用 Get-AksHciNodePool
命令並指定叢集名稱。
若要取得叢集中的節點集區清單,請執行下列 Get-AksHciNodePool PowerShell 命令。
Get-AksHciNodePool -clusterName mycluster
ClusterName : mycluster
NodePoolName : linuxnodepool
Version : v1.20.7
OsType : Linux
NodeCount : 1
VmSize : Standard_K8S3_v1
Phase : Deployed
安裝 Kubernetes CLI
若要從本機電腦連線至 Kubernetes 叢集,您應使用 kubectl (Kubernetes 命令列用戶端)。
使用 kubectl 連線至叢集
若要設定 kubectl
以連線到 Kubernetes 叢集,請使用 Get-AksHciCredential 命令。 下列範例會針對名為 mycluster 的叢集取得認證。
Get-AksHciCredential -name mycluster
若要驗證叢集的連線,請執行 kubectl get nodes 命令以傳回叢集節點的清單:
kubectl get nodes
輸出:
NAME STATUS ROLES AGE VERSION
moc-lbs6got5dqo Ready <none> 6d20h v1.20.7
moc-lel7tzxdt30 Ready control-plane,master 6d20h v1.20.7
後續步驟
在本教學課程中,Kubernetes 叢集已部署在 AKS 中,且您已設定 kubectl
加以連線。 您已了解如何︰
- 在 Azure Stack HCI 上部署 AKS 叢集
- 安裝 Kubernetes CLI (kubectl)
- 設定 kubectl 以連線至您的 AKS 叢集
繼續進行下一個教學課程,以了解如何將應用程式部署至叢集。