Difficulty: Intermediate
How does a Pod authenticate to the Kubernetes API server, and what is a ServiceAccount token?
A Pod authenticates to the Kubernetes API using the token of its ServiceAccount. The kubelet mounts a short-lived, Pod-bound JWT at /var/run/secrets/kubernetes.io/serviceaccount/token (alongside ca.crt and namespace), and client libraries send it as a Bearer token to the API server, which verifies it and then applies RBAC to the identity system:serviceaccount:<namespace>:<name>.
Unless you opt out, every Pod runs as a ServiceAccount. If you do not set spec.serviceAccountName, it uses the default ServiceAccount of its namespace. The ServiceAccount admission controller adds a projected volume that contains three files:
token: a signed JWT issued through the TokenRequest APIca.crt: the cluster CA bundle, so the client can verify the API server's TLS certificatenamespace: the Pod's namespace, handy for building API pathsThe API server address comes from the environment variables KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT, which point at the kubernetes Service in the default namespace. Libraries such as client-go (rest.InClusterConfig()) and the Python client (config.load_incluster_config()) read all of this automatically.
Modern clusters use bound, projected tokens, and this changed how the feature works:
| Aspect | Legacy token (before 1.22/1.24) | Bound projected token (current) |
|---|---|---|
| Where it lives | A Secret of type kubernetes.io/service-account-token | Projected volume written by the kubelet |
| Expiry | Never expires | Time limited, refreshed by the kubelet |
| Bound to | Only the ServiceAccount | The ServiceAccount and the specific Pod |
| Revocation | Delete the Secret | Invalid once the Pod is deleted |
Since Kubernetes 1.24, creating a ServiceAccount no longer auto-generates a long-lived token Secret. If you need a token outside a Pod, run kubectl create token <sa-name>, which returns a short-lived token. The kubelet rotates the projected token before it expires, and current client libraries re-read the file, so long-running apps keep working.
A valid token only proves who the Pod is. What it can do is decided by RBAC. A fresh ServiceAccount has almost no permissions, so a Pod that lists Pods needs a Role and a RoleBinding (or the cluster-wide versions). Grant the narrowest verbs and resources possible; a ServiceAccount bound to cluster-admin turns any compromised container into a compromised cluster.
automountServiceAccountToken: false on Pods (or the ServiceAccount) that never call the API. Most web apps, like a Swiggy order service, do not need it.default, so RBAC and audit logs are meaningful.audience and expirationSeconds, which is how cloud workload identity works: EKS IRSA / Pod Identity, GKE Workload Identity and AKS Workload Identity exchange the Kubernetes token for cloud credentials without storing access keys in the cluster.apiVersion: v1
kind: ServiceAccount
metadata:
name: pod-reader
namespace: payments
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: read-pods
namespace: payments
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: pod-reader-binding
namespace: payments
subjects:
- kind: ServiceAccount
name: pod-reader
namespace: payments
roleRef:
kind: Role
name: read-pods
apiGroup: rbac.authorization.k8s.io
Pods that set serviceAccountName: pod-reader can read Pods in the payments namespace and nothing else.
# Run inside a container
SA=/var/run/secrets/kubernetes.io/serviceaccount
TOKEN=$(cat $SA/token)
NS=$(cat $SA/namespace)
curl --cacert $SA/ca.crt \
-H "Authorization: Bearer $TOKEN" \
https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_SERVICE_PORT/api/v1/namespaces/$NS/pods
# From your laptop: check what the ServiceAccount is allowed to do
kubectl auth can-i list pods -n payments \
--as=system:serviceaccount:payments:pod-reader
# Get a short-lived token for testing (1.24+)
kubectl create token pod-reader -n payments --duration=10m
This is exactly what client libraries do under the hood. kubectl auth can-i with --as is the fastest way to debug a 403 Forbidden.
ServiceAccount, Projected Token Volume, TokenRequest API, RBAC, In-cluster Config