Authentication, RBAC, and ServiceAccounts
Trace an API identity through short-lived workload credentials and additive authorization rules.
What you will understand
- Separate authentication from authorization and admission
- Grant narrow namespace permissions through RBAC bindings
- Explain projected ServiceAccount token audience, lifetime and rotation
Builds on: Termination, disruption, and drain
Mental model: establish identity, then evaluate the requested action
Authentication answers who is making an API request. A cluster can accept client certificates, bearer tokens or integrations with identity providers, depending on configuration. Human access commonly uses kubeconfig credentials or an exec-based login integration. Workloads should use a purpose-specific ServiceAccount, a namespaced identity represented as system:serviceaccount:NAMESPACE:NAME. The ServiceAccount object is not itself a human login and does not automatically grant application administration privileges.
Authorization evaluates whether that authenticated identity may perform a particular action on a particular API resource. Admission then validates or mutates permitted object operations according to cluster rules. A request can authenticate successfully, pass RBAC and still fail admission because the proposed Pod is forbidden by security policy. Conversely, no amount of correct Pod YAML repairs an identity lacking permission to create Pods. Keep these failure boundaries distinct when reading an API error.
By default, eligible Pods receive projected ServiceAccount credentials. Modern bound tokens are time-limited, audience-bound and associated with the relevant workload identity and object binding. The kubelet refreshes projected credentials; applications reading a token file must tolerate rotation instead of caching the first contents forever. Audience determines which relying service should accept the token. A token for the Kubernetes API is not automatically suitable for an external cloud API; workload identity integrations require explicit trust and audience configuration.
A workload identity receives rotating, scoped credentials
Trace ServiceAccount selection, projected bound tokens and API authentication.
The Pod chooses pod-reader in its own namespace. That identity name does not itself specify permissions.
- Pod to ServiceAccountworkload identity
Read the complete visual sequence
- Select a namespaced identity
The Pod chooses pod-reader in its own namespace. That identity name does not itself specify permissions.
- Issue a bound token
The credential is requested with audience, lifetime and object binding rather than being a permanently valid password.
- Project and refresh
The kubelet projects credentials and refreshes them. A long-running client must reread rotating credentials as its library requires.
- Authenticate the request
The API verifies the credential and obtains the ServiceAccount identity, then authorization separately evaluates the requested action.
Client caches an expired token
The projected file rotates but the application keeps the old credential in memory.
- Initial request works
The original token is valid for its intended audience and lifetime.
- File rotates, cache does not
The kubelet refreshes the projected file, while the application continues presenting its old cached token.
- Authentication fails
An expired credential can be rejected before RBAC is evaluated. Use a client that reloads the rotating token instead of granting a broader role.
About this model
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.Walkthrough: bind one narrow role to one workload identity
A Role describes namespaced permissions, and a RoleBinding grants those permissions to subjects in the binding’s namespace. A ClusterRole can describe reusable permissions, including cluster-scoped resources. Referencing a ClusterRole from a RoleBinding grants its applicable namespaced permissions in that binding namespace; a ClusterRoleBinding grants at cluster scope. Names and locations matter as much as the verb list.
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-state
Apply all three resources in learning or change the subject namespace and command namespace together. A workload selects this identity using spec.serviceAccountName: pod-reader. The role grants Pod observation, not Pod deletion, Secrets access or container exec. Subresources such as pods/log and pods/exec are distinct resource permissions and should be granted only when needed.
Roles describe actions; bindings grant them
Follow namespaced RBAC evaluation and show why a second allow remains effective.
The Role grants explicit verbs on core Pods. Pod deletion, logs and exec are not implied by read access to the Pod resource.
- Role read-pod-state to API authorizationrules
Read the complete visual sequence
- Describe the allowed actions
The Role grants explicit verbs on core Pods. Pod deletion, logs and exec are not implied by read access to the Pod resource.
- Grant to a subject
The namespaced RoleBinding connects this ServiceAccount to those Role rules in learning.
- Evaluate the concrete request
list pods in learning is allowed through the binding; delete pods is not supplied by this role.
- Union all applicable grants
Another binding can grant delete or cluster-wide access. The narrow role does not subtract those permissions.
Unexpected delete permission
A broader binding already grants the action.
- Narrow role inspected
The read role contains no delete verb and appears safe when viewed alone.
- Other allow applies
A second binding authorizes deletion, so the effective RBAC decision is allowed.
- Remove the excessive grant
Audit bindings and groups, narrow the unintended grant and re-run can-i. An extra read-only role cannot function as a deny.
About this model
RBAC is the relevant authorizer in this example; subjects may receive several grants.
Practice: verify permissions without printing credentials
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-reader
Expect yes for list Pods and no for deletion or Secrets, assuming no other bindings grant those actions. An impersonation-forbidden error means your current identity cannot run that check; it is not the same as the ServiceAccount being denied the requested resource action. Use an administrator-provided access review or an actual restricted workload test in that situation. Avoid placing bearer tokens into logs, command transcripts or source files to make the experiment easier.
RBAC permissions are additive. A second RoleBinding can add deletion permission, and a narrow role cannot subtract it. There is no RBAC deny rule that overrides an existing allow. When observed access is broader than expected, examine group membership, all matching bindings and cluster-wide grants. Wildcards and binding-management permissions can have effects beyond the obvious resource list, so prefer explicit verbs and resources.
Pitfalls: unnecessary credentials enlarge the boundary
A workload that never calls the Kubernetes API generally does not need an automatically mounted API token. Set automountServiceAccountToken: false where appropriate, then verify the Pod spec and filesystem. That setting reduces automatic credential exposure; it does not constrain every other credential a workload can acquire. Access to create Pods can also allow indirect access to namespace resources through chosen ServiceAccounts or mounted Secrets unless admission and RBAC are designed together.
Check your understanding
A read-only Role is bound to a ServiceAccount that also has an administrator ClusterRoleBinding. Does the read-only role restrict it?
No. The effective permissions include the administrator grant because RBAC unions allowed actions from applicable bindings. Remove or narrow the broad binding if it is unintended, then verify the effective permissions again. Adding a restrictive-looking role cannot negate an existing allow.
Use the security lab to verify the identity and namespace boundary. Next, constrain what an authorized Pod creator may ask the node to execute.
Primary documentation
Go to the source
Reviewed 2026-10-27 · Kubernetes 1.37 · Related local exercise