Service Account Security and Token Management
What You'll Learn
You'll learn how to properly configure Kubernetes service accounts, prevent automatic token mounting, and use projected volumes to securely deliver authentication tokens to pods.
Understanding Service Account Tokens
Every pod in Kubernetes runs with a service account—essentially an identity for workloads to authenticate with the API server. By default, Kubernetes automatically mounts a long-lived JWT token into every pod at /var/run/secrets/kubernetes.io/serviceaccount/token.
Think of this like leaving a building access card inside every office, even when most offices never need one. If an attacker compromises a pod, they inherit that service account's permissions—potentially allowing lateral movement or privilege escalation within your cluster.
Secure Configuration Strategies
Disable Auto-Mounting When Unused
Many pods don't need API access at all. Disable automatic token mounting at the pod or service account level:
apiVersion: v1
kind: ServiceAccount
metadata:
name: no-api-access
automountServiceAccountToken: false
Or per-pod:
spec:
serviceAccountName: my-sa
automountServiceAccountToken: false
Use Projected Volumes with Short-Lived Tokens
For pods that do need API access, use projected volumes to mount time-bound tokens that automatically rotate:
spec:
volumes:
- name: sa-token
projected:
sources:
- serviceAccountToken:
path: token
expirationSeconds: 3600
audience: api
containers:
- volumeMounts:
- mountPath: /var/run/secrets/tokens
name: sa-token
This approach limits the blast radius if a token is compromised—it expires quickly and can't be reused indefinitely.
Apply Least Privilege
Bind service accounts only to the minimal RBAC roles they require, never use cluster-admin for application workloads.
Key Takeaway: Disable automatic service account token mounting where unnecessary, use projected volumes with short-lived tokens when API access is required, and always apply least-privilege RBAC bindings to limit the impact of compromised credentials.