設定角色型存取權控管 (RBAC)

驗證通常會與 Kubernetes 角色型存取控制 (RBAC) 搭配使用,為已驗證的使用者和服務帳戶提供更精細的叢集存取權控管。建議建立使用群組名稱而非使用者 ID 的 RBAC 政策。將 RBAC 政策明確連結至群組後,您就能完全透過身分識別供應商管理使用者存取權,因此使用者權限變更時,不需要更新叢集。請注意,如要根據安全性群組的成員身分設定存取控制項,您必須確保 GKE Identity Service 已設定為支援從身分識別提供者取得群組成員資訊。

範例

如要讓特定已驗證的使用者存取叢集的 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 授權」。

建立 Google Cloud 主控台存取權的 RBAC 角色

使用 OIDC 提供者通過驗證的使用者,可以從 Google Cloud 控制台登入叢集,也可以透過指令列登入。

如要透過 Google Cloud 控制台 存取叢集資源,經過驗證的使用者必須具備相關的 Kubernetes 權限。如果您不想授予這些使用者更廣泛的權限 (例如叢集管理員權限),可以建立自訂 RBAC 角色,其中包含查看叢集節點、永久磁碟區、Pod 和儲存空間類別所需的最低權限。您可以在叢集中建立 ClusterRole RBAC 資源 cloud-console-reader,定義這組權限。

cloud-console-reader 會授予使用者叢集節點、永久磁碟區、Pod 和儲存空間類別的 getlistwatch 權限,讓使用者查看這些資源的詳細資料。

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 控制台中查看叢集。