Skip to main content

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.

flowchart TD P["Pod with
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

YAML
apiVersion: v1
kind: ServiceAccount
metadata:
name: prometheus-scraper
namespace: monitoring
YAML
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: prometheus-metrics-reader
rules:
- apiGroups: [""]
resources: ["nodes", "pods", "services", "endpoints"]
verbs: ["get", "list", "watch"]
YAML
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: prometheus-scraper-binding
subjects:
- kind: ServiceAccount
name: prometheus-scraper
namespace: monitoring
roleRef:
kind: ClusterRole
name: prometheus-metrics-reader
apiGroup: rbac.authorization.k8s.io

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

Bash
kubectl create token prometheus-scraper -n monitoring --duration=1h
kubectl auth can-i get pods --as=system:serviceaccount:monitoring:prometheus-scraper

Disable the token when unused

YAML
spec:
serviceAccountName: payments-api-sa
automountServiceAccountToken: false

Troubleshooting

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
Security

Give each workload its own ServiceAccount with least privilege, and never rely on the default one for anything sensitive.

Common Mistake

Creating a ServiceAccount but forgetting serviceAccountName in the pod spec. The pod silently runs as default.

Tip

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

Found a mistake, an outdated step or a broken link?