πŸ“šBook Signing at KubeCon EU 2026Meet us at Booking.com HQ (Mon 18:30-21:00) & vCluster booth #521 (Tue 24 Mar, 12:30-1:30pm) β€” free book giveaway!RSVP Booking.com Event
Security intermediate ⏱ 15 minutes K8s 1.28+

Kubernetes Service Accounts Guide

Create and manage Kubernetes service accounts for pod identity. Covers RBAC binding, token projection, workload identity, and least-privilege access

By Luca Berton β€’ β€’ πŸ“– 5 min read

πŸ’‘ Quick Answer: security

The Problem

This is a fundamental Kubernetes topic that engineers search for frequently. A comprehensive reference with production-ready examples saves hours of trial and error.

The Solution

Create a Service Account

apiVersion: v1
kind: ServiceAccount
metadata:
  name: my-app
  namespace: default
  annotations:
    # AWS IRSA
    eks.amazonaws.com/role-arn: arn:aws:iam::123456789:role/my-app-role
    # GCP Workload Identity
    iam.gke.io/gcp-service-account: my-app@project.iam.gserviceaccount.com
---
# Bind permissions
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: my-app-role
rules:
  - apiGroups: [""]
    resources: ["configmaps"]
    verbs: ["get", "list"]
  - apiGroups: [""]
    resources: ["secrets"]
    resourceNames: ["my-app-config"]   # Only specific secret
    verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: my-app-binding
subjects:
  - kind: ServiceAccount
    name: my-app
roleRef:
  kind: Role
  name: my-app-role
  apiGroup: rbac.authorization.k8s.io

Use in Pods

apiVersion: v1
kind: Pod
spec:
  serviceAccountName: my-app      # Use specific SA
  automountServiceAccountToken: false  # Disable if not needed!
  containers:
    - name: app
      image: my-app:v1

Projected Token (Short-Lived)

# K8s 1.20+ bound tokens (auto-rotated, audience-scoped)
spec:
  containers:
    - name: app
      volumeMounts:
        - name: token
          mountPath: /var/run/secrets/tokens
  volumes:
    - name: token
      projected:
        sources:
          - serviceAccountToken:
              path: vault-token
              expirationSeconds: 3600
              audience: vault
# Check SA permissions
kubectl auth can-i --list --as=system:serviceaccount:default:my-app
kubectl auth can-i get secrets --as=system:serviceaccount:default:my-app
graph TD
    A[Service Account: my-app] --> B[RoleBinding]
    B --> C[Role: read configmaps + specific secret]
    D[Pod: serviceAccountName: my-app] --> E[Gets projected token]
    E --> F[Can call K8s API with SA permissions]
    A --> G[Cloud annotation: AWS IRSA / GCP WI]
    G --> H[Pod can access cloud resources]

Frequently Asked Questions

Should every app have its own service account?

Yes β€” using the default SA means every pod in the namespace shares the same identity. Create dedicated SAs with minimal permissions per application.

What is automountServiceAccountToken: false?

It prevents the SA token from being mounted into the pod. Use this for pods that don’t need to call the Kubernetes API β€” reduces attack surface.

Best Practices

  • Start with the simplest configuration that meets your needs
  • Test changes in staging before production
  • Use kubectl describe and events for troubleshooting
  • Document your decisions for the team

Key Takeaways

  • This is essential Kubernetes knowledge for production operations
  • Follow the principle of least privilege and minimal configuration
  • Monitor and iterate based on real-world behavior
  • Automation reduces human error and improves consistency
#service-account #rbac #tokens #workload-identity #security
Luca Berton
Written by Luca Berton

Principal Solutions Architect specializing in Kubernetes, AI/GPU infrastructure, and cloud-native platforms. Author of Kubernetes Recipes and creator of CopyPasteLearn courses.

Kubernetes Recipes book cover

Want More Kubernetes Recipes?

This recipe is from Kubernetes Recipes, our 750-page practical guide with hundreds of production-ready patterns.

Luca Berton Ansible Pilot Ansible by Example Open Empower K8s Recipes Terraform Pilot CopyPasteLearn ProteinLens