使用 Azure 角色型存取控制來定義 Azure Kubernetes Service (AKS) 中的 Kubernetes 組態檔存取權

您可以使用 kubectl 工具與 Kubernetes 叢集互動。 Azure CLI 可讓您使用 kubectl 輕鬆地存取連線到 AKS 叢集所需的認證和 kubeconfig 組態檔。 您可以使用 Azure 角色型存取控制 (Azure RBAC) 來限制誰可以存取 kubeconfig 檔案及其擁有的許可權。

本文將說明如何指派 Azure 角色,以限制誰可以取得 AKS 叢集的組態資訊。

開始之前

  • 此文章假設您目前具有 AKS 叢集。 如果您需要 AKS 叢集,則請使用 Azure CLIAzure PowerShellAzure 入口網站予以建立。
  • 本文也需要您執行 Azure CLI 2.0.65 版或更新版本。 執行 az --version 以尋找版本。 如果您需要安裝或升級,請參閱安裝 Azure CLI

叢集角色可用的權限

當您使用 kubectl 工具與 AKS 叢集互動時,稱為 kubecongif 的組態檔會定義叢集連線資訊。 此組態檔通常會儲存在 ~/.kube/config。此 kubeconfig 檔案中可定義多個叢集。 您可以使用 kubectl config use-context 命令在叢集之間切換。

az aks get-credentials 命令可讓您取得 AKS 叢集的存取認證,並將其合併至 kubeconfig 檔案。 您可以使用 Azure RBAC 來控制這些認證的存取權。 這些 Azure 角色可讓您定義誰可以擷取 kubeconfig 檔案,以及他們在叢集內具有的權限。

有 2 種 Azure 角色可讓您套用至 Microsoft Entra 使用者或群組:

  • Azure Kubernetes Service 叢集管理員角色

    • 允許存取 Microsoft.ContainerService/managedClusters/listClusterAdminCredential/action API 呼叫。 此 API 呼叫會 列出叢集管理員認證
    • 下載 clusterAdmin 角色的 kubeconfig
  • Azure Kubernetes Service 叢集使用者角色

    • 允許存取 Microsoft.ContainerService/managedClusters/listClusterUserCredential/action API 呼叫。 此 API 呼叫會列出叢集使用者認證
    • 下載 clusterUser 角色的 kubeconfig

注意

在使用 Microsoft Entra ID 的叢集上,擁有 clusterUser 角色的使用者會有提示登入的空 kubeconfig 檔案。 登入之後,使用者可以根據 Microsoft Entra 使用者或群組設定來存取。 擁有 clusterAdmin 角色的使用者擁有管理員存取權。

在不使用 Microsoft Entra ID 的叢集上,clusterUser 角色的效果與 clusterAdmin 角色相同。

指派角色權限給使用者或群組

若要將其中一個可用的角色指派給使用者,您需要取得 AKS 叢集的資源識別碼和 Microsoft Entra 使用者帳戶或群組的識別碼,請使用下列步驟:

  1. myResourceGroup 資源群組中名為 myAKSCluster 的叢集使用 az aks show命令,以取得叢集資源識別碼。 如有需要,請提供您自己的叢集和資源群組名稱。
  2. 使用 az account showaz ad user show 命令來取得您的使用者識別碼。
  3. 使用 az role assignment create 命令指派角色。

下列範例會將「Azure Kubernetes Service 叢集管理員角色」指派給個人使用者帳戶:

# Get the resource ID of your AKS cluster
AKS_CLUSTER=$(az aks show --resource-group myResourceGroup --name myAKSCluster --query id -o tsv)

# Get the account credentials for the logged in user
ACCOUNT_UPN=$(az account show --query user.name -o tsv)
ACCOUNT_ID=$(az ad user show --id $ACCOUNT_UPN --query objectId -o tsv)

# Assign the 'Cluster Admin' role to the user
az role assignment create \
    --assignee $ACCOUNT_ID \
    --scope $AKS_CLUSTER \
    --role "Azure Kubernetes Service Cluster Admin Role"

如果您想要將許可權指派給 Microsoft Entra 群組,請使用群組的物件識別碼來更新上一個範例中顯示的 --assignee 參數,而不是使用者

若要取得群組的物件識別碼,請使用 az ad group show 命令。 下列命令會取得名稱為 appdev 的 Microsoft Entra 群組所用的物件識別碼:

az ad group show --group appdev --query objectId -o tsv

重要

在某些情況下,帳戶中的 user.nameuserPrincipalName 不同,例如使用 Microsoft Entra 來賓使用者。

$ az account show --query user.name -o tsv
user@contoso.com

$ az ad user list --query "[?contains(otherMails,'user@contoso.com')].{UPN:userPrincipalName}" -o tsv
user_contoso.com#EXT#@contoso.onmicrosoft.com

在此情況下,請將 ACCOUNT_UPN 的值設定為 Microsoft Entra 使用者的 userPrincipalName。 例如,如果您的帳戶 user.nameuser@contoso.com,則此動作看起來會像下列範例:

ACCOUNT_UPN=$(az ad user list --query "[?contains(otherMails,'user@contoso.com')].{UPN:userPrincipalName}" -o tsv)

取得及驗證組態資訊

指派角色後,使用 az aks get-credentials 命令來取得您 AKS 叢集的 kubeconfig 定義。 下列範例會取得 --admin 認證,如果使用者已獲派叢集管理員角色,則認證可正常運作:

az aks get-credentials --resource-group myResourceGroup --name myAKSCluster --admin

接著,您可以使用 kubectl config view 命令,確認叢集的內容會顯示已套用管理員組態資訊。

$ kubectl config view

您的輸出看起來應類似下列的範例輸出:

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://myaksclust-myresourcegroup-19da35-4839be06.hcp.eastus.azmk8s.io:443
  name: myAKSCluster
contexts:
- context:
    cluster: myAKSCluster
    user: clusterAdmin_myResourceGroup_myAKSCluster
  name: myAKSCluster-admin
current-context: myAKSCluster-admin
kind: Config
preferences: {}
users:
- name: clusterAdmin_myResourceGroup_myAKSCluster
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED
    token: e9f2f819a4496538b02cefff94e61d35

移除角色權限

若要移除角色指派,請使用 az role assignment delete 命令。 指定您在先前步驟中取得的帳戶識別碼和叢集資源識別碼。 如果您將角色指派給群組而非使用者,請指定適當的群組物件識別碼,而不是參數的 --assignee 帳戶物件識別碼。

az role assignment delete --assignee $ACCOUNT_ID --scope $AKS_CLUSTER

下一步

若要加強 AKS 叢集存取的安全性,請整合 Microsoft Entra 驗證