AKS 管理的Azure Active Directory整合
AKS 管理的 Azure AD 整合簡化了 Azure AD 整合程序。 先前,使用者必須建立用戶端和伺服器應用程式,並要求Azure AD租使用者授與目錄讀取權限。 在新版本中,AKS 資源提供者會為您管理用戶端和伺服器應用程式。
Azure AD驗證概觀
叢集管理員可以根據使用者的身分識別或目錄群組成員資格,設定 Kubernetes 角色型存取控制 (Kubernetes RBAC) 。 透過 OpenID Connect 對 AKS 叢集提供 Azure AD 驗證。 OpenID Connect 是以 OAuth 2.0 通訊協定為建置基礎的身分識別層。 如需 OpenID 連線的詳細資訊,請參閱Open ID connect 檔。
深入瞭解Azure Active Directory整合概念檔上的Azure AD整合流程。
限制
- 無法停用 AKS 管理的Azure AD整合
- 不支援將 AKS 管理的Azure AD整合式叢集變更為舊版AAD
- AKS 受控Azure AD整合不支援未啟用 Kubernetes RBAC 的叢集
必要條件
重要
您必須使用 Kubectl,最低版本為 1.18.1 或 kubelogin。 Kubernetes 和 kubectl 次要版本之間的差異不應超過 1 個版本。 如果您未使用正確的版本,您會注意到驗證問題。
若要安裝 kubectl 和 kubelogin,請使用下列命令:
sudo az aks install-cli
kubectl version --client
kubelogin --version
針對其他作業系統使用這些 指示 。
開始之前
針對叢集,您需要Azure AD群組。 此群組將會註冊為叢集上的系統管理員群組,以授與叢集管理員許可權。 您可以使用現有的Azure AD群組,或建立新的群組。 記錄Azure AD群組的物件識別碼。
# List existing groups in the directory
az ad group list --filter "displayname eq '<group-name>'" -o table
若要為叢集系統管理員建立新的Azure AD群組,請使用下列命令:
# Create an Azure AD group
az ad group create --display-name myAKSAdminGroup --mail-nickname myAKSAdminGroup
建立已啟用Azure AD的 AKS 叢集
使用下列 CLI 命令建立 AKS 叢集。
建立 Azure 資源群組:
# Create an Azure resource group
az group create --name myResourceGroup --location centralus
建立 AKS 叢集,並啟用Azure AD群組的系統管理存取權
# Create an AKS-managed Azure AD cluster
az aks create -g myResourceGroup -n myManagedCluster --enable-aad --aad-admin-group-object-ids <id> [--aad-tenant-id <id>]
成功建立 AKS 受控Azure AD叢集的回應本文中有下一節
"AADProfile": {
"adminGroupObjectIds": [
"5d24****-****-****-****-****afa27aed"
],
"clientAppId": null,
"managed": true,
"serverAppId": null,
"serverAppSecret": null,
"tenantId": "72f9****-****-****-****-****d011db47"
}
建立叢集之後,您就可以開始存取它。
存取已啟用Azure AD叢集
使用Azure AD定義群組存取叢集之前,您需要Azure Kubernetes Service叢集使用者內建角色。
取得使用者認證以存取叢集:
az aks get-credentials --resource-group myResourceGroup --name myManagedCluster
請依照指示登入。
使用 kubectl get nodes 命令來檢視叢集中的節點:
kubectl get nodes
NAME STATUS ROLES AGE VERSION
aks-nodepool1-15306047-0 Ready agent 102m v1.15.10
aks-nodepool1-15306047-1 Ready agent 102m v1.15.10
aks-nodepool1-15306047-2 Ready agent 102m v1.15.10
設定 Azure 角色型存取控制 (Azure RBAC) 來設定叢集的其他安全性群組。
針對 Azure AD 的存取問題進行疑難排解
重要
以下所述的步驟會略過一般Azure AD群組驗證。 請只在緊急狀況中使用。
如果您永久遭到封鎖,因為無法存取具有叢集存取權的有效Azure AD群組,您仍然可以取得系統管理員認證,直接存取叢集。
若要執行這些步驟,您必須能夠存取Azure Kubernetes Service叢集管理員內建角色。
az aks get-credentials --resource-group myResourceGroup --name myManagedCluster --admin
在您的現有叢集上啟用 AKS 管理的Azure AD整合
您可以在已啟用現有 Kubernetes RBAC 的叢集上啟用 AKS 管理的Azure AD整合。 請務必設定系統管理員群組,以保留叢集的存取權。
az aks update -g MyResourceGroup -n MyManagedCluster --enable-aad --aad-admin-group-object-ids <id-1> [--aad-tenant-id <id>]
成功啟用 AKS 管理的Azure AD叢集,在回應本文中有下一節
"AADProfile": {
"adminGroupObjectIds": [
"5d24****-****-****-****-****afa27aed"
],
"clientAppId": null,
"managed": true,
"serverAppId": null,
"serverAppSecret": null,
"tenantId": "72f9****-****-****-****-****d011db47"
}
遵循 這裡的步驟,再次下載使用者認證以存取您的叢集。
升級至 AKS 管理的 Azure AD 整合
如果您的叢集使用舊版Azure AD整合,您可以升級至 AKS 管理的 Azure AD 整合。
az aks update -g myResourceGroup -n myManagedCluster --enable-aad --aad-admin-group-object-ids <id> [--aad-tenant-id <id>]
成功移轉 AKS 受控Azure AD叢集的回應本文中有下一節
"AADProfile": {
"adminGroupObjectIds": [
"5d24****-****-****-****-****afa27aed"
],
"clientAppId": null,
"managed": true,
"serverAppId": null,
"serverAppSecret": null,
"tenantId": "72f9****-****-****-****-****d011db47"
}
更新 kubeconfig 以存取叢集,請遵循 這裡的步驟。
使用 kubelogin 的非互動式登入
有一些非互動式案例,例如持續整合管線,目前不適用於 kubectl。 您可以使用 kubelogin
來存取具有非互動式服務主體登入的叢集。
停用本機帳戶
部署 AKS 叢集時,預設會啟用本機帳戶。 即使啟用 RBAC 或Azure Active Directory整合, --admin
存取仍然存在,基本上是不可稽核的備份選項。 請記住,AKS 可讓使用者透過旗標停用本機帳戶。 disable-local-accounts
properties.disableLocalAccounts
欄位也會新增至受控叢集 API,以指出是否已在叢集上啟用此功能。
注意
在已啟用Azure AD整合的叢集上,屬於 所 aad-admin-group-object-ids
指定群組的使用者仍然可以透過非系統管理員認證取得存取權。 在未啟用Azure AD整合且 properties.disableLocalAccounts
設為 true 的叢集上,取得使用者和系統管理員認證將會失敗。
注意
在使用者可能已使用本機帳戶/秒的現有 AKS 叢集上停用本機帳戶使用者之後,系統管理員必須 輪替叢集憑證,才能撤銷這些使用者可能有權存取的憑證。 如果這是新的叢集,則不需要採取任何動作。
建立沒有本機帳戶的新叢集
若要在沒有任何本機帳戶的情況下建立新的 AKS 叢集,請使用 az aks create 命令搭配 disable-local-accounts
旗標:
az aks create -g <resource-group> -n <cluster-name> --enable-aad --aad-admin-group-object-ids <aad-group-id> --disable-local-accounts
在輸出中,檢查欄位 properties.disableLocalAccounts
已設定為 true 來確認本機帳戶已停用:
"properties": {
...
"disableLocalAccounts": true,
...
}
嘗試取得系統管理員認證將會失敗,並出現錯誤訊息,指出此功能無法存取:
az aks get-credentials --resource-group <resource-group> --name <cluster-name> --admin
Operation failed with status: 'Bad Request'. Details: Getting static credential is not allowed because this cluster is set to disable local accounts.
停用現有叢集上的本機帳戶
若要停用現有 AKS 叢集上的本機帳戶,請使用 az aks update 命令搭配 disable-local-accounts
旗標:
az aks update -g <resource-group> -n <cluster-name> --enable-aad --aad-admin-group-object-ids <aad-group-id> --disable-local-accounts
在輸出中,檢查欄位 properties.disableLocalAccounts
已設定為 true 來確認本機帳戶已停用:
"properties": {
...
"disableLocalAccounts": true,
...
}
嘗試取得系統管理員認證將會失敗,並出現錯誤訊息,指出此功能無法存取:
az aks get-credentials --resource-group <resource-group> --name <cluster-name> --admin
Operation failed with status: 'Bad Request'. Details: Getting static credential is not allowed because this cluster is set to disable local accounts.
重新啟用現有叢集上的本機帳戶
AKS 也可讓您使用 旗標重新啟用現有叢集 enable-local
上的本機帳戶:
az aks update -g <resource-group> -n <cluster-name> --enable-aad --aad-admin-group-object-ids <aad-group-id> --enable-local
在輸出中,確認已重新啟用本機帳戶,方法是檢查欄位 properties.disableLocalAccounts
設定為 false:
"properties": {
...
"disableLocalAccounts": false,
...
}
嘗試取得系統管理員認證將會成功:
az aks get-credentials --resource-group <resource-group> --name <cluster-name> --admin
Merged "<cluster-name>-admin" as current context in C:\Users\<username>\.kube\config
搭配 Azure AD 和 AKS 使用條件式存取
將Azure AD與您的 AKS 叢集整合時,您也可以使用條件式存取來控制叢集的存取。
注意
Azure AD條件式存取是Azure AD Premium功能。
若要建立要搭配 AKS 使用的條件式存取原則範例,請完成下列步驟:
- 在Azure 入口網站頂端,搜尋並選取 [Azure Active Directory]。
- 在左側Azure Active Directory功能表中,選取[Enterprise應用程式]。
- 在左側Enterprise應用程式的功能表中,選取 [條件式存取]。
- 在左側的條件式存取功能表中,選取 [ 原則 ] 和 [ 新增原則]。
- 輸入原則的名稱,例如 aks-policy。
- 選取 [使用者和群組],然後在 [ 包含 ] 下選取 [選取使用者和群組]。 選擇要套用原則的使用者和群組。 在此範例中,請選擇具有叢集管理存取權的相同Azure AD群組。
- 選取 [雲端應用程式或動作],然後在 [ 包含 ] 下選取 [ 選取應用程式]。 搜尋Azure Kubernetes Service,然後選取[Azure Kubernetes Service AAD伺服器]。
- 在 [存取控制] 底下,選取 [授與] 。 選取 [授與存取 權],然後 [要求裝置標示為相容]。
- 在 [啟用原則] 底下,選取 [ 開啟 ] 和 [ 建立]。
取得使用者認證以存取叢集,例如:
az aks get-credentials --resource-group myResourceGroup --name myManagedCluster
請依照指示登入。
kubectl get nodes
使用 命令來檢視叢集中的節點:
kubectl get nodes
請依照指示再次登入。 請注意,有錯誤訊息指出您已成功登入,但您的系統管理員需要要求存取權的裝置由您的Azure AD管理,才能存取資源。
在Azure 入口網站中,流覽至 [Azure Active Directory],選取[Enterprise應用程式],然後在 [活動] 底下選取 [登入]。請注意頂端的專案,其狀態為[失敗] 和 [條件式存取成功]。 選取專案,然後在[詳細資料] 中選取[條件式存取]。 請注意,您的條件式存取原則已列出。
使用 Azure AD 和 AKS 設定 Just-In-Time 叢集存取
叢集存取控制的另一個選項是針對 Just-In-Time 要求使用Privileged Identity Management (PIM) 。
注意
PIM 是需要 進階版 P2 SKU 的Azure AD Premium功能。 如需Azure AD SKU 的詳細資訊,請參閱定價指南。
若要使用 AKS 管理的 Azure AD 整合,將 Just-In-Time 存取要求與 AKS 叢集整合,請完成下列步驟:
- 在Azure 入口網站頂端,搜尋並選取 [Azure Active Directory]。
- 記下租使用者識別碼,以取得這些指示的其餘部分稱為
<tenant-id>
- 在左側Azure Active Directory功能表的 [管理] 底下,選取 [群組] 和 [新增群組]。
- 請確定已選取 [ 安全性 的群組類型],並輸入組名,例如 myJITGroup。 在[Azure AD角色可以指派給此群組 (Preview) ],選取 [是]。 最後,選取 [建立]。
- 您會回到 [ 群組 ] 頁面。 選取新建立的群組,並記下 [物件識別碼],這些指示的其餘部分
<object-id>
稱為 。 - 使用
<tenant-id>
先前的 和<object-id>
值,部署 AKS 叢集與 AKS 管理的Azure AD整合:az aks create -g myResourceGroup -n myManagedCluster --enable-aad --aad-admin-group-object-ids <object-id> --aad-tenant-id <tenant-id>
- 回到Azure 入口網站,在左側的 [活動] 功能表中,選取 [特殊許可權存取] ([預覽) ],然後選取 [啟用特殊許可權存取]。
- 選取 [新增指派 ] 以開始授與存取權。
- 選取 成員的角色,然後選取您想要授與叢集存取權的使用者和群組。 群組管理員可以隨時修改這些指派。當您準備好繼續進行時,請選取 [ 下一步]。
- 選擇 [ 作用中] 的指派類型、所需的持續時間,並提供理由。 當您準備好繼續時,請選取 [ 指派]。 如需指派類型的詳細資訊,請參閱在Privileged Identity Management 中指派特殊許可權存取群組的資格 (預覽) 。
指派完成後,請透過存取叢集來確認 Just-In-Time 存取是否正常運作。 例如:
az aks get-credentials --resource-group myResourceGroup --name myManagedCluster
請遵循步驟登入。
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-61156405-vmss000000 Ready agent 6m36s v1.18.14
aks-nodepool1-61156405-vmss000001 Ready agent 6m42s v1.18.14
aks-nodepool1-61156405-vmss000002 Ready agent 6m33s v1.18.14
在命名空間層級套用 Just-In-Time 存取
- 整合 AKS 叢集與 Azure RBAC。
- 透過角色指派,將您想要與 Just-In-Time 存取整合的群組與叢集中的命名空間產生關聯。
az role assignment create --role "Azure Kubernetes Service RBAC Reader" --assignee <AAD-ENTITY-ID> --scope $AKS_ID/namespaces/<namespace-name>
- 將您剛才在命名空間層級設定的群組與 PIM 產生關聯,以完成設定。
疑難排解
如果 kubectl get nodes
傳回類似下列的錯誤:
Error from server (Forbidden): nodes is forbidden: User "aaaa11111-11aa-aa11-a1a1-111111aaaaa" cannot list resource "nodes" in API group "" at the cluster scope
請確定安全性群組的系統管理員已將 帳戶指派給 您的帳戶。
後續步驟
- 瞭解 Kubernetes 授權的 Azure RBAC 整合
- 瞭解Azure AD與 Kubernetes RBAC 的整合。
- 使用 kubelogin 來存取 kubectl 中無法使用的 Azure 驗證功能。
- 深入瞭解 AKS 和 Kubernetes 身分識別概念。
- 使用Azure Resource Manager (ARM) 範本來建立已啟用 AKS 管理的Azure AD叢集。