Concept and usage
Service accounts can be used to create a non-human identity (user equivalent) for processes and applications in Kubernetes.
| ServiceAccount | User or group | |
|---|---|---|
| Location | Kubernetes API (ServiceAccount object) | External |
| Access control | Kubernetes RBAC or other authorization mechanisms | Kubernetes RBAC or other identity and access management mechanisms |
| Intended use | Workloads, automation, third-party applications | People |
- Every namespace automatically gets a default service account.
-
When a pod is created, Kubernetes mounts the service account token as a volume at
/var/run/secrets/kubernetes.io/serviceaccount. That path contains atoken,ca.crt, andnamespacefile. -
To prevent auto-mounting, set
automountServiceAccountToken: falsein the pod spec:pod.yaml - The default service account has limited permissions for basic Kubernetes API queries only.
- To change the service account of an existing pod, you must delete and recreate the pod. For Deployments, editing the deployment will trigger a rolling update automatically.
Creating and using a service account
Generate a token for the service account
This token is used by the application to authenticate with the Kubernetes API server. By default, the token is valid for 1 hour.
Creating a long-lived API token
If you need a non-expiring token for a service account, create a Secret and associate it with the service account.Create a service account
- Without pre-populating secrets field
- With pre-populating secrets field
data-sa.yaml
secrets field (only auto-generated secrets do that).Adding imagePullSecrets to a service account
You can attachimagePullSecrets to a service account so that any pod using that service account can pull images from a private registry.
Assuming you have a secret named my-registry with credentials for your private Docker registry:
- Using kubectl patch
- Using kubectl edit
imagePullSecrets are set for new pods using this service account: