Authentication, RBAC, and ServiceAccounts
Give a workload its own Kubernetes identity and verify which API actions it can perform.
What you will understand
- Follow a workload credential through authentication to a ServiceAccount identity.
- Build a namespaced Role and bind it to the intended subject.
- Check allowed and denied operations without displaying the token.
Builds on: Termination, disruption, and drain
How the API identifies and authorizes a caller
A reporting app needs to list Pods in the learning namespace. It should be able to read their status, and that is the extent of its job. When it sends a request, the API server first authenticates it. Authentication establishes who is calling using a credential the server trusts. Authorization evaluates what that identity is allowed to do for this particular request.
A ServiceAccount is a namespaced Kubernetes identity that a Pod can use. The Pod chooses it with spec.serviceAccountName. In a modern cluster, a Pod that mounts the standard API credentials receives a projected, time-limited token. The kubelet refreshes it before expiration. Projected tokens have an intended audience and are typically bound to the Pod. In the first scene, trace the credential to the resulting name system:serviceaccount:learning:pod-reader.
Which identity does this Pod use at the API?
A projected token lets an application authenticate as its own ServiceAccount for a supported audience.
- Pod specSelect the namespaced ServiceAccountKubeletThe intended workload identity is known.
- KubeletRequest and project a short-lived tokenApplication containerThe application can read the token through its mounted file.
- ApplicationSend a request with the tokenAPI serverThe server verifies the token and establishes the ServiceAccount identity.
A projected token lets an application authenticate as its own ServiceAccount for a supported audience.
Default modern projected token integration; external cloud identity federation is separate.
Try the mechanism with your own inputs
- Authentication to Authorization
- Authorization to Admission
Identity, permission, and admission checks all succeed for this request.
# Result: Accepted
# Authentication establishes identity.
# Authorization grants a specific verb on a resource.
# Admission applies to applicable create/modify/delete/connect operations, not ordinary reads.
# Invalid credentials are modeled; anonymous access when enabled is a separate identity path.Explore another case
Grant read access in one namespace
Role-based access control, or RBAC, describes permitted API actions. The example Role allows get, list, and watch on pods in its namespace. The empty API group means the core API. The RoleBinding grants those rules to the pod-reader ServiceAccount in learning. A ClusterRole can describe permissions for cluster-scoped resources or be reused via bindings. A RoleBinding limits the grant to its own namespace. A ClusterRoleBinding can grant applicable permissions cluster-wide.
apiVersion: v1
kind: ServiceAccount
metadata:
name: pod-reader
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: read-pod-state
rules:
- apiGroups: [""] # Empty group is the core API group.
resources: [pods]
verbs: [get, list, watch]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: pod-reader-state
subjects:
- kind: ServiceAccount
name: pod-reader
namespace: learning
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: read-pod-stateRBAC permissions are additive. A second binding can grant an action that this Role omits. In the scene, try listing Pods, deleting one, and accessing a resource governed by a different rule. Check the verb, resource, namespace, and identity for every request.
Which requests does this RoleBinding allow?
A Role defines operations. A binding grants them to a subject in scope. Other applicable grants can add permissions.
Role permits read verbs
- Role read-pod-stateAPI authorizationrules
The Role grants explicit verbs on core Pods. Pod deletion, logs and exec are not implied by read access to the Pod resource.
Binding names the ServiceAccount
- IdentityRoleBindingsubject
- RoleBindingRole read-pod-stateroleRef
The namespaced RoleBinding connects this ServiceAccount to those Role rules in learning.
Check the requested verb and resource
- IdentityAPI authorizationlist pods / learning
list pods in learning is allowed through the binding; delete pods is not supplied by this role.
Combine applicable grants
- Other bindingAPI authorizationadditional allow
Another binding can grant delete or cluster-wide access. The narrow role does not subtract those permissions.
RBAC is the relevant authorizer in this example; subjects may receive several grants.
Inspect the permission check
A reporting app can list Pods in kv-lab. Follow that decision, then change the API verb to delete and see why the same identity gets a different answer.
From Pod readerTo API server
Identify the Pod reader
The API server has established that the caller is system:serviceaccount:kv-lab:m11-reader. It passes that identity and the request attributes to authorization.
- Before
- An authenticated caller
- After
- list · pods · kv-lab
The API server has established that the caller is system:serviceaccount:kv-lab:m11-reader. It passes that identity and the request attributes to authorization.
Uses the permissions lab’s ServiceAccount and RoleBinding. Only this binding grants Pod permissions to the identity in this example. The lab uses impersonation to check permissions without reading a token.
Reveal prediction answer
RBAC permissions add together. The second binding grants delete in kv-lab. Leaving delete out of the reader Role does not cancel another grant.
Run kubectl auth can-i for both list pods and delete pods with the same impersonated identity and namespace.
Check the reader’s permissions in kindInspect YAML or commands
Evaluate the concrete request
kubectl auth can-i list pods -n learning --as=system:serviceaccount:learning:pod-readerExplore another case
Open Inspect the permission check to compare two requests made by the same ServiceAccount. The lab’s reader can list Pods because its Role includes list. A request to delete a Pod changes the verb and receives 403 Forbidden.
Test allowed and denied operations
Create learning if needed in your disposable cluster. Save the manifest as pod-reader-rbac.yaml and run the checks with a lab user allowed to impersonate the ServiceAccount. Expect listing Pods to succeed, deletion to be denied, and reading Secrets to be denied with only the grant shown. If an action returns an unexpected answer, inspect other bindings and check that the command targets the right identity and namespace.
kubectl apply -n learning -f pod-reader-rbac.yaml
# These impersonation checks need permission to impersonate the subject.
kubectl auth can-i list pods -n learning \
--as=system:serviceaccount:learning:pod-reader
kubectl auth can-i delete pods -n learning \
--as=system:serviceaccount:learning:pod-reader
kubectl auth can-i get secrets -n learning \
--as=system:serviceaccount:learning:pod-readerIn the permissions lab, record the allowed and denied results. Add only a documented permission if the application needs a new API operation, then repeat the original checks.
Mount credentials only where needed
If a Pod only serves HTTP and never calls the Kubernetes API, set automountServiceAccountToken: false where appropriate. The app still runs under its workload configuration and no default API token is mounted. Keep credentials out of diagnostic output. Authorization checks can demonstrate the grant without reading the token.
Check your understanding
The reporting job moves to a namespace called reports and uses a ServiceAccount also named pod-reader. Does the existing binding apply?
The namespace is part of the identity. system:serviceaccount:reports:pod-reader differs from the existing subject in learning. If the job still needs to list Pods in learning, bind the new subject to the intended Role in that namespace.
Primary documentation
Go to the source
Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise