驗證通常會搭配 Kubernetes 角色型存取控制 (RBAC),為已驗證的使用者和服務帳戶提供更精細的叢集存取權控管。建議您建立使用群組名稱而非使用者 ID 的 RBAC 政策。將 RBAC 政策明確連結至群組後,您就能完全透過識別資訊提供者管理使用者存取權,因此使用者權限變更時,不需要更新叢集。請注意,如要根據安全性群組成員資格設定 OIDC 存取控管,請務必設定叢集,以支援從識別資訊提供者取得群組成員資格資訊。本文僅適用於機群成員叢集。
範例
如要讓特定已驗證的使用者存取叢集的 Pod,請建立 ClusterRole,授予這些資源的存取權,如下例所示:
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: pod-reader rules: - apiGroups: [""] # The resource type for which access is granted resources: ["pods"] # The permissions granted by the ClusterRole verbs: ["get", "watch", "list"]
接著,您會建立對應的 ClusterRoleBinding,在 ClusterRole 中授予相關使用者權限,在本例中,這些使用者是 us-east1-cluster-admins 安全性群組的成員,以及 ID 為 u98523-4509823 的使用者:
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: read-pods-admins subjects: # Grants anyone in the "us-east1-cluster-admins" group # read access to Pods in any namespace within this cluster. - kind: Group name: gid-us-east1-cluster-admins # Name is case-sensitive apiGroup: rbac.authorization.k8s.io # Grants this specific user read access to Pods in any # namespace within this cluster - kind: User name: uid-u98523-4509823 apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole name: pod-reader apiGroup: rbac.authorization.k8s.io
在下列範例中,這個 ClusterRoleBinding 會將 ClusterRole 中的權限授予 ID 為 12345678-BBBb-cCCCC-0000-123456789012 的相關群組。請注意,這項設定僅適用於 Azure AD 供應商,且適用於 Google Distributed Cloud 叢集。
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: pod-reader-binding subjects: # Retrieves group information for the group ID mentioned - kind: Group name: 12345678-BBBb-cCCCC-0000-123456789012 apiGroup: rbac.authorization.k8s.io
如要進一步瞭解如何使用 RBAC,請參閱「設定角色型存取權控管」和「使用 RBAC 授權」。
建立 RBAC 角色,以供 Google Cloud 存取控制台
透過 OIDC 提供者完成驗證的使用者,可以從 Google Cloud 控制台和指令列登入叢集。
如要透過 Google Cloud 控制台存取叢集資源,經過驗證的使用者必須具備相關 Kubernetes 權限。如果您不想授予使用者更廣泛的權限 (例如叢集管理員權限),可以建立自訂 RBAC 角色,其中包含查看叢集節點、永久磁碟區、Pod 和儲存空間級別所需的最低權限。您可以在叢集中建立 ClusterRole RBAC 資源 cloud-console-reader,定義這組權限。
cloud-console-reader 會授予使用者叢集節點、永久磁碟區、Pod 和儲存空間級別的 get、list 和 watch 權限,讓使用者查看這些資源的詳細資料。
kubectl
如要建立 cloud-console-reader ClusterRole 並套用至叢集,請執行下列指令:
cat <<EOF > cloud-console-reader.yaml
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: cloud-console-reader
rules:
- apiGroups: [""]
resources: ["nodes", "persistentvolumes", "pods"]
verbs: ["get", "list", "watch"]
- apiGroups: ["storage.k8s.io"]
resources: ["storageclasses"]
verbs: ["get", "list", "watch"]
EOF
kubectl apply -f cloud-console-reader.yaml
接著,您可以在設定權限政策時,將這個 ClusterRole 授予使用者,如上一節所述。請注意,使用者也需要 IAM 權限,才能在 Google Cloud 控制台中查看叢集。