Skip to content

RBAC and service accounts

Draft: not yet run against a live cluster. Treat output as representative and check it on yours.

Every Forbidden error contains the full answer to “what’s missing”: who asked, which verb, which resource, which API group, and which namespace. Most people skim past it. This chapter teaches you to read it and fix exactly that, without reaching for cluster-admin.

  1. Authentication establishes who you are: username and groups, from a client certificate, an OIDC token, or a service account token.

  2. Authorization checks the request’s attributes against every binding: verb (get, list, watch, create, update, patch, delete), resource, subresource (like pods/exec or pods/log), API group, namespace, and optionally resource name.

  3. Service accounts get short-lived tokens. In current Kubernetes, the kubelet projects a time-bound, audience-bound token into the pod and refreshes it. Long-lived token Secrets are no longer created automatically for service accounts.

  4. A pod uses default unless you say otherwise. Every namespace has a default service account. Set serviceAccountName per workload, and automountServiceAccountToken: false if the pod never calls the API.

Terminal window
kubectl create namespace team-a
kubectl -n team-a create serviceaccount reader
kubectl -n team-a create role pod-reader --verb=get,list,watch --resource=pods
kubectl -n team-a create rolebinding reader-pods --role=pod-reader --serviceaccount=team-a:reader
kubectl auth can-i list pods -n team-a --as=system:serviceaccount:team-a:reader # yes
kubectl auth can-i list secrets -n team-a --as=system:serviceaccount:team-a:reader # no
kubectl auth can-i list pods -n default --as=system:serviceaccount:team-a:reader # no

kubectl auth can-i --as is the fastest RBAC debugging tool there is. Impersonating another identity requires the impersonate permission yourself, which cluster admins have.

Can a RoleBinding reference a ClusterRole? What scope does the grant have?

Yes. The permissions apply only in the RoleBinding’s namespace. It’s a common way to reuse one role definition across namespaces.

Why is create on pods considered a privileged permission?

Because a pod can mount any Secret or service account in its namespace, so anyone who can create pods can effectively read those secrets and act as those service accounts.

How do you check what a service account can do without logging in as it?

kubectl auth can-i <verb> <resource> -n <ns> --as=system:serviceaccount:<ns>:<name>, or --list to see everything it can do in that namespace.

Maps to: CKA: Cluster Architecture, Installation and Configuration; CKAD: Application Environment, Configuration and Security. Check the current exam curriculum, since domain names change between revisions.