Skip to main content
Security & multi-tenancyLesson 2 of 13

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.

Identityshop / reporter
Token—
Result—
Worker nodePod reporterSA · reporterToken fileProjected volumekubeletGets the tokenAPI serverTokenRequestToken checkSignature · aud · expRBACIs this allowed?ServiceAccountns shopCloud IAMTrusts via OIDCserviceAccountName
RequestResponse / reportWatchAppears in a later step

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?​

AnswerStageTypical causeWhat to check
401 UnauthorizedAuthenticationToken expired, e.g. read once at startup and never re-readThe app re-reads the token file; update old client libraries
401 UnauthorizedAuthenticationThe bound Pod was deleted, or the token was copied to another placeGet a fresh token from a running Pod or with kubectl create token
401 UnauthorizedAuthenticationWrong audience: a token made for another systemThe aud claim matches what the API server accepts
401 UnauthorizedAuthenticationNo token sent at all (automountServiceAccountToken: false)The token is mounted where the app looks for it
403 ForbiddenAuthorizationNo RoleBinding or ClusterRoleBinding gives this verb on this resourcekubectl auth can-i ... --as=system:serviceaccount:<ns>:<sa>
403 ForbiddenAuthorizationThe binding exists but is in a different namespaceThe 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

  1. Pod reporter has a valid token, but listing Secrets returns 403 Forbidden. What fixes it?
  2. Someone copied a Pod's mounted token and uses it from a laptop after the Pod was deleted. What happens?
  3. An app reads the token file once at startup and starts getting 401 a few hours later. What is the most likely cause?

Further reading: Service Accounts · Configure Service Accounts for Pods · Managing Service Accounts · Authenticating: service account tokens · Who Can Do What (RBAC) · Managed Kubernetes