ServiceAccount Token

Difficulty: Intermediate

Question

How does a Pod authenticate to the Kubernetes API server, and what is a ServiceAccount token?

Answer

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>.

What gets mounted into every Pod

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 API
  • ca.crt: the cluster CA bundle, so the client can verify the API server's TLS certificate
  • namespace: the Pod's namespace, handy for building API paths

The 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.

Bound tokens vs the old Secret-based tokens

Modern clusters use bound, projected tokens, and this changed how the feature works:

AspectLegacy token (before 1.22/1.24)Bound projected token (current)
Where it livesA Secret of type kubernetes.io/service-account-tokenProjected volume written by the kubelet
ExpiryNever expiresTime limited, refreshed by the kubelet
Bound toOnly the ServiceAccountThe ServiceAccount and the specific Pod
RevocationDelete the SecretInvalid 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.

Authentication is not authorization

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.

Hardening and real-world use

  • Set automountServiceAccountToken: false on Pods (or the ServiceAccount) that never call the API. Most web apps, like a Swiggy order service, do not need it.
  • Create one ServiceAccount per workload instead of reusing default, so RBAC and audit logs are meaningful.
  • Projected tokens can carry a custom 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.
  • Controllers, operators, CI runners like Argo CD and Prometheus all authenticate exactly this way.
How a Pod calls the Kubernetes API. The kubelet supplies a bound token, the API server authenticates it and RBAC decides what is allowed.
  1. Kubelet → API Server: TokenRequest for the Pod's ServiceAccount
  2. API Server → Kubelet: Signed, time-limited JWT
  3. Kubelet → Pod: Mount token, ca.crt and namespace
  4. Pod → API Server: GET /api/v1/pods with Bearer token
  5. API Server → RBAC: Can this ServiceAccount list pods?
  6. RBAC → API Server: Allowed by RoleBinding
  7. API Server → Pod: 200 OK with pod list

Code examples

ServiceAccount with least-privilege RBAC

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.

Calling the API from inside a Pod

# 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.

Key points

  • Every Pod runs as a ServiceAccount; the namespace's default one is used if none is set
  • The token is a projected, Pod-bound, time-limited JWT, not a never-expiring Secret
  • Since 1.24 no token Secret is auto-created; use kubectl create token for a manual token
  • Authentication identifies the Pod as system:serviceaccount:<ns>:<name>; RBAC decides permissions
  • Disable automounting with automountServiceAccountToken: false when the app never calls the API

Common follow-up questions

  • How would you give a Pod access to an S3 bucket or GCS bucket without storing cloud keys?
  • What happens to the token when the Pod is deleted?
  • How do you debug a 403 Forbidden from inside a Pod?
  • What is the difference between a User and a ServiceAccount in Kubernetes?

Interview tips

  • Separate the two steps clearly: the token authenticates, RBAC authorizes
  • Mention the 1.24 change and bound tokens; it shows you know current behaviour, not a 2019 blog post
  • Bring up automountServiceAccountToken: false and least privilege as security wins

Concepts covered

ServiceAccount, Projected Token Volume, TokenRequest API, RBAC, In-cluster Config

More Kubernetes & Orchestration questions