Working with Service Accounts

1. Creating Service Account

kubectl create sa app-sa
kubectl create sa app-sa -n payments
apiVersion: v1
kind: ServiceAccount
metadata:
  name: app-sa
  namespace: payments
  annotations:
    eks.amazonaws.com/role-arn: arn:aws:iam::111:role/app
automountServiceAccountToken: false

2. Listing Service Accounts

kubectl get sa
kubectl get sa -A
kubectl get sa app-sa -o yaml
ColumnMeaning
SECRETSLegacy auto-mounted token secret count (1.24-)
AGECreation time

3. Describing Service Account Details

FieldInfo
TokensBound tokens (1.24+, projected, not Secret)
Image pull secretsAuto-injected to pods
Mountable secretsAllowed for pods (security)

4. Using Service Account in Pod

spec:
  serviceAccountName: app-sa
  automountServiceAccountToken: true
EffectDetail
Mounts/var/run/secrets/kubernetes.io/serviceaccount/
Filestoken, ca.crt, namespace

5. Understanding Default Service Account

DetailValue
Namedefault (auto-created per namespace)
PermissionsNone by default
Used whenPod omits serviceAccountName

6. Configuring Automount Token

LocationPrecedence
SA specDefault for all pods using SA
Pod specOverrides SA setting
Note: Disable token mount for workloads that don't call the API to reduce blast radius.

7. Creating Service Account Token

kubectl create token app-sa --duration=1h
kubectl create token app-sa --audience=vault --bound-object-kind=Pod --bound-object-name=web-abc
FlagEffect
--durationTTL (default 1h, max from apiserver flag)
--audienceToken audience (for OIDC/Vault)
--bound-object-*Token invalid when object deleted

8. Using Bound Service Account Tokens

volumes:
- name: sa-token
  projected:
    sources:
    - serviceAccountToken:
        audience: vault
        expirationSeconds: 3600
        path: token
PropertyDetail
Bound toPod (auto-rotated, invalidated on delete)
AudienceRestricts token validity
StableDefault since 1.21

9. Configuring Token Expiration

WhereSetting
Pod projectionexpirationSeconds (min 600)
kubectl create token--duration
API server--service-account-max-token-expiration

10. Deleting Service Accounts

kubectl delete sa app-sa
Warning: Deleting an SA invalidates pods' tokens at next rotation; pods using API calls will fail.