Security
RBAC and ServiceAccounts
Who can do what
How a Pod proves who it is to the API server with a projected token, and how RBAC answers. The identity is a JWT the kubelet keeps fresh in a file inside the container; the permission is three small objects - a ServiceAccount, a Role, a RoleBinding - checked on every single request. One allowed read, one denied read, and the machinery is the same both times.
- Serviceaccount
- report-runner · ns default
- Token
- projected JWT · RS256 · 3607 s
- Role
- report-reader · get list watch deployments
- Denied
- secrets → 403 (allow-only)
Read the whole lesson as text
1. The identity
Before the Pod exists, its identity does: a ServiceAccount called report-runner in namespace default. A Role, report-reader, grants exactly three verbs on one resource - get, list, watch on deployments - and a RoleBinding, report-list, staples the account to the role. Roles say what, bindings say who, and neither knows the other's purpose. A Pod that declares no account gets the namespace's default one.
- kubectl creates the account, the role and the binding that ties them.
- All three land in etcd like any other object.
2. The token
Zoom into node-1, where the report Pod runs. Its spec says serviceAccountName: report-runner and, by default, automountServiceAccountToken: true. So before starting the container the kubelet mints a token with the TokenRequest API - 3607 seconds, bound to this Pod - and projects it into the container as a file. Unlike the old static Secret tokens, this one expires, and the kubelet re-mints it at about 80 percent of its lifetime.
- The kubelet sees the Pod and which ServiceAccount it speaks for.
- The kubelet asks the API server to mint a short-lived token for the account.
- The token lands in the container as a file on a tmpfs mount.
3. The JWT
The app reads the file - three base64url blobs joined by dots. The header names the algorithm, RS256; the payload is the identity claim: issuer, subject system:serviceaccount:default:report-runner, audience, and when it was issued and when it should be replaced: 3607 seconds later. The signature is the part that matters: only the cluster's signing key can produce it, so the app can carry this token anywhere without being able to forge one.
- The app reads the file and gets a JWT: header, payload, signature.
- One request carries both the question and the identity: list Deployments, Bearer token attached.
4. Two questions
The API server answers two questions about every request, in order. First, who: the token's signature checks out against the service-account signing keys, it hasn't expired, and its audience is this server - so the subject becomes system:serviceaccount:default:report-runner. Then, may it: RBAC gathers every RoleBinding in this namespace and every ClusterRoleBinding that names this subject, and unions the rules. report-list → report-reader grants list on deployments: allow, and the list of Deployments comes back.
- Question one: is the token genuine and alive? Yes.
- Question two: may this subject list deployments here? The binding says yes.
- The list comes back: three Deployments.
5. The deny
Now the same Pod asks for something adjacent: the Secrets in the namespace. Authentication passes identically - the token is still good. Authorization fails: RBAC searches every binding for this subject and finds get, list, watch on deployments and nothing that touches secrets. RBAC is allow-only with a default deny: there are no deny rules and no ordering - bindings simply union, and an empty union is a refusal. The answer is a 403 with the exact reason in the body.
- The same Pod, the same token, asks for Secrets this time.
- Authn passes again; authz finds no rule that grants secrets to this subject.
- The answer is 403, with the exact reason in the body.
6. Hygiene
Two habits close the holes. First, automountServiceAccountToken: false on any Pod that doesn't call the API - the token file then isn't even created, so a compromised container has nothing to steal. Second, one ServiceAccount per app with a narrow Role: the namespace's default account is mounted by every Pod that says nothing, so whatever it can do, every hacked Pod can do. The same bindings work for people - User and Group subjects bind identically - and kubectl --as lets you impersonate an identity to preview exactly what it may do.
- The web Pods never call the API, so their template turns automount off.
- Impersonation answers "what could this identity do?" without its credentials.
Summary
Identity and permission only meet at request time. The Pod's whole identity is a JWT the kubelet mints, projects and refreshes - it expires, and it is bound to one Pod. The API server verifies it, then asks RBAC whether the union of the subject's bindings grants this verb on this resource; when no rule matches, the default deny answers 403. RBAC never looks inside the object, though. What a Deployment may contain is a separate question, asked at a later stop on the same road.
Next lesson: Kyverno policies