A simple lab to explore Service Accounts and their tokens in Kubernetes.
Prerequisites
A Kubernetes cluster (kind, minikube, or any cluster)
kubectl configured
jq installed (for JSON parsing)
Lab Overview
Understanding default Service Accounts
Creating custom Service Accounts
Exploring Service Account tokens (JWT)
Using tokens to authenticate
Token mounting behavior
Part 1: Default Service Account
Every namespace automatically gets a default service account.
# Create a lab namespace# The shared kind cluster (on the CKS training server: kind export kubeconfig, then kubectx kind-kind)kubectx kind-kind
NS=sa-$(whoami)kubectl create namespace $NS
# View the default service accountkubectl get serviceaccount -n $NS
kubectl describe serviceaccount default -n $NS
# No Secret is associated: since Kubernetes 1.24, the tokens are not stored in Secrets any morekubectl get serviceaccount default -n $NS -o yaml
Part 2: Create a Pod with Default Service Account
# Create a simple podcat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: default-sa-pod
namespace: $NS
spec:
containers:
- name: nginx
image: nginx:alpine
command: ["sleep", "3600"]
EOF# Wait for the pod to be readykubectl wait --for=condition=ready pod/default-sa-pod -n $NS --timeout=60s
# Check which service account the pod useskubectl get pod default-sa-pod -n $NS -o jsonpath='{.spec.serviceAccountName}'echo
# View the mounted service account fileskubectl exec default-sa-pod -n $NS -- ls -la /var/run/secrets/kubernetes.io/serviceaccount/
What you’ll find:
token - JWT token for authentication
ca.crt - Certificate authority for the cluster
namespace - The namespace the pod is running in
Part 3: Explore the Token
# Read the tokenkubectl exec default-sa-pod -n $NS -- cat /var/run/secrets/kubernetes.io/serviceaccount/token
# Save the token to a variableTOKEN=$(kubectl exec default-sa-pod -n $NS -- cat /var/run/secrets/kubernetes.io/serviceaccount/token)# View the tokenecho $TOKEN
# Decode the JWT payload (it's base64 encoded)# JWT format: header.payload.signature, each part in base64url without paddingecho $TOKEN | jq -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson'# Read the namespace filekubectl exec default-sa-pod -n $NS -- cat /var/run/secrets/kubernetes.io/serviceaccount/namespace
Part 4: Create Custom Service Accounts
# Create two custom service accountscat <<EOF | kubectl apply -f -
apiVersion: v1
kind: ServiceAccount
metadata:
name: app-sa
namespace: $NS
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: monitor-sa
namespace: $NS
EOF# List all service accountskubectl get serviceaccount -n $NS
# Describe a custom service accountkubectl describe serviceaccount app-sa -n $NS
Part 5: Use Custom Service Account in a Pod
# Create a pod with the custom service accountcat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: app-pod
namespace: $NS
spec:
serviceAccountName: app-sa
containers:
- name: app
image: nginx:alpine
command: ["sleep", "3600"]
EOF# Wait and verifykubectl wait --for=condition=ready pod/app-pod -n $NS --timeout=60s
kubectl get pod app-pod -n $NS -o jsonpath='{.spec.serviceAccountName}'echo
Compare the identity carried by the token of each pod: the sub claim of the payload. The first characters of the tokens do not tell them apart: they encode the header of the JWT (algorithm and key id), the same for all the tokens of the cluster.
# The sub claim of a pod's tokensub(){ kubectl exec $1 -n $NS -- cat /var/run/secrets/kubernetes.io/serviceaccount/token \
| jq -rR 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson | .sub'}sub default-sa-pod
# system:serviceaccount:sa-<user>:defaultsub app-pod
# system:serviceaccount:sa-<user>:app-sa
Notice: each pod gets a token of its own, which carries its service account.
Part 6: Use Token from Outside the Pod
Call the API server with the token of app-pod, from your terminal.
Warning
kubectl --token alone is not enough: kubectl still sends the client certificate of your kubeconfig, and the API server authenticates you as the administrator. --kubeconfig=/dev/null leaves out all the credentials of your kubeconfig.
# Get the token, and the CA certificate which signed the certificate of the API serverAPP_TOKEN=$(kubectl exec app-pod -n $NS -- cat /var/run/secrets/kubernetes.io/serviceaccount/token)kubectl exec app-pod -n $NS -- cat /var/run/secrets/kubernetes.io/serviceaccount/ca.crt > /tmp/ca-$NS.crt
# Get the API server URLAPISERVER=$(kubectl config view --minify -o jsonpath='{.clusters[0].cluster.server}')# Only the token: which identity does the API server see?kubectl --kubeconfig=/dev/null --server="$APISERVER" --certificate-authority=/tmp/ca-$NS.crt \
--token="$APP_TOKEN" auth whoami
# Username system:serviceaccount:sa-<user>:app-sa# Try to list pods using the tokenkubectl --kubeconfig=/dev/null --server="$APISERVER" --certificate-authority=/tmp/ca-$NS.crt \
--token="$APP_TOKEN" get pods -n $NS || echo "EXPECTED ERROR"# Error from server (Forbidden): pods is forbidden: User "system:serviceaccount:sa-<user>:app-sa" cannot list resource "pods" ...rm /tmp/ca-$NS.crt
The token authenticates the request, but the service account has no permission by default: RBAC denies it.
Part 7: Inspect Token Details
Inspect JWT token
Answer
# Get full token detailsAPP_TOKEN=$(kubectl exec app-pod -n $NS -- cat /var/run/secrets/kubernetes.io/serviceaccount/token)# Decode headerecho "=== JWT Header ==="echo $APP_TOKEN | jq -R 'split(".")[0] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson'# Decode payloadecho "=== JWT Payload ==="echo $APP_TOKEN | jq -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson'# Key fields in the payload:# - iss: issuer, the API server (https://kubernetes.default.svc.cluster.local on kind)# - aud: audience, the API server# - sub: system:serviceaccount:<namespace>:<service account># - kubernetes.io: namespace, service account, pod and node: the token is bound to the pod# - iat, exp: issue and expiration times
Part 8: Multiple Containers, Same Token
All containers in a pod share the same service account token.