Appearance

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.

Flow viewStep 1 of 4
Select a namespaced identityThe Pod chooses pod-reader in its own namespace. That identity name does not itself specify permissions.. Pod, serviceAccountName: pod-reader, active. ServiceAccount, learning / pod-reader, active. TokenRequest API, Audience + expiration + binding, idle. Projected volume, Kubelet refreshes token file, idle. API server, Authenticates bearer token, idlePod: serviceAccountName: pod-reader
PodserviceAccountName: pod-readerIn focus
ServiceAccount: learning / pod-reader
ServiceAccountlearning / pod-readerIn focus
TokenRequest API: Audience + expiration + binding
TokenRequest APIAudience + expiration + bindingContext
Projected volume: Kubelet refreshes token file
Projected volumeKubelet refreshes token fileContext
API server: Authenticates bearer token
API serverAuthenticates bearer tokenContext
PodserviceAccountName: pod-readerIn focus
ServiceAccountlearning / pod-readerIn focus
TokenRequest APIAudience + expiration + bindingContext
Projected volumeKubelet refreshes token fileContext
API serverAuthenticates bearer tokenContext
Animation controls
Speed
0.5×
1×
2×
Explore a scenario
Normal behavior
Client caches an expired token
Select a namespaced identity

The Pod chooses pod-reader in its own namespace. That identity name does not itself specify permissions.

Read the complete visual sequence
  1. Select a namespaced identity

    The Pod chooses pod-reader in its own namespace. That identity name does not itself specify permissions.

  2. Issue a bound token

    The credential is requested with audience, lifetime and object binding rather than being a permanently valid password.

  3. Project and refresh

    The kubelet projects credentials and refreshes them. A long-running client must reread rotating credentials as its library requires.

  4. 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.

  1. Initial request works

    The original token is valid for its intended audience and lifetime.

  2. File rotates, cache does not

    The kubelet refreshes the projected file, while the application continues presenting its old cached token.

  3. 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
AcceptedIdentity, permission, and admission checks all succeed for this request.. Authentication, Identity established, ready. Authorization, Verb permitted, ready. Admission, Object policy permits, readyAuthentication: Identity established
AuthenticationIdentity establishedReady
Authorization: Verb permitted
AuthorizationVerb permittedReady
Admission: Object policy permits
AdmissionObject policy permitsReady
AuthenticationIdentity establishedReady
AuthorizationVerb permittedReady
AdmissionObject policy permitsReady
Accepted

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.

Policy viewStep 1 of 4
Describe the allowed actionsThe Role grants explicit verbs on core Pods. Pod deletion, logs and exec are not implied by read access to the Pod resource.. Identity, learning:pod-reader, idle. Role read-pod-state, get/list/watch core pods, active. RoleBinding, Identity → Role in learning, idle. API authorization, Evaluate verb/resource/namespace, idle. Other binding, May add wider permission, idleIdentity: learning:pod-reader
Identitylearning:pod-readerContext
Role read-pod-state: get/list/watch core pods
Role read-pod-stateget/list/watch core podsIn focus
RoleBinding: Identity → Role in learning
RoleBindingIdentity → Role in learningContext
API authorization: Evaluate verb/resource/namespace
API authorizationEvaluate verb/resource/namespaceContext
Other binding: May add wider permission
Other bindingMay add wider permissionContext
Identitylearning:pod-readerContext
Role read-pod-stateget/list/watch core podsIn focus
RoleBindingIdentity → Role in learningContext
API authorizationEvaluate verb/resource/namespaceContext
Other bindingMay add wider permissionContext
Animation controls
Speed
0.5×
1×
2×
Explore a scenario
Normal behavior
Unexpected delete permission
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.

Read the complete visual sequence
  1. 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.

  2. Grant to a subject

    The namespaced RoleBinding connects this ServiceAccount to those Role rules in learning.

  3. Evaluate the concrete request

    list pods in learning is allowed through the binding; delete pods is not supplied by this role.

  4. 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.

  1. Narrow role inspected

    The read role contains no delete verb and appears safe when viewed alone.

  2. Other allow applies

    A second binding authorizes deletion, so the effective RBAC decision is allowed.

  3. 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

Search the book

Search loads when you open it.