Appearance

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.

  1. Pod specSelect the namespaced ServiceAccount
    KubeletThe intended workload identity is known.
  2. KubeletRequest and project a short-lived token
    Application containerThe application can read the token through its mounted file.
  3. ApplicationSend a request with the token
    API serverThe server verifies the token and establishes the ServiceAccount identity.
Follow the sequence

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

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

  1. Role permits read verbs

    The Role grants explicit verbs on core Pods. Pod deletion, logs and exec are not implied by read access to the Pod resource.

  2. Binding names the ServiceAccount

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

  3. Check the requested verb and resource

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

  4. Combine applicable grants

    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.

1 / 5

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.

Request attributes
Before
An authenticated caller
After
list · pods · kv-lab
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.

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.

If another RoleBinding grants this ServiceAccount delete on Pods in kv-lab, will the reader Role still keep it from deleting them?
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 kind
Inspect YAML or commands

Evaluate the concrete request

kubectl auth can-i list pods -n learning --as=system:serviceaccount:learning:pod-reader
Explore 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-reader

In 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

Search the book

Search loads when you open it.

Keyboard shortcuts

Ctrl + K / ⌘ + K
Search the book
?
Open this guide
Tab / Shift + Tab
Move between controls
Enter / Space
Activate a focused button or disclosure
← / →
Focused visual timeline · previous or next step
Home / End
Focused visual timeline · first or last point
Esc
Close the open dialog or menu