Skip to content
Tutorials Center
My learning

Topics

Lesson 47 of 78 ยท Security

Service Accounts and Workload Identity

How pods get an identity in Kubernetes: ServiceAccounts, projected tokens, automounting, and cloud workload identity such as IRSA and GKE Workload Identity.

People log in to a cluster with certificates or OIDC. But software running inside the cluster also needs to talk to the API server: a controller lists pods, a CI agent patches a Deployment, an operator watches custom resources. These workloads need an identity too, and Kubernetes gives them one through the ServiceAccount object.

A ServiceAccount is the one kind of identity that exists as a real API object. In this lesson you will see how a pod receives a token, how modern clusters make that token short-lived and audience-bound, why you should often switch it off, and how the same idea extends to the cloud, so a pod can read an S3 bucket without any stored access key. The permissions themselves come from RBAC; this lesson is about the identity those permissions attach to.

ServiceAccounts in a nutshell

Every namespace has a ServiceAccount called default. A pod that does not name one uses it. You create more with a tiny manifest:

YAML
apiVersion: v1
kind: ServiceAccount
metadata:
  name: report-builder
  namespace: shop
automountServiceAccountToken: false

A pod then selects it with spec.serviceAccountName:

YAML
apiVersion: v1
kind: Pod
metadata:
  name: report-job
  namespace: shop
spec:
  serviceAccountName: report-builder
  containers:
    - name: app
      image: example/report-builder:1.4

The API server sees requests from this pod as the user system:serviceaccount:shop:report-builder, belonging to the groups system:serviceaccounts and system:serviceaccounts:shop. Those are the names you write in a RoleBinding's subjects.

How a pod gets its token

Pod is created It names a ServiceAccount.
Kubelet asks It requests a token from the API server.
Token is mounted A projected volume file inside the container.
Kubelet renews The file is refreshed before it expires.
1 / 2

Projected tokens

Early Kubernetes created a Secret holding a permanent token for each ServiceAccount. Anyone who copied it had access forever, until the ServiceAccount was deleted. That design was replaced by bound service account tokens through the TokenRequest API:

  • The token is a signed JWT (JSON Web Token) issued by the API server.
  • It is time-limited. The kubelet requests it with an expiry (an hour by default for a projected volume) and renews it at about 80 percent of its life.
  • It is bound to the pod. When the pod is deleted, the token becomes invalid.
  • It has an audience (aud), which says who the token is meant for. The API server only accepts tokens whose audience includes itself, so a token meant for another service cannot be replayed against the API.

The kubelet mounts the token at a well-known path inside every container:

TEXT
/var/run/secrets/kubernetes.io/serviceaccount/token      the JWT
/var/run/secrets/kubernetes.io/serviceaccount/ca.crt     the cluster CA certificate
/var/run/secrets/kubernetes.io/serviceaccount/namespace  the pod's namespace

Client libraries (client-go, the Python kubernetes package, and others) read these files automatically in "in-cluster" mode. You can also request a token yourself for testing, a read-only command in this lesson:

CONSOLE
$ kubectl create token report-builder -n shop --duration 10m
eyJhbGciOiJSUzI1NiIsImtpZCI6Ii4uLiJ9.eyJhdWQiOlsi...

You can also define your own projected volume to get a token with a custom audience and lifetime, which is how you authenticate to systems other than the API server (Vault, a service mesh, a cloud provider):

YAML
volumes:
  - name: vault-token
    projected:
      sources:
        - serviceAccountToken:
            audience: vault
            expirationSeconds: 600
            path: token

Do you need the token at all?

Most application pods never call the Kubernetes API. A web server that renders HTML has no use for a token, and a token it does not need is a credential that can be stolen if the app has a vulnerability. The fix is to turn off automounting:

YAML
spec:
  automountServiceAccountToken: false

Set it on the ServiceAccount to disable it for all pods that use the account, or on the pod spec to override just one pod. A good baseline for a shared cluster: every namespace gets its own default ServiceAccount with automounting off, no permissions bound to it, and workloads that really need API access get a dedicated account.

Reading a token: what is inside?

A JWT has three parts separated by dots: a header, a payload of claims and a signature. Each part is base64url-encoded JSON (except the signature). The claims of a projected token look roughly like this:

JSON
{
  "aud": ["https://kubernetes.default.svc"],
  "exp": 1893459600,
  "iat": 1893456000,
  "iss": "https://kubernetes.default.svc",
  "sub": "system:serviceaccount:shop:report-builder",
  "kubernetes.io": {
    "namespace": "shop",
    "serviceaccount": {"name": "report-builder", "uid": "0b0f..."},
    "pod": {"name": "report-job", "uid": "7c1e..."}
  }
}

The real signature uses an RSA or ECDSA private key held by the control plane, and verification uses the public key. The stand-in below uses a shared secret with HMAC-SHA256 so it can run in your browser using only the standard library. The format and the claims logic are the same idea; the signing algorithm is a simplification. It builds a token, decodes it and applies the same checks the API server makes: signature, expiry and audience.

Python Decoding and validating a JWT-like token

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line shows why signing matters. Anyone can edit the payload (it is only base64), but without the key they cannot produce a matching signature, so the forged identity is rejected. Never trust a token's claims before verifying its signature: decoding is not verifying.

Workload identity in the cloud

Pods often need to call cloud services: read a bucket, publish to a queue, fetch a secret. The old way was to put a long-lived cloud access key in a Kubernetes Secret. That key can leak, never rotates by itself and is shared by everything that can read the Secret.

Workload identity replaces it with a trust relationship. The cloud provider is told to trust the cluster's token issuer (OIDC). The pod's projected token, with the cloud provider as the audience, is exchanged for short-lived cloud credentials. No static secret is stored anywhere.

  • AWS (IRSA, and EKS Pod Identity). You annotate a ServiceAccount with the ARN of an IAM role. The pod receives a token with audience sts.amazonaws.com, and the AWS SDK exchanges it through STS for temporary credentials. The IAM role's trust policy names the exact namespace and ServiceAccount allowed to assume it.
  • Google Cloud (GKE Workload Identity). A Kubernetes ServiceAccount is linked to a Google identity, so the pod is treated as that identity when calling Google APIs.
  • Azure (Entra Workload ID). A ServiceAccount is federated with a managed identity or app registration.
YAML
apiVersion: v1
kind: ServiceAccount
metadata:
  name: uploader
  namespace: shop
  annotations:
    eks.amazonaws.com/role-arn: arn:aws:iam::123456789012:role/shop-uploader

The pattern is the same everywhere: one ServiceAccount per workload, one narrowly scoped cloud role per ServiceAccount, and the trust restricted to that exact pair. The stand-in below mimics the trust check a cloud's token exchange makes: it only hands out credentials when the token's subject matches the role's trust policy.

Python A trust policy check for workload identity

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

What is real and what is the stand-in: the claims, the idea of audiences and the trust-policy matching are exactly how real clouds work. The actual exchange happens over HTTPS with the provider's token service, and real tokens are RSA-signed.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the main advantage of a projected (bound) service account token over a legacy Secret-based token?

    Bound tokens are time-limited, invalid after the pod is deleted and only accepted by services in the audience. Legacy tokens never expired.
  2. 2. Your web app never calls the Kubernetes API. What is the best practice?

    A token that is not needed should not be mounted. If the application has a vulnerability, there is then no credential to steal.
  3. 3. Decoding the payload of a JWT is enough to know the token is genuine.

    The payload is merely encoded. Only a successful signature check proves the issuer created it.
  4. 4. How does IRSA or GKE Workload Identity avoid storing cloud access keys in the cluster?

    The trust relationship is configured once in the cloud. Pods present their projected token and receive short-lived credentials, so no static key exists.

Summary

  • ServiceAccounts are the identity objects for workloads; requests appear as system:serviceaccount:<namespace>:<name>.
  • Modern tokens are projected, short-lived, bound to a pod and carry an audience; the kubelet mounts and renews them.
  • Disable automountServiceAccountToken for pods that do not call the API, and give each workload its own ServiceAccount.
  • A JWT is three base64url parts; decoding is not verifying, so always check the signature, expiry and audience.
  • Cloud workload identity (IRSA, GKE Workload Identity, Entra Workload ID) exchanges a pod token for temporary credentials, with trust limited to one namespace and ServiceAccount.

Next, tighten what the containers themselves may do: Pod Security and Security Contexts.