Sub topic 3
Granting permissions with Roles and RoleBindings
This chapter assumes you can apply YAML with kubectl, know the difference between authentication and authorization, and have created a ServiceAccount in a namespace. Authentication tells the API server who a Pod is. RBAC (role-based access control) decides what that identity may do.
The RBAC model
RBAC has two kinds of objects:
- A Role is a list of permissions inside one namespace. It names API groups, resources, and verbs.
- A RoleBinding attaches a Role to one or more subjects, such as a ServiceAccount.
RBAC is additive. There are no deny rules. A new ServiceAccount has almost no permissions until you bind a Role to it.
Write a Role and a RoleBinding
The manifest below lets the app-reader ServiceAccount read ConfigMaps in the demo namespace and nothing else.
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: configmap-reader
namespace: demo
rules:
- apiGroups: [""]
resources: ["configmaps"]
verbs: ["get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: app-reader-configmaps
namespace: demo
subjects:
- kind: ServiceAccount
name: app-reader
namespace: demo
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: configmap-readerPoints to notice:
apiGroups: [""]means the core API group. ConfigMaps, Pods, and Secrets live there. Deployments live inapps.verbslists the allowed actions. Here they areget(one object) andlist(many objects).- A ServiceAccount subject needs a
namespacefield. A User or Group subject does not. roleRefis immutable. To point a binding at a different Role, delete the binding and create it again.- The Role and RoleBinding must be in the same namespace. The ServiceAccount can come from another namespace if you name it in
subjects.
Verify with kubectl auth can-i
Do not assume the binding works. Ask the API server by impersonating the ServiceAccount:
kubectl auth can-i list configmaps -n demo --as=system:serviceaccount:demo:app-reader
kubectl auth can-i delete pods -n demo --as=system:serviceaccount:demo:app-readerThe first command should print yes. The second should print no, because no Role grants delete on Pods. The username format is system:serviceaccount:<namespace>:<name>. You need permission to impersonate to run these checks, which cluster admins normally have.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Wrong `namespace` in the subject | Binding exists but grants nothing to the real ServiceAccount | Match the ServiceAccount's own namespace |
| Wrong `apiGroup` for a resource | Rule never matches | Check with `kubectl api-resources` |
| Using `*` for verbs or resources | Grants far more than needed | List only the verbs and resources the app uses |
| Binding to the default ServiceAccount | Every Pod without an explicit account inherits the permissions | Create a dedicated ServiceAccount |
Roles versus ClusterRoles
A Role only covers one namespace. A ClusterRole is cluster-wide, and it covers resources that have no namespace, such as Nodes. You can also bind a ClusterRole with a RoleBinding. The permissions then apply only inside the binding's namespace. This lets you define a rule set once and reuse it across namespaces.
Exercise
A Pod using the app-reader ServiceAccount in demo gets forbidden when it lists ConfigMaps in the staging namespace. The Role and RoleBinding above exist in demo. Why does this happen, and what is the least-privilege fix?
Show answer
A Role and RoleBinding only apply in their own namespace, so the grant covers demo only. The fix is to create a Role in staging that allows get and list on ConfigMaps, and a RoleBinding in staging whose subject is the app-reader ServiceAccount from namespace demo. Do not use a ClusterRoleBinding, which would grant access in every namespace.
Exercise
What command shows whether app-reader may delete Pods in demo, and what output do you expect with the manifests above?
Show answer
Run kubectl auth can-i delete pods -n demo --as=system:serviceaccount:demo:app-reader. The output is no, because the only rule allows get and list on ConfigMaps.