Sub topic 7
ClusterRoles: cluster-wide and reusable permissions
This chapter assumes you can already read a Role and a RoleBinding. It also assumes you know the parts of a rule (apiGroups, resources, verbs) and can apply manifests with kubectl.
What a ClusterRole is
A ClusterRole has the same rules structure as a Role, but it has no namespace. Because it lives at the cluster level, you can use it in three ways:
- Grant access to cluster-scoped resources, such as nodes, persistent volumes, and namespaces. A Role cannot do this, because those resources belong to no namespace.
- Grant access to non-resource URLs, such as
/healthz, usingnonResourceURLsin a rule. - Define a permission set once and reuse it in many namespaces.
This ClusterRole allows reading secrets:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: secret-reader
rules:
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "watch", "list"]The ClusterRole grants nothing by itself. A binding connects it to subjects, and the kind of binding decides how far the permissions reach.
Two ways to bind a ClusterRole
A ClusterRoleBinding applies the rules in every namespace, and to cluster-scoped resources. A RoleBinding that points at a ClusterRole applies the rules only in the RoleBinding's own namespace.
The first manifest below binds secret-reader to the group manager across the whole cluster. The second binds the same ClusterRole to jane, but only in the dev namespace.
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.io
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-secrets-dev
namespace: dev
subjects:
- kind: User
name: jane
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: secret-reader
apiGroup: rbac.authorization.k8s.ioThe second pattern is the main reason to write a ClusterRole for namespaced resources. You maintain one definition and bind it per namespace. The built-in view, edit, and admin roles work this way.
Common errors
- Referencing a Role from a ClusterRoleBinding. A ClusterRoleBinding can only reference a ClusterRole. A Role belongs to one namespace, so it cannot be bound cluster-wide.
- Adding a namespace to a ClusterRole. The
metadata.namespacefield is ignored on a ClusterRole, which can hide a mistaken assumption about its scope. - Editing
roleRef. TheroleRefof a binding is immutable. To point a binding at a different role, delete the binding and create it again. - Granting too much through a ClusterRoleBinding. Binding
secret-readerthis way exposes secrets in every namespace, includingkube-system. Use a RoleBinding when the need is limited to one or a few namespaces.
Choosing a scope
| Requirement | Use |
|---|---|
| Read nodes or persistent volumes | ClusterRole and ClusterRoleBinding |
| Same permissions in several namespaces | One ClusterRole, a RoleBinding in each namespace |
| Permissions in one namespace only | Role and RoleBinding |
| Access to every namespace | ClusterRole and ClusterRoleBinding |
Exercise
A team needs to read pods in the namespaces dev and staging, but not in prod. You want to define the permissions only once. Which objects do you create?
Show answer
Create one ClusterRole that allows get, list, and watch on pods. Then create a RoleBinding in dev and another in staging, each with a roleRef pointing at that ClusterRole. Do not create a ClusterRoleBinding, because it would also grant access in prod.