Kubernetes RBAC & Service Accounts
Grant a namespace read-only access, give a workload its own identity, and verify with the cluster rather than by hoping.
- Time
- 50 min
- Level
- Intermediate
- Objectives
- 4 objectives
- Cost
- Free
Before you start
You will need
- kind or minikube
- kubectl 1.28+
You will be able to
- Assemble Role, RoleBinding, ClusterRole and ClusterRoleBinding correctly
- Give a Pod an identity that is not the default ServiceAccount
- Verify permissions with `auth can-i` instead of by trial
Success criteria
0 of 4
The scenario#
Every workload in the cluster runs as the default ServiceAccount, and its token is mounted into every Pod. Anything that reaches a container reaches the Kubernetes API with it.
NetworkPolicies control what a Pod can talk to. RBAC controls what it can do, and you need both.
1. Four objects, two questions#
| Object | Answers | Scope |
|---|---|---|
Role | What may be done? | One namespace |
ClusterRole | The same, cluster-wide | Whole cluster |
RoleBinding | Who gets it? | One namespace |
ClusterRoleBinding | Who gets it everywhere? | Whole cluster |
Permissions and subjects are deliberately separate, which is what lets one
ClusterRole be bound differently in twenty namespaces.
2. A Role, which names nobody#
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: team-a
name: pod-reader
rules:
- apiGroups: [""] # "" is the core API group
resources: ["pods", "pods/log"]
verbs: ["get", "list", "watch"]pods/log is a subresource and a separate grant — get pods does not include
reading their logs, which surprises people.
3. A RoleBinding, which names the subject#
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
namespace: team-a
name: team-a-read
subjects:
- kind: ServiceAccount
name: viewer
namespace: team-a
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io4. Verify against the cluster#
kubectl auth can-i list pods -n team-a --as system:serviceaccount:team-a:viewer # yes
kubectl auth can-i list pods -n team-b --as system:serviceaccount:team-a:viewer # no
kubectl auth can-i delete pods -n team-a --as system:serviceaccount:team-a:viewer # no
kubectl auth can-i --list -n team-a --as system:serviceaccount:team-a:viewer--as asks the API server to evaluate the real policy. Reading YAML tells
you what you meant; this tells you what the cluster will actually do, and they
differ more often than anyone expects.
5. A workload with its own identity#
apiVersion: v1
kind: ServiceAccount
metadata:
name: api
namespace: team-a
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
spec:
template:
spec:
serviceAccountName: api
automountServiceAccountToken: false # it never calls the APIThat last line matters more than it looks. Kubernetes mounts an API token into every Pod by default. An application that never talks to the API has no use for one — but an attacker who reaches the container does.
kubectl exec deploy/api -- ls /var/run/secrets/kubernetes.io/serviceaccount 2>&1
# No such file — correct6. Two properties that surprise people#
RBAC is purely additive; there is no deny rule. A subject can do the union of everything its bindings grant. You restrict by not granting — so when someone has too much access, the fix is finding the extra binding, never adding a denial.
A namespaced RoleBinding may reference a ClusterRole. This is the common
pattern: define view or edit once cluster-wide, bind it per namespace, and
the permissions apply only inside the binding's namespace.
kubectl get clusterrole view edit admin
kubectl create rolebinding team-a-view --clusterrole=view \
--serviceaccount=team-a:viewer -n team-a --dry-run=client -o yaml7. Audit what you already have#
kubectl get clusterrolebindings -o json | \
jq -r '.items[] | select(.roleRef.name=="cluster-admin") | .metadata.name'Run that on any cluster you inherit. A ServiceAccount bound to cluster-admin
is the most common over-grant in existence, and it is usually there because
something did not work once.
When it goes wrong#
auth can-i says no when the YAML looks right
Check the ServiceAccount namespace in subjects — a RoleBinding can reference a subject from another namespace, and a mismatch fails silently.
Permissions work in one namespace only
That is a RoleBinding doing its job. Cluster-wide needs a ClusterRoleBinding.
Forbidden reading logs despite get pods
pods/log is a separate resource. Add it to the rule.
Removing a binding does not revoke access
Another binding still grants it. RBAC is additive — search all bindings for the subject.
Clean up#
Run this even if you did not finish.
Destructive — This removes real resources. Check which environment you are in first.
kubectl delete namespace <ns> --ignore-not-found
kubectl get all -A | grep -v kube-systemCost of this lab: Free — kind or minikube.
The concept behind it
Next up
Lab 39 of 58 on the project path