Sub topic 1
What a ServiceAccount is
Prerequisites
This chapter assumes you already know:
- Pods, Deployments, and namespaces.
- How to apply YAML manifests with
kubectl. - The difference between authentication (who are you?) and authorization (what may you do?).
The working model
A ServiceAccount is a namespaced Kubernetes object that gives a workload an identity. Human users are different. Kubernetes has no User object, because people are authenticated by an outside system such as a certificate authority or an identity provider. ServiceAccounts are the one identity type you create and manage through the Kubernetes API.
Each ServiceAccount has a fixed username in this form:
system:serviceaccount:<namespace>:<name>For example, a ServiceAccount named app-reader in the demo namespace is system:serviceaccount:demo:app-reader. Later chapters use this string in RBAC checks and in cloud trust policies, so it helps to recognize it now.
A ServiceAccount does not hold permissions. It only names an identity. Permissions come from RBAC bindings that refer to that identity, which a later chapter covers.
The default ServiceAccount
Kubernetes creates a ServiceAccount named default in every namespace. If a Pod does not set spec.serviceAccountName, it runs as default in its own namespace.
A fresh default account has no permissions beyond the basic access every authenticated identity gets. The risk is that every Pod that skips the field shares one identity. If someone later grants default a permission, all of those Pods inherit it. Giving each workload its own ServiceAccount avoids this.
Creating a ServiceAccount
Create the namespace, then apply this manifest:
kubectl create namespace demoapiVersion: v1
kind: ServiceAccount
metadata:
name: app-reader
namespace: demo
automountServiceAccountToken: falseThe automountServiceAccountToken: false line tells Kubernetes not to mount an API token into Pods that use this account. Chapters on tokens and hardening explain why this matters. For now, treat it as a safe default. A Pod that needs API access can still turn the token on in its own spec.
You can list and inspect ServiceAccounts like any other object:
kubectl get serviceaccounts -n demo
kubectl describe serviceaccount app-reader -n demoAttaching it to a Pod
A Pod selects its identity with serviceAccountName:
apiVersion: v1
kind: Pod
metadata:
name: sa-demo
namespace: demo
spec:
serviceAccountName: app-reader
containers:
- name: app
image: registry.example.com/app:1.0In a Deployment, set the field under spec.template.spec. Changing it there triggers a normal rollout.
Common errors
- Not found: The Pod is rejected if the named ServiceAccount does not exist in its namespace. Create the ServiceAccount first.
- Wrong namespace: A Pod cannot use a ServiceAccount from another namespace. The name is always resolved in the Pod's own namespace.
- Editing a running Pod: You cannot change
serviceAccountNameon an existing Pod. Change the Deployment template, or delete and recreate the Pod. - Everything on
default: Pods work without errors when they sharedefault, so the problem goes unnoticed until permissions are granted.
Exercise
A Pod in namespace shop omits serviceAccountName. What identity does it run as, and why is that a concern?
Show answer
It runs as system:serviceaccount:shop:default. Every Pod in shop that omits the field shares that identity, so any permission later granted to default applies to all of them. A dedicated ServiceAccount per workload avoids this.