Sub topic 10
Granting access inside one namespace
This chapter assumes you can read Kubernetes YAML, run kubectl, and know what Pods and Services are. It also assumes you know a namespace scopes names and objects, as covered earlier. Here you will use Role-Based Access Control (RBAC) to grant access inside a single namespace.
The working model
RBAC answers one question: may this subject perform this verb on this resource? A subject is a user, a group, or a ServiceAccount. A verb is an action such as get, list, watch, create, or delete.
Two objects work together inside a namespace:
- A Role lists permissions. It is a namespaced object, so it only describes access to resources in its own namespace.
- A RoleBinding attaches a Role to subjects. It also lives in one namespace, and it grants access only there.
RBAC is additive. There are no deny rules. A subject can do something only if some Role bound to it allows it.
A Role and a RoleBinding
This manifest lets the user jane read Pods in team-a and nowhere else.
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-reader
namespace: team-a
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
namespace: team-a
subjects:
- kind: User
name: jane
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.ioSome details matter here:
apiGroups: [""]means the core API group. Pods, Services, ConfigMaps, and Secrets are in it. Deployments are inapps.roleRefis immutable. To point a binding at a different Role, delete and recreate the binding.- Kubernetes does not store users. The name
janemust match the identity your authenticator reports, such as a certificate common name or an OIDC claim.
Checking what a subject can do
Use kubectl auth can-i with impersonation. You need permission to impersonate, which cluster admins usually have.
kubectl auth can-i list pods --namespace team-a --as jane
kubectl auth can-i list pods --namespace team-b --as jane
kubectl auth can-i delete pods --namespace team-a --as janeThe first command prints yes. The other two print no. The second shows the namespace boundary at work, and the third shows that a Role grants only the verbs it lists.
Common errors
- Forbidden message. An error such as
pods is forbidden: User "jane" cannot list resource "pods" in API group "" in the namespace "team-b"names the user, verb, resource, and namespace. Compare each with your Role and binding. - Wrong namespace on the binding. A Role in
team-abound by a RoleBinding inteam-bdoes nothing useful. A RoleBinding can reference a Role only in its own namespace. - Missing resource or API group. Listing
deploymentsunderapiGroups: [""]grants nothing, because Deployments are inapps. - Subject name mismatch. For a ServiceAccount, the subject needs
kind: ServiceAccount, aname, and anamespace, and it does not take anapiGroup. - Using a ClusterRole by accident. A RoleBinding may reference a ClusterRole. The permissions then apply only in the binding's namespace. This is a common way to reuse a role such as
viewacross many namespaces. A ClusterRoleBinding, by contrast, grants access in every namespace.
Exercise
Exercise
A Role named editor exists in team-a and allows create on deployments with apiGroups: [""]. A RoleBinding gives it to jane, but kubectl auth can-i create deployments --namespace team-a --as jane prints no. What is wrong, and how do you fix it?
Show answer
Deployments belong to the apps API group, not the core group "". The rule matches nothing. Change the rule to apiGroups: ["apps"] with resources: ["deployments"], apply the Role, and run the check again. It should print yes.
Exercise
You want jane to have the built-in read-only view ClusterRole in team-a but not in team-b. Which binding object do you create, and in which namespace?
Show answer
Create a RoleBinding in team-a whose roleRef has kind: ClusterRole and name: view. A RoleBinding limits the ClusterRole's permissions to its own namespace. A ClusterRoleBinding would grant the access in all namespaces.