ServiceAccount
A ServiceAccount is a Kubernetes identity that pods use when calling the Kubernetes API, and it is the subject that RBAC permissions are granted to. Every pod runs under one, and if you do not name one, the namespace's default ServiceAccount is used.
What it gives a pod
When a pod calls the API server, it presents a token tied to its ServiceAccount, and RBAC decides whether the call is allowed. Think of it as an identity for a workload, much like an IAM role for an application.
ServiceAccount"] -->|"bearer token"| A["API server"] A --> R{"RBAC check"} R -->|"allowed"| OK["200 OK"] R -->|"denied"| NO["403 Forbidden"] classDef pod fill:#475569,color:#ffffff classDef api fill:#326CE5,color:#ffffff classDef decide fill:#FFEDD5,color:#1E293B classDef ok fill:#D1FAE5,color:#1E293B classDef bad fill:#FEE2E2,color:#1E293B class P pod class A api class R decide class OK ok class NO bad
Create a scoped identity
apiVersion: v1kind: ServiceAccountmetadata: name: prometheus-scraper namespace: monitoringapiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata: name: prometheus-metrics-readerrules: - apiGroups: [""] resources: ["nodes", "pods", "services", "endpoints"] verbs: ["get", "list", "watch"]apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata: name: prometheus-scraper-bindingsubjects: - kind: ServiceAccount name: prometheus-scraper namespace: monitoringroleRef: kind: ClusterRole name: prometheus-metrics-reader apiGroup: rbac.authorization.k8s.ioReference it in the pod template with serviceAccountName: prometheus-scraper.
Tokens
Current Kubernetes versions mount short-lived, automatically rotated, audience-bound tokens through a projected volume at /var/run/secrets/kubernetes.io/serviceaccount/. Long-lived token Secrets are no longer created automatically.
kubectl create token prometheus-scraper -n monitoring --duration=1hkubectl auth can-i get pods --as=system:serviceaccount:monitoring:prometheus-scraperDisable the token when unused
spec: serviceAccountName: payments-api-sa automountServiceAccountToken: falseTroubleshooting
| Problem | Likely cause | Fix |
|---|---|---|
| Pod gets 403 | SA lacks permission | Test with auth can-i, add a binding |
| Pod will not start | SA does not exist | Create the SA first |
| Wrong access | serviceAccountName omitted | Pod used the default SA |
SecurityGive each workload its own ServiceAccount with least privilege, and never rely on the default one for anything sensitive.
Common MistakeCreating a ServiceAccount but forgetting
serviceAccountNamein the pod spec. The pod silently runs as default.
TipOn EKS, bind a ServiceAccount to an IAM role (IRSA or Pod Identity) so pods get AWS permissions without static keys.
Frequently Asked Questions
What happens if I never create a ServiceAccount?
Pods use the namespace's default ServiceAccount. It normally has no meaningful RBAC permissions, so API calls fail with 403, though the token is still mounted unless you disable it.
What is the best practice for assigning ServiceAccounts?
One dedicated ServiceAccount per application, bound to the narrowest role that works, and no token mounted when the app never calls the Kubernetes API.
Are ServiceAccounts used for anything besides the Kubernetes API?
Yes. Meshes derive workload identity from them for mTLS, and cloud integrations use them to map pods to cloud IAM roles.