Skip to content
intermediate Phase 7 · Security

ServiceAccounts

Manage pod identities and API authentication with ServiceAccounts and tokens.

45m
0 problems
Topic Progress 0%

Identity for Workloads

Identity for Workloads

Humans authenticate with OIDC or client certificates; pods get a ServiceAccount. Every namespace ships a default account, and pods that specify nothing use it — a frequent source of surprising 403s for apps that never opted in.

On startup, kubelet projects a token into the container at /var/run/secrets/kubernetes.io/serviceaccount. Modern clusters (1.24+) request short-lived tokens through the TokenRequest API — typically one hour, refreshed by kubelet — instead of creating permanent token Secrets.

The token identifies system:serviceaccount:<namespace>:<name> to the API server, where RBAC decides what that identity may do. The same name can also bind a cloud IAM role through workload identity integrations.

Wiring Identity Correctly

Wiring Identity Correctly

spec:
  serviceAccountName: billing-api
  automountServiceAccountToken: false   # for apps that never call the API
  containers:
  - name: app
    image: registry.example.com/billing:1.2

Create the account and its permissions:

kubectl create serviceaccount billing-api -n prod
kubectl create rolebinding billing-api-read \
  --clusterrole=view --serviceaccount=prod:billing-api

When a token mount is needed (for the API or an external audience), declare a projected volume with serviceAccountToken, setting expirationSeconds and audience explicitly. Out-of-cluster automation should prefer OIDC federation over minting long-lived static tokens.

Pitfalls and Interview Angles

Pitfalls and Interview Angles

Pitfall 1 — automount on everything. Any pod running the default account carries a usable token; a compromised web container inherits whatever that SA can do. Set automountServiceAccountToken: false for workloads that never call the API.

Pitfall 2 — audience mismatches. A token minted for a cloud-identity audience is rejected by the API server, which expects its own — TokenReview fails even though the signature is valid. Match audience to consumer.

Interview-ready: contrast human and workload identity, explain why Kubernetes moved to short-lived TokenRequest tokens (static Secret tokens were leaky and permanent), and list hardening moves: one SA per app, least-privilege binding, automount disabled where unused, projected tokens with explicit audiences.

Quiz

1. Which ServiceAccount does a pod use if serviceAccountName is omitted?

Question 1 options

2. How do modern clusters issue SA tokens (1.24+)?

Question 2 options

3. When should automountServiceAccountToken be false?

Question 3 options

Flashcards

Question

ServiceAccount identity form

Answer

system:serviceaccount:<namespace>:<name>, resolved by RBAC on every API call.

Question

TokenRequest tokens

Answer

Short-lived (~1h default), kubelet-refreshed, audience-scoped — the 1.24+ standard.

Question

automountServiceAccountToken

Answer

Set false on tokenless apps so they never carry credentials they do not use.

Question

Projected token volume

Answer

Declares serviceAccountToken source with explicit expirationSeconds and audience.

Revision Notes

Key Takeaways

  • 1. Pods are service accounts; the default SA is a quiet privilege risk
  • 2. Tokens are short-lived and audience-scoped by design
  • 3. Identity wiring and RBAC grants are one conversation, not two

Interview Tips

  • • Explain the 1.24 shift away from auto-created token Secrets
  • • Describe hardening a compromised-container scenario with SA permissions

Cheat Sheet

Pods auth as system:serviceaccount:ns:name. 1.24+ = TokenRequest short-lived projected tokens. Set automountServiceAccountToken:false when unused; bind least privilege.

Continue Learning