Sub topic 8
Granting cluster-wide access with a ClusterRoleBinding
This chapter assumes you know what a Role, a ClusterRole, and a RoleBinding are, and that you can apply YAML with kubectl. A ClusterRoleBinding attaches a ClusterRole to subjects across the whole cluster.
What a ClusterRoleBinding does
A RoleBinding grants permissions inside one namespace. A ClusterRoleBinding grants them in every namespace, including namespaces created later. It also covers resources that have no namespace, such as nodes and persistent volumes.
A ClusterRoleBinding has two parts:
subjects: the users, groups, or service accounts that receive the permissions.roleRef: the ClusterRole being granted. It can only point to a ClusterRole, never to a Role.
A worked example
The manifest below defines a ClusterRole that can read secrets. It then binds that ClusterRole to the group manager.
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: secret-reader
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "watch", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: read-secrets-global
subjects:
- kind: Group
name: manager
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: secret-reader
apiGroup: rbac.authorization.k8s.ioThe empty string in apiGroups means the core API group, where secrets live. Every member of manager can now get, list, and watch secrets in all namespaces.
A ClusterRoleBinding has no namespace field in its metadata, because it is not namespaced. For a ServiceAccount subject, you must still give the service account's namespace, since service accounts belong to a namespace.
Choosing between the two bindings
| Requirement | Use |
|---|---|
| Access to one namespace only | Role or ClusterRole with a RoleBinding |
| Same permissions in a few chosen namespaces | One ClusterRole with a RoleBinding in each namespace |
| Access to every namespace | ClusterRole with a ClusterRoleBinding |
| Access to non-namespaced resources such as nodes | ClusterRole with a ClusterRoleBinding |
Common errors
- Immutable
roleRef. After you create a binding, you cannot change itsroleRef. To point it at a different ClusterRole, delete the binding and create it again. This prevents someone with only update rights from silently swapping the granted permissions. - Granting too widely. A ClusterRoleBinding for
secret-readerexposes secrets in every namespace, includingkube-system. If the subject only needs one namespace, use a RoleBinding that references the ClusterRole instead. - Wrong subject kind or name.
GroupandUsernames are matched as plain strings from the authentication step. A typo does not cause an error. The binding is created and then grants access to nobody. - Missing namespace on a service account. A
ServiceAccountsubject withoutnamespaceis rejected.
Verifying the result
After you apply the binding, test it with impersonation. Impersonating a group needs a user name too, so pass both.
kubectl auth can-i list secrets --all-namespaces --as alice --as-group manager
kubectl get clusterrolebinding read-secrets-global -o yamlThe first command should print yes. The second shows the stored subjects and roleRef, so you can check them for typos.
Exercise
A team needs to read pods in the namespaces dev and test, but nowhere else. Should you use a ClusterRoleBinding? What would you use?
Show answer
No. A ClusterRoleBinding would grant access in every namespace. Define one ClusterRole (or a Role in each namespace) that reads pods, then create a RoleBinding in dev and another in test, each referencing it.
Exercise
You created a ClusterRoleBinding that points to the wrong ClusterRole. How do you fix it?
Show answer
The roleRef field cannot be changed after creation. Delete the binding with kubectl delete clusterrolebinding <name>, then create it again with the correct roleRef.