跳至主要内容

建立 AKS 叢集

您可以使用 Rancher 建立託管在 Microsoft Azure Kubernetes Service (AKS) 中的叢集。

Microsoft Azure 的先決條件​

警告

部署到 AKS 將產生費用。

要與 Azure API 互動,AKS 叢集需要一個 Azure Active Directory (AD) 服務主體。服務主體需要動態建立和管理其他 Azure 資源,並為您的叢集提供與 AKS 通訊的憑證。有關服務主體的更多資訊,請參閱 AKS 文件。

在建立服務主體之前,您需要從 Microsoft Azure Portal 取得以下資訊:

  • 訂閱 ID
  • 用戶端 ID(也稱為應用程式 ID)
  • 用戶端密碼

以下各節說明如何使用 Azure 命令列工具或 Azure 入口網站設定這些先決條件。

使用 Azure 命令列工具設定服務主體​

您必須為服務主體分配角色,以便它具有與 AKS API 的通訊權限。它還需要存取以建立和列出虛擬網路。

在以下範例中,該命令建立服務主體並授予它 Contributor 角色。Contributor 角色可以管理 AKS 上的任何內容,但無法授予他人存取權限。請注意,您必須為 scopes 提供至少一個 Azure 資源的完整路徑:

az ad sp create-for-rbac --role Contributor --scopes /subscriptions/<subscription-id>/resourceGroups/<resource-group-name>

結果應顯示有關新服務主體的資訊:

{
"appId": "xxxx--xxx",
"displayName": "<service-principal-name>",
"name": "http://<service-principal-name>",
"password": "<secret>",
"tenant": "<tenant-name>"
}

以下建立一個資源群組以包含您的 Azure 資源:

az group create --location <azure-location-name> --resource-group <resource-group-name>

從 Azure 入口網站設定服務主體​

按照以下說明從 Azure 入口網站設定服務主體並授予它基於角色的存取權限。

  1. 前往 Microsoft Azure Portal 首頁。
  2. 按一下 Azure Active Directory。
  3. 按一下 App registrations。
  4. 按一下 New registration。
  5. 為您的服務主體輸入名稱。
  6. 選擇性:選擇哪些帳戶可以使用服務主體。
  7. 按一下 Register。
  8. 您現在應該會在 Azure Active Directory > App registrations 下看到您的服務主體名稱。
  9. 按一下您的服務主體名稱。記下應用程式 ID(也稱為應用程式 ID 或用戶端 ID),以便您在配置 AKS 叢集時使用。然後按一下 Certificates & secrets。
  10. 按一下 New client secret。
  11. 輸入簡短描述,選擇過期時間,然後按一下 Add。記下用戶端密碼,以便您在配置 AKS 叢集時使用。

結果: 您已建立一個服務主體,您應該能夠在 Azure Active Directory 區段的 App registrations 下看到它列出。您仍需要授予服務主體存取 AKS 的權限。

要授予服務主體基於角色的存取權限,

  1. 在左側導航欄中按一下 All Services。然後按一下 Subscriptions。
  2. 按一下您要與 Kubernetes 叢集關聯的訂閱名稱。記下訂閱 ID,以便您在配置 AKS 叢集時使用。
  3. 按一下 Access Control (IAM)。
  4. 在 Add role assignment 區段中,按一下 Add。
  5. 在 Role 欄位中,選擇將有權存取 AKS 的角色。例如,您可以使用 Contributor 角色,該角色有權管理除了授予其他使用者存取權限之外的所有內容。
  6. 在 Assign access to 欄位中,選擇 Azure AD user, group, or service principal。
  7. 在 Select 欄位中,選擇您的服務主體名稱,然後按一下 Save。

結果: 您的服務主體現在可以存取 AKS。

建立 AKS 雲端憑證​

  1. 在 Rancher UI 中,按一下 ☰ > Cluster Management。
  2. 按一下 Cloud Credentials。
  3. 按一下 Create。
  4. 按一下 Azure。
  5. 填寫表單。有關填寫表單的說明,請參閱配置參考。
  6. 按一下 Create。

建立 AKS 叢集​

使用 Rancher 設定和配置您的 Kubernetes 叢集。

  1. 按一下 ☰ > Cluster Management。
  2. 在 Clusters 區段中,按一下 Create。
  3. 按一下 Azure AKS。
  4. 填寫表單。有關填寫表單的說明,請參閱配置參考。
  5. 按一下 Create。

結果: 您的叢集已建立並指派為 Provisioning 狀態。Rancher 正在建立您的叢集。

您的叢集狀態更新為 Active 後,即可存取它。

配置基於角色的存取控制​

在 Rancher UI 中配置 AKS 叢集時,RBAC 無法配置,因為必須啟用它。

對於註冊或匯入到 Rancher 的 AKS 叢集,RBAC 是必需的。

使用 Azure 命令列工具設定服務主體的角色分配​

使用 Azure 命令列工具將 Rancher AKSv2 角色分配給服務主體:

az role assignment create \
--assignee <client-id> \
--scope "/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>" \
--role "Rancher AKSv2"

AKS 叢集配置參考​

有關如何從 Rancher UI 配置 AKS 叢集的更多資訊,請參閱配置參考。

私有叢集​

通常,AKS worker 節點不會獲得公用 IP,無論叢集是否為私有叢集。在私有叢集中,控制平面沒有公用端點。

Rancher 可以透過兩種方式之一連接到私有 AKS 叢集。

第一種方式是確保 Rancher 在與 AKS 節點相同的 NAT 上執行。

第二種方式是執行命令將叢集註冊到 Rancher。叢集配置完成後,您可以在可以連接到叢集 Kubernetes API 的任何地方執行顯示的命令。當您配置啟用了私有 API 端點的 AKS 叢集時,此命令會顯示在彈出視窗中。

備註

請注意,在註冊現有 AKS 叢集時,叢集可能需要一些時間(可能數小時)才會出現在 Cluster To register 下拉清單中。這個結果將取決於區域。

有關連接到 AKS 私有叢集的更多資訊,請參閱 AKS 文件。

使用 Azure 命令列工具設定最小權限角色​

  1. 執行以下命令建立最小 Rancher AKSv2 權限角色:
cat >> rancher-azure.json << EOF

{
"Name": "Rancher AKSv2",
"IsCustom": true,
"Description": "Everything needed by Rancher AKSv2 operator",
"Actions": [
"Microsoft.Compute/disks/delete",
"Microsoft.Compute/disks/read",
"Microsoft.Compute/disks/write",
"Microsoft.Compute/diskEncryptionSets/read",
"Microsoft.Compute/locations/DiskOperations/read",
"Microsoft.Compute/locations/vmSizes/read",
"Microsoft.Compute/locations/operations/read",
"Microsoft.Compute/proximityPlacementGroups/write",
"Microsoft.Compute/snapshots/delete",
"Microsoft.Compute/snapshots/read",
"Microsoft.Compute/snapshots/write",
"Microsoft.Compute/virtualMachineScaleSets/manualUpgrade/action",
"Microsoft.Compute/virtualMachineScaleSets/delete",
"Microsoft.Compute/virtualMachineScaleSets/read",
"Microsoft.Compute/virtualMachineScaleSets/virtualMachines/networkInterfaces/read",
"Microsoft.Compute/virtualMachineScaleSets/virtualMachines/networkInterfaces/ipconfigurations/publicipaddresses/read",
"Microsoft.Compute/virtualMachineScaleSets/virtualmachines/instanceView/read",
"Microsoft.Compute/virtualMachineScaleSets/virtualMachines/read",
"Microsoft.Compute/virtualMachineScaleSets/virtualMachines/write",
"Microsoft.Compute/virtualMachineScaleSets/write",
"Microsoft.Compute/virtualMachines/read",
"Microsoft.Compute/virtualMachines/write",
"Microsoft.ContainerService/managedClusters/read",
"Microsoft.ContainerService/managedClusters/write",
"Microsoft.ContainerService/managedClusters/delete",
"Microsoft.ContainerService/managedClusters/accessProfiles/listCredential/action",
"Microsoft.ContainerService/managedClusters/agentPools/read",
"Microsoft.ContainerService/managedClusters/agentPools/write",
"Microsoft.ContainerService/managedClusters/agentPools/delete",
"Microsoft.ManagedIdentity/userAssignedIdentities/assign/action",
"Microsoft.Network/applicationGateways/read",
"Microsoft.Network/applicationGateways/write",
"Microsoft.Network/loadBalancers/write",
"Microsoft.Network/loadBalancers/backendAddressPools/join/action",
"Microsoft.Network/loadBalancers/delete",
"Microsoft.Network/loadBalancers/read",
"Microsoft.Network/networkInterfaces/join/action",
"Microsoft.Network/networkInterfaces/read",
"Microsoft.Network/networkInterfaces/write",
"Microsoft.Network/networkSecurityGroups/read",
"Microsoft.Network/networkSecurityGroups/write",
"Microsoft.Network/publicIPAddresses/delete",
"Microsoft.Network/publicIPAddresses/join/action",
"Microsoft.Network/publicIPAddresses/read",
"Microsoft.Network/publicIPAddresses/write",
"Microsoft.Network/publicIPPrefixes/join/action",
"Microsoft.Network/privatednszones/*",
"Microsoft.Network/routeTables/read",
"Microsoft.Network/routeTables/routes/delete",
"Microsoft.Network/routeTables/routes/read",
"Microsoft.Network/routeTables/routes/write",
"Microsoft.Network/routeTables/write",
"Microsoft.Network/virtualNetworks/read",
"Microsoft.Network/virtualNetworks/subnets/join/action",
"Microsoft.Network/virtualNetworks/subnets/read",
"Microsoft.Network/virtualNetworks/joinLoadBalancer/action",
"Microsoft.OperationalInsights/workspaces/sharedkeys/read",
"Microsoft.OperationalInsights/workspaces/read",
"Microsoft.OperationsManagement/solutions/write",
"Microsoft.OperationsManagement/solutions/read",
"Microsoft.Resources/subscriptions/resourcegroups/read",
"Microsoft.Resources/subscriptions/resourcegroups/write",
"Microsoft.Storage/operations/read",
"Microsoft.Storage/storageAccounts/listKeys/action",
"Microsoft.Storage/storageAccounts/delete",
"Microsoft.Storage/storageAccounts/read",
"Microsoft.Storage/storageAccounts/write"
],
"NotActions": [],
"DataActions": [],
"NotDataActions": [],
"AssignableScopes": [
"/subscriptions/SUBSCRIPTION_ID"
]
}
EOF
  1. 套用 Rancher AKSv2 角色:
az role definition create --role-definition rancher-azure.json
  1. 驗證是否已建立 Rancher AKSv2 角色:
az role definition list | grep "Rancher AKSv2"

同步​

AKS 配置器可以在 Rancher 和提供者之間同步 AKS 叢集的狀態。有關其運作方式的深入技術說明,請參閱同步。

有關配置重新整理間隔的資訊,請參閱本節。

以程式設計方式建立 AKS 叢集​

透過 Rancher 以程式設計方式部署 AKS 叢集的最常見方式是使用 Rancher2 Terraform 提供者。使用 Terraform 建立叢集的文件在這裡。