K8s by Example: Service Accounts
| ServiceAccounts provide identity for Pods. Pods use them to authenticate to the Kubernetes API and external services (via OIDC/IRSA). Each namespace has a default SA. Token is automatically projected into the Pod. |
| serviceaccount.yaml | |
| ServiceAccount is a namespaced resource. Create dedicated ServiceAccounts for different workloads instead of using the default. Token is mounted at /var/run/secrets/kubernetes.io/serviceaccount/. | |
| pod-serviceaccount.yaml | |
| Assign a ServiceAccount to Pods with | |
| serviceaccount-notoken.yaml | |
| Disable auto-mounting of API token if not needed. Reduces attack surface for Pods that don’t use the API. Can set on ServiceAccount or Pod level. | |
| serviceaccount-projected.yaml | |
| Projected service account tokens are time-limited and audience-bound. More secure than legacy tokens. Kubernetes 1.22+ uses projected tokens by default with 1-hour expiry (auto-rotated by kubelet). | |
| serviceaccount-irsa.yaml | |
| AWS IRSA (EKS) lets Pods assume IAM roles using OIDC federation. Annotate ServiceAccount with role ARN. Pod gets AWS credentials automatically without access keys. | |
| serviceaccount-gcp.yaml | |
| GCP Workload Identity binds Kubernetes ServiceAccounts to Google Cloud service accounts. Similar to IRSA, no keys needed. Annotate the ServiceAccount and create IAM binding. | |
| serviceaccount-pull-secrets.yaml | |
| Image pull secrets can be attached to ServiceAccounts. All Pods using that ServiceAccount automatically get the pull secrets. Useful for private registries. | |
| terminal | |
| Debug ServiceAccount issues by checking token mounts and RBAC. Token must be present and valid. RBAC must grant required permissions to the ServiceAccount. | |