How a Pod Proves Who It Is (ServiceAccount Tokens)
The RBAC lesson shows that a Pod's requests are checked as its ServiceAccount. This lesson zooms in on the step before that: where the Pod's token comes from, what is inside it, and why it stops working.
What you will learn
- Every Pod runs as a ServiceAccount, default if none is set
- The kubelet gets a bound, short-lived token with TokenRequest
- What the API server checks in a token
- 401 Unauthorized versus 403 Forbidden
- How outside systems trust the token through OIDC discovery
401 or 403?
| Answer | Stage | Typical cause | What to check |
|---|---|---|---|
| 401 Unauthorized | Authentication | Token expired, e.g. read once at startup and never re-read | The app re-reads the token file; update old client libraries |
| 401 Unauthorized | Authentication | The bound Pod was deleted, or the token was copied to another place | Get a fresh token from a running Pod or with kubectl create token |
| 401 Unauthorized | Authentication | Wrong audience: a token made for another system | The aud claim matches what the API server accepts |
| 401 Unauthorized | Authentication | No token sent at all (automountServiceAccountToken: false) | The token is mounted where the app looks for it |
| 403 Forbidden | Authorization | No RoleBinding or ClusterRoleBinding gives this verb on this resource | kubectl auth can-i ... --as=system:serviceaccount:<ns>:<sa> |
| 403 Forbidden | Authorization | The binding exists but is in a different namespace | The RoleBinding's namespace and subject namespace |
Many clusters extend the default mounted token's lifetime on the server for compatibility, and log when an app uses a token older than an hour. Treat the token as short-lived anyway.
Try it yourself
kubectl create serviceaccount reporter -n shop
kubectl create token reporter -n shop --duration=10m # A short-lived token printed to stdout
kubectl create token reporter -n shop | cut -d. -f2 | base64 -d 2>/dev/null; echo # Claims: iss, sub, aud, exp
kubectl auth can-i list configmaps -n shop --as=system:serviceaccount:shop:reporter # yes / no (= 403)
kubectl exec -n shop <pod> -- ls /var/run/secrets/kubernetes.io/serviceaccount/ # token, ca.crt, namespace
kubectl get pod <pod> -n shop -o jsonpath='{.spec.serviceAccountName}{"\n"}' # Which SA the Pod runs as
kubectl get --raw /.well-known/openid-configuration # Issuer and JWKS URL
The base64 step may print a short error because JWT parts drop the = padding. The claims still show. The kubernetes.io claim of a Pod's mounted token names the Pod it is bound to.
Check yourself
Further reading: Service Accounts · Configure Service Accounts for Pods · Managing Service Accounts · Authenticating: service account tokens · Who Can Do What (RBAC) · Managed Kubernetes