ServiceAccounts, Roles and RoleBindings
Every Pod runs as a ServiceAccount whether you choose one or not, and by default it can do nothing. Granting it one permission takes three objects and three commands, and kubectl auth can-i --as= answers whether it worked without deploying anything. This does that, then calls the API from inside the Pod to show the allowed request succeeding and the forbidden one returning 403.
Configuration and Security Guide 29 of 44 Intermediate
- Kubernetes1.36.4
- Cluster4 nodes
- Runtimecontainerd 2.2.6
- CNICalico v3.32.1
- TimeAbout 16 min
The token in a Pod is a projected, time-bound token - expirationSeconds reads 3607 here, so it is rotated roughly hourly by the kubelet. Kubernetes no longer creates a permanent Secret per ServiceAccount, which is why kubectl get secrets in a namespace with ServiceAccounts is usually empty.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| CKA1001 | 192.168.0.175 | Ubuntu 26.04 LTS | Control Plane Node | 2 Core | 4 GB | 50 GB |
| CKA1001-NODE01 | 192.168.0.176 | Ubuntu 26.04 LTS | Worker Node | 2 Core | 4 GB | 50 GB |
| CKA1001-NODE02 | 192.168.0.177 | Ubuntu 26.04 LTS | Worker Node | 2 Core | 4 GB | 50 GB |
| CKA1001-NODE03 | 192.168.0.178 | Ubuntu 26.04 LTS | Worker Node | 2 Core | 4 GB | 50 GB |
This guide includes
Use this when a Pod needs to talk to the API and should be able to do nothing else. This matters because every Pod already runs as a ServiceAccount whether you chose one or not, and by default it can do almost nothing - which is the right place to start from.
- creating a ServiceAccount and confirming what it cannot do yet
- granting exactly one permission with a Role and a RoleBinding, imperatively
- proving it from inside a Pod using the projected, time-bound token
- turning the token off entirely with
automountServiceAccountToken: false
Before you start
- A cluster and kubectl, with permission to create Roles and RoleBindings.
- The session creates namespace
ckad-sa, a ServiceAccountreader, a Role limited to reading Pods, a RoleBinding, and two Pods - one using the token and one with it turned off.
-
A ServiceAccount, and what it cannot do
-
One Role, one RoleBinding
-
Proving it from inside a Pod
-
Turning the token off