Sub topic 6
Attaching permissions to subjects with a RoleBinding
A Role lists permissions, but it grants nothing until a binding connects it to someone. This chapter covers the RoleBinding, the object that does that in one namespace. It assumes you can write YAML manifests, apply them with kubectl, and know what a Role and a subject (user, group, or service account) are.
What a RoleBinding does
A RoleBinding is a namespaced object with two parts:
subjects: a list of users, groups, or service accounts that receive the permissions.roleRef: a single reference to the Role (or ClusterRole) being granted.
The binding lives in a namespace, and the permissions it grants apply only in that namespace. RBAC is additive and deny-by-default, so a subject can do something only if at least one binding gives it that permission.
A worked example
The Role pod-reader allows reading pods in the default namespace. This binding grants it to the user jane:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
namespace: default
subjects:
- kind: User
name: jane
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.ioApply it with kubectl apply -f rolebinding.yaml. After that, jane can get, list, and watch pods in default. She still has no access to pods in other namespaces, and she still cannot delete anything.
The fields that matter
Each subject has a kind, a name, and an apiGroup. For User and Group, the apiGroup is rbac.authorization.k8s.io. Kubernetes does not store users or groups. The names must match what your authenticator reports, such as a certificate's common name or a claim from your identity provider.
For a ServiceAccount subject, you set namespace instead of apiGroup.
The roleRef has these properties:
kindisRoleorClusterRole.namemust match an existing object. A RoleBinding that names a missing Role is accepted, but it grants nothing until that Role exists.- The Role must be in the same namespace as the binding. A ClusterRole is also allowed, and the binding then limits it to its own namespace.
roleRef is immutable
You cannot change roleRef after you create the binding. This stops someone with permission to edit bindings from quietly swapping in a stronger role. To point a binding at a different Role, delete it and create it again. Changing the subjects list is allowed.
Common mistakes
- Wrong namespace. The binding's namespace decides where the permissions apply. A binding in
staginghas no effect indefault. - Name mismatch. A typo in the user name does not produce an error. The binding exists but matches nobody.
- Missing namespace on a service account subject. The subject is rejected or matches nothing.
- Trying to edit
roleRef. The API server refuses the update.
Exercise
You apply a RoleBinding in the staging namespace that references the Role pod-reader and names the user jane. Jane runs kubectl get pods -n default and gets a Forbidden error. Why, and what is the smallest fix?
Show answer
A RoleBinding only grants permissions in its own namespace, so the binding in staging gives Jane nothing in default. The smallest fix is to create a RoleBinding in default that references a pod-reader Role existing in default. If you want the rules shared across namespaces, bind a ClusterRole with a RoleBinding in each namespace.
Exercise
You need to change the read-pods binding so it references the Role pod-editor instead of pod-reader. How do you do it?
Show answer
You cannot edit roleRef on an existing binding. Delete the RoleBinding and create it again with roleRef.name: pod-editor.