Q: What is a ServiceAccount token and when does it expire?
A ServiceAccount token is a signed JWT that proves a pod or client is a specific ServiceAccount to the API server.
Want to master this scenario in a live sandbox? KodeKloud's CKA & CKAD Hands-On Certification Track covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: A <strong>ServiceAccount token</strong> is a signed JWT that proves a pod or client is a specific ServiceAccount to the API server. Modern clusters issue <em>bound</em>, time-limited tokens through the TokenRequest API, projected into the pod and refreshed by the kubelet, instead of the old never-expiring Secret tokens. Interviewers ask about expiry to test whether you know the difference between legacy and bound tokens, the 1.22 and 1.24 changes, and the security impact of long-lived credentials.
Mental model: legacy secret tokens versus bound tokens
There are two generations of ServiceAccount tokens, and the expiry answer is different for each.
- Legacy token: a Secret of type
kubernetes.io/service-account-tokenholding a JWT with no expiry. It stays valid until the Secret or ServiceAccount is deleted. Kubernetes stopped auto-creating these in v1.24. - Bound token: issued by the TokenRequest API, with an
expclaim, an audience, and a binding to a specific pod (and its Secret or Node where applicable). It becomes invalid when the pod is deleted. - Default pod behaviour: the kubelet mounts a projected
serviceAccountTokenvolume at/var/run/secrets/kubernetes.io/serviceaccount. The default requested lifetime is 3600 seconds, and the kubelet rotates the token before it lapses (it refreshes once the token passes roughly 80% of its TTL). - Short answer: projected tokens expire in about an hour but are renewed automatically; tokens from
kubectl create tokenalso default to a short lifetime; only manually created legacy Secrets never expire.
Hands-on: ServiceAccount token expiry with kubectl create token
Request a token, decode it, and compare the timestamps to see exactly when it expires.
TOKEN=$(kubectl create token my-sa -n prod --duration 10m)
# decode the payload (second segment)
echo "$TOKEN" | cut -d. -f2 | tr '_-' '/+' | base64 -d 2>/dev/null | jq '{iss,aud,exp,iat,sub}'
# convert exp to a date (GNU date; use date -r on macOS)
date -d @$(echo "$TOKEN" | cut -d. -f2 | tr '_-' '/+' | base64 -d 2>/dev/null | jq .exp)
- Request a token:
kubectl create token my-sa -n prod --duration 10m. The server may clamp or override the requested duration. - Decode the claims: a JWT has three base64url parts; the middle one holds
exp,iat,audand thekubernetes.iobinding object. - Bind to an object:
--bound-object-kind Pod --bound-object-name my-podties validity to that pod, so the token dies when the pod does. - Set audience:
--audience https://vault.example.comproduces a token that only the intended relying party should accept. - Why it matters in practice: tokens from
kubectl create tokenare not stored anywhere in the cluster, so there is nothing to clean up, and you can mint one per CI job with a short duration instead of sharing a long-lived credential.
Projected token in a pod: audience, expirationSeconds and rotation
For workloads that call external systems, request a dedicated audience-scoped token through a projected volume.
apiVersion: v1
kind: Pod
metadata:
name: my-app
namespace: prod
spec:
serviceAccountName: my-sa
containers:
- name: app
image: registry.example.com/my-app:1.0.0
volumeMounts:
- name: vault-token
mountPath: /var/run/secrets/tokens
readOnly: true
volumes:
- name: vault-token
projected:
sources:
- serviceAccountToken:
path: vault-token
audience: https://vault.example.com
expirationSeconds: 3600
- expirationSeconds: the minimum is 600 seconds (10 minutes); choose the shortest lifetime your application can renew.
- Rotation is the app's job: the kubelet rewrites the file, but your process must re-read it. Old client libraries that read the token once at start-up will eventually fail with 401.
- Audience: a token for Vault, a cloud IAM federation endpoint, or another cluster should carry that audience, never the API server's.
- Opt out: set
automountServiceAccountToken: falseon the ServiceAccount or pod when the workload does not call the API. - Where the file lives by default: if you do nothing, the default token is mounted under
/var/run/secrets/kubernetes.io/serviceaccount/tokenwith the audience of the API server, so any in-cluster client library finds it automatically.
Failure modes: legacy secrets, extended lifetime and 401s
Most real incidents come from tokens that either live too long or are cached too long.
- Legacy Secrets: list them with
kubectl get secrets -A --field-selector type=kubernetes.io/service-account-token. Each one is a non-expiring credential that grants the ServiceAccount's RBAC to anyone who reads it. - Extended lifetime: to ease the migration to bound tokens, the API server can accept tokens past
expfor a longer window and annotates such use in audit logs. Treat audit entries about stale tokens as a to-do list of clients to fix. - Token cached in code: an app returns 401 roughly an hour after start. Fix the client to reload the file, or use an up-to-date client library that does.
- Verification path: a
TokenReviewrequest lets an external service ask the API server whether a token is valid for a given audience. - Pod deletion: a bound token stops validating once its pod is gone, so a token exfiltrated from a short-lived pod is far less useful than a legacy Secret token.
Production practice: least privilege and auditing
Short-lived tokens only help if the permissions behind them are small.
- Dedicated ServiceAccounts: one per workload, never the
defaultaccount, with minimal Role bindings. - Check permissions:
kubectl auth can-i --list --as=system:serviceaccount:prod:my-sa -n prod. - Prefer workload identity: for cloud APIs, federate the projected token (for example IRSA on EKS or Workload Identity on GKE) rather than storing static keys.
- Audit: alert on creation of
kubernetes.io/service-account-tokenSecrets. - Rotation policy: shorter lifetimes reduce exposure but raise renewal traffic, so pick a duration your clients can refresh reliably, and test the refresh path in staging before tightening it.
- A ServiceAccount token is a signed JWT identifying a pod to the API server; modern ones are bound, audience-scoped and short-lived.
- Projected tokens default to about an hour and are rotated by the kubelet, while legacy Secret tokens never expire; I verify by decoding the exp claim.
- I automate audits for legacy token Secrets, disable automount where unused, and federate to cloud IAM instead of static keys.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Creating a kubernetes.io/service-account-token Secret for convenience It yields a credential that never expires and is valid until someone deletes it.
- Reading the projected token once at start-up The kubelet rotates the file, so a cached token starts producing 401 errors after it expires.
- Sharing the default ServiceAccount across workloads Any RBAC grant to it applies to every pod that uses it, and token compromise has a bigger blast radius.
- Using the API server audience for third-party systems A token meant for the API server can be replayed against it if the third party leaks it.