適用於 Kubernetes 的 Azure 角色型存取控制授權

當您利用 Microsoft Entra ID 與 AKS 之間的整合式驗證時,您可以使用 Microsoft Entra 使用者、群組或服務主體作為 Kubernetes 角色型存取控制 (Kubernetes RBAC) 中的主體。 此功能可讓您不必個別管理 Kubernetes 的使用者身分識別和認證。 不過,您仍然需要個別設定和管理 Azure RBAC 和 Kubernetes RBAC。

本文說明如何使用適用於 Kubernetes 授權的 Azure RBAC,以允許跨 Azure 資源、AKS 和 Kubernetes 資源進行統一管理和存取控制。 如需詳細資訊,請參閱 適用於 Kubernetes 授權的 Azure RBAC 授權

開始之前

  • 您必須安裝並設定 Azure CLI 2.24.0 版或更新版本。 執行 az --version 以尋找版本。 如果您需要安裝或升級,請參閱安裝 Azure CLI
  • 您需要最低版本為 1.18.3kubectl
  • 您需要在叢集上啟用受控 Microsoft Entra 整合,才能新增適用於 Kubernetes 的 Azure RBAC 授權。 如果您需要啟用受控 Microsoft Entra 整合,請參閱 在 AKS 中使用 Microsoft Entra ID
  • 如果您有 CRD,並進行自訂角色定義,則目前涵蓋 CRD 的唯一方法是使用 Microsoft.ContainerService/managedClusters/*/read。 對於其餘物件,您可以使用特定的 API 群組,例如:Microsoft.ContainerService/apps/deployments/read
  • 新的角色指派最多可能需要五分鐘才能傳播,並由授權伺服器更新。
  • 適用於 Kubernetes 的 Azure RBAC 授權要求針對驗證設定的 Microsoft Entra 租用戶,與保留 AKS 叢集之訂用帳戶的租用戶相同。

使用受控 Microsoft Entra 整合和適用於 Kubernetes 的 RBAC 授權,建立新的 AKS 叢集

使用 az group create 命令建立 Azure 資源群組。

az group create --name myResourceGroup --location westus2

使用 az aks create 命令搭配受控 Microsoft Entra 整合和適用於 Kubernetes 的 RBAC 授權,建立 AKS 叢集。

az aks create -g myResourceGroup -n myManagedCluster --enable-aad --enable-azure-rbac

輸出看起來會與下列範例輸出類似:

"AADProfile": {
    "adminGroupObjectIds": null,
    "clientAppId": null,
    "enableAzureRbac": true,
    "managed": true,
    "serverAppId": null,
    "serverAppSecret": null,
    "tenantId": "****-****-****-****-****"
}

在現有的 AKS 叢集上啟用 Azure RBAC

若要將適用於 Kubernetes 的 Azure RBAC 授權加入現有的 AKS 叢集,請使用 az aks updateaz aks updateenable-azure-rbac 命令搭配旗標。

az aks update -g myResourceGroup -n myAKSCluster --enable-azure-rbac

將適用於 Kubernetes 的 Azure RBAC 授權從 AKS 叢集停用

若要將適用於 Kubernetes 的 Azure RBAC 授權從現有的 AKS 叢集中移除,請使用 az aks update 命令搭配 disable-azure-rbac 旗標。

az aks update -g myResourceGroup -n myAKSCluster --disable-azure-rbac

建立角色指派,以便使用者存取叢集

AKS 提供下列內建角色:

角色 描述
Azure Kubernetes Service RBAC 讀取者 允許唯讀存取來查看命名空間中的大部分物件。 其不允許檢視角色或角色繫結。 此角色不允許檢視 Secrets,因為讀取祕密的內容可存取命名空間中的 ServiceAccount 認證,這會允許 API 以命名空間中任何 ServiceAccount 身分來存取 (權限提升的形式)。
Azure Kubernetes Service RBAC 寫入者 允許命名空間中大部分物件的讀取/寫入存取權。 不允許檢視或修改角色或角色繫結。 然而,此角色允許以命名空間中的任何 ServiceAccount 身分存取 Secrets 和執行 Pod,因此可用來取得命名空間中任何 ServiceAccount 的 API 存取層級。
Azure Kubernetes Service RBAC 管理員 允許管理員存取權,其用途是在命名空間內授與。 允許命名空間 (或叢集範圍) 中大部分的資源的讀取/寫入存取權 ,包括可在命名空間內建立角色和角色繫結。 此角色不允許對資源配額或命名空間本身進行寫入存取。
Azure Kubernetes Service RBAC 叢集管理員 允許進階使用者在任何資源上執行的存取權。。 這會提供叢集中和全部命名空間中每個資源的完整控制權。

範圍設為整個 AKS 叢集的角色指派可以在 Azure 入口網站上叢集資源的 [存取控制 (IAM)] 刀鋒視窗中完成,或使用下列 Azure CLI 命令完成:

使用 az aks show 命令取得您的 AKS 資源識別碼。

AKS_ID=$(az aks show -g myResourceGroup -n myManagedCluster --query id -o tsv)

使用 az role assignment create 命令建立角色指派。 <AAD-ENTITY-ID> 可以是服務主體的使用者名稱或用戶端識別碼。

az role assignment create --role "Azure Kubernetes Service RBAC Admin" --assignee <AAD-ENTITY-ID> --scope $AKS_ID

注意

您可以使用 az role assignment create 命令將 Azure Kubernetes Service RBAC 讀取器Azure Kubernetes Service RBAC 寫入器建立在叢集內特定命名空間,並設定所需命名空間的範圍。

az role assignment create --role "Azure Kubernetes Service RBAC Reader" --assignee <AAD-ENTITY-ID> --scope $AKS_ID/namespaces/<namespace-name>

注意

在 Azure 入口網站中,建立限定為所需命名空間的角色指派之後,您將無法在範圍 看到命名空間 的「角色指派」。 您可以使用 az role assignment list 命令來找到它,或為使用者或群組列出角色指派,也就是您指派角色的地方。

az role assignment list --scope $AKS_ID/namespaces/<namespace-name>

建立自訂角色定義

以下是自訂角色的範例,可讓使用者唯讀部署,而不讀取其他內容。 如需可能動作的完整清單,請參閱 Microsoft.ContainerService 作業

若要建立您自己的自訂角色定義,請複製下列檔案,將 <YOUR SUBSCRIPTION ID> 取代為您自己的訂用帳戶識別碼,然後將它儲存為 deploy-view.json

{
    "Name": "AKS Deployment Reader",
    "Description": "Lets you view all deployments in cluster/namespace.",
    "Actions": [],
    "NotActions": [],
    "DataActions": [
        "Microsoft.ContainerService/managedClusters/apps/deployments/read"
    ],
    "NotDataActions": [],
    "assignableScopes": [
        "/subscriptions/<YOUR SUBSCRIPTION ID>"
    ]
}

使用 az role definition create 命令建立角色定義,將 --role-definition 設定為您在上一個步驟中建立的 deploy-view.json 檔案。

az role definition create --role-definition @deploy-view.json 

使用 az role assignment create 命令,將角色定義指派給使用者或其他身分識別。

az role assignment create --role "AKS Deployment Reader" --assignee <AAD-ENTITY-ID> --scope $AKS_ID

將 Azure RBAC for Kubernetes Authorization 與 kubectl 搭配使用

請確定您有 Azure Kubernetes Service 叢集使用者內建角色,然後使用 az aks get-credentials 命令取得 AKS 叢集的 kubeconfig。

az aks get-credentials -g myResourceGroup -n myManagedCluster

現在,您可以使用 kubectl 管理叢集了。 例如,您可以使用 kubectl get nodes 命令,列出叢集中的節點。 第一次執行時,您必須登入,如下列範例所示:

kubectl get nodes
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code AAAAAAAAA to authenticate.

NAME                                STATUS   ROLES   AGE    VERSION
aks-nodepool1-93451573-vmss000000   Ready    agent   3h6m   v1.15.11
aks-nodepool1-93451573-vmss000001   Ready    agent   3h6m   v1.15.11
aks-nodepool1-93451573-vmss000002   Ready    agent   3h6m   v1.15.11

將 Azure RBAC for Kubernetes Authorization 與 kubelogin 搭配使用

AKS 建立 kubelogin 外掛程式協助解除封鎖其他案例,例如非互動式登入、較舊的 kubectl 版本,或跨多個叢集利用 SSO,而不需要登入新的叢集。

您可以執行下列命令來使用 kubelogin 外掛程式:

export KUBECONFIG=/path/to/kubeconfig
kubelogin convert-kubeconfig

如同 kubectl,第一次執行需要登入,如下列範例所示:

kubectl get nodes
To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code AAAAAAAAA to authenticate.

NAME                                STATUS   ROLES   AGE    VERSION
aks-nodepool1-93451573-vmss000000   Ready    agent   3h6m   v1.15.11
aks-nodepool1-93451573-vmss000001   Ready    agent   3h6m   v1.15.11
aks-nodepool1-93451573-vmss000002   Ready    agent   3h6m   v1.15.11

清除資源

刪除角色指派

# List role assignments
az role assignment list --scope $AKS_ID --query [].id -o tsv

# Delete role assignments
az role assignment delete --ids <LIST OF ASSIGNMENT IDS>

刪除角色定義

az role definition delete -n "AKS Deployment Reader"

刪除資源群組和 AKS 叢集

az group delete -n myResourceGroup

下一步

若要深入了解 AKS Authentication、Authorization、Kubernetes RBAC 和 Azure RBAC,請參閱: