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.
Under the hood
Section titled “Under the hood”-
Authentication establishes who you are: username and groups, from a client certificate, an OIDC token, or a service account token.
-
Authorization checks the request’s attributes against every binding: verb (
get,list,watch,create,update,patch,delete), resource, subresource (likepods/execorpods/log), API group, namespace, and optionally resource name. -
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.
-
A pod uses
defaultunless you say otherwise. Every namespace has adefaultservice account. SetserviceAccountNameper workload, andautomountServiceAccountToken: falseif the pod never calls the API.
Minimal example
Section titled “Minimal example”kubectl create namespace team-akubectl -n team-a create serviceaccount readerkubectl -n team-a create role pod-reader --verb=get,list,watch --resource=podskubectl -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 # yeskubectl auth can-i list secrets -n team-a --as=system:serviceaccount:team-a:reader # nokubectl auth can-i list pods -n default --as=system:serviceaccount:team-a:reader # nokubectl auth can-i --as is the fastest RBAC debugging tool there is. Impersonating another identity requires the impersonate permission yourself, which cluster admins have.
How it breaks
Section titled “How it breaks”Self-check
Section titled “Self-check”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.