ServiceAccount Practical Scenarios

Theory is clear. Now look at two concrete scenarios where ServiceAccount design decisions have real security consequences. The first is a workload that genuinely needs Kubernetes API access. The second is a workload that never should.

Scenario A: a monitoring agent

A metrics-collection agent runs in your simulated cluster. Its job is to scrape metrics from Pods across all namespaces. To build its list of targets, it calls the Kubernetes API to list Pods. It needs a dedicated ServiceAccount so you can attach exactly the right permissions to it and nothing more.

Create the ServiceAccount.

Terminal window
kubectl create serviceaccount monitoring-agent

Verify it appears in the list alongside default.

Terminal window
kubectl get serviceaccounts
Monitoring workload Web workload API Server(RBAC per identity) ServiceAccountmonitoring-agent Podmonitoring-agent-pod ServiceAccountweb-app Podweb-pod
Monitoring workload Web workload API Server(RBAC per identity) ServiceAccountmonitoring-agent Podmonitoring-agent-pod ServiceAccountweb-app Podweb-pod

The next step in a real workflow would be to create a ClusterRoleBinding connecting monitoring-agent to a ClusterRole that allows listing Pods. RBAC is covered in its own module. What matters here is that the identity is isolated: only the monitoring agent Pod carries this SA, so only it can ever receive those permissions.

Write the manifest for the agent Pod.

Terminal window
nano agent-pod.yaml
agent-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: monitoring-agent-pod
namespace: default
spec:
serviceAccountName: monitoring-agent
containers:
- name: agent
image: nginx:stable
Terminal window
kubectl apply -f agent-pod.yaml
Quiz

You created a ServiceAccount called monitoring-agent. Without any RBAC bindings, what happens when the agent Pod tries to list Pods via the API?

Reveal answer

The request is denied with a 403 Forbidden response. The SA identity is valid, but the API server’s authorization layer finds no RoleBinding or ClusterRoleBinding granting that identity the right to list Pods. Identity and permission are always separate concerns.

Scenario B: a stateless web application

Your second workload serves HTTP responses to users. It reads from a database and has no reason to call the Kubernetes API. Mounting a token into this Pod is unnecessary exposure.

Create a dedicated ServiceAccount for it.

Terminal window
kubectl create serviceaccount web-app

Write the manifest with automount disabled.

Terminal window
nano web-pod.yaml
web-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: web-pod
namespace: default
spec:
serviceAccountName: web-app
automountServiceAccountToken: false
containers:
- name: web
image: nginx:stable
Terminal window
kubectl apply -f web-pod.yaml

This Pod has an identity (web-app) but carries no token in its filesystem. If the container is ever compromised, the attacker finds no API credential to escalate with.

Quiz

Why assign a dedicated web-app ServiceAccount at all, if you are just going to disable automount? Why not use default with automount disabled?

Reveal answer

Because identity isolation matters independently of token presence. If you later add a RoleBinding for the default SA, the web Pod’s identity would inherit those permissions. A dedicated SA per application means that permissions are always tied to a specific identity, and a mistake in one binding cannot silently expand the attack surface of unrelated Pods.

Confirming both ServiceAccounts exist

Describe each SA to confirm their presence and configuration.

Terminal window
kubectl describe serviceaccount monitoring-agent
Terminal window
kubectl describe serviceaccount web-app

Both should show the correct namespace, no unexpected token Secrets, and the details Kubernetes recorded at creation time.

The risk of an overpermissioned default ServiceAccount

Here is the failure pattern worth internalizing. Suppose a developer is debugging a RBAC issue and temporarily grants cluster-admin to the default ServiceAccount in a namespace.

Every Pod in that namespace that does not declare an explicit serviceAccountName is now running with cluster-admin permissions. A single compromised container in any of those Pods gives an attacker full control of the cluster. This is not a contrived scenario. It happens in environments where RBAC is treated as a debugging convenience rather than a security boundary. One dedicated ServiceAccount per application, combined with least-privilege RBAC bindings, makes this class of mistake impossible to propagate silently.

The principle is consistent across both scenarios: isolate identities by application, grant only what is needed, and disable automount for workloads that have no reason to authenticate to the API.

Lesson 05 closes the module by explaining the difference between old-style token Secrets and the modern projected token system, and why the newer approach eliminates an entire category of credential-theft risk.

Get hands-on with Kubernetes

This lesson includes a live terminal with a simulated Kubernetes cluster. Upgrade to Pro to unlock the terminal (free during early access)

Contact us