Sub topic 2
How a Pod Authenticates to the API Server
This chapter assumes you know Pods, namespaces, and kubectl apply. It also assumes you know the difference between authentication (who are you?) and authorization (what may you do?). It uses the app-reader ServiceAccount in the demo namespace from the previous chapter.
What the kubelet gives the Pod
When a Pod has a token to mount, the kubelet adds a projected volume to every container. The volume is at /var/run/secrets/kubernetes.io/serviceaccount and holds three files:
| File | Contents |
|---|---|
| `token` | A signed JWT (JSON Web Token) that identifies the ServiceAccount |
| `ca.crt` | The cluster CA certificate, used to verify the API server |
| `namespace` | The Pod's namespace |
Kubernetes also sets KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT in each container. Client libraries read these files and variables automatically when they run in-cluster. That is why most SDKs need no configuration inside a Pod.
The request flow
The Pod sends the token as a bearer token in the Authorization header. The API server checks the JWT's signature, its expiry, and its audience. For bound tokens, it also checks that the Pod the token was issued for still exists.
A valid token becomes this identity:
- Username:
system:serviceaccount:<namespace>:<name>, for examplesystem:serviceaccount:demo:app-reader. - Groups:
system:serviceaccounts,system:serviceaccounts:<namespace>, andsystem:authenticated.
RBAC rules match against these names. The token proves who the caller is and grants nothing by itself.
Try it
This Pod sets automountServiceAccountToken: true at the Pod level. The Pod setting overrides the false on the ServiceAccount, so the Pod gets a token anyway.
apiVersion: v1
kind: Pod
metadata:
name: api-check
namespace: demo
spec:
serviceAccountName: app-reader
automountServiceAccountToken: true
containers:
- name: curl
image: curlimages/curl:8.10.1
command: ["sleep", "3600"]After you apply it, call the API server from inside the Pod:
kubectl exec -n demo api-check -- sh -c '
SA=/var/run/secrets/kubernetes.io/serviceaccount
curl -s --cacert $SA/ca.crt \
-H "Authorization: Bearer $(cat $SA/token)" \
https://kubernetes.default.svc/api/v1/namespaces/demo/configmaps'Without a Role, you get a 403 Status object. Its message names the identity the server saw: User "system:serviceaccount:demo:app-reader" cannot list resource "configmaps". The server authenticated the token and then denied the request. With the Role and RoleBinding from the next chapter applied, the same call returns a ConfigMap list.
Common errors
- No token in the Pod. The ServiceAccount or the Pod sets
automountServiceAccountToken: false, and the Pod does not override it. Thetokenfile is missing. - TLS failure. The client does not use
ca.crt, so it cannot verify the server. - Wrong namespace in the name. A ServiceAccount is namespaced.
demo/app-readerandother/app-readerare different identities. - Cached token. Tokens expire and the kubelet refreshes the file. Code that reads the file once at startup eventually fails with 401.
Exercise
A Pod's request to list ConfigMaps returns 403, and the error text includes User "system:serviceaccount:demo:app-reader". Was authentication or authorization the step that failed?
Show answer
Authorization. The API server accepted the token and identified the caller as demo/app-reader, which is why the name appears in the message. RBAC then denied the list action. A failed authentication would return 401 with no user identity.