How Kubernetes authenticate internal access?
When you access the Kubernetes API server, you authenticate as a regular user. But what happens when Pods start making requests? How does Kubernetes verify the identity of these internal actors?
The answer is ServiceAccounts. In Kubernetes, both application Pods and system components authenticate themselves using the credentials associated with a specific ServiceAccount.
This post delves into how ServiceAccounts work, highlights the differences between the legacy strategy and the current approach, and guides you through creating, inspecting, and debugging these credentials.
Why is internal access authentication crucial?
You may wonder why pods inside the cluster need to connect to the Kubernetes API. There are some common scenarios:
What is a Service Account in Kubernetes?
A ServiceAccount is a type of non-human account that, in Kubernetes, provides a distinct identity in a Kubernetes cluster.
A ServiceAccount provides an identity for processes that run in a Pod to authenticate to the cluster's API server.
Legacy Service Account Tokens
Traditionally, Kubernetes created and stored tokens as Secrets in the cluster.
Bound Service Account Tokens
To address the above concerns, Kubernetes introduced Bound service account tokens.
Bound tokens became more stable and are the recommended approach in Kubernetes 1.22 and later.
Hands-On: Creating and Inspecting Tokens
In this section, let’s walk through the commands you can use to create, inspect, and debug Service Account tokens in a Kubernetes cluster.
领英推荐
Legacy service account - the K8s automatically create a Secret and add its reference to the service account.
apiVersion: v1
kind: ServiceAccount
metadata:
name: controller-svc-legacy
namespace: controller-role
secrets:
- name: controller-svc-legacy-token-vj74z
Bound service account:
apiVersion: v1
kind: ServiceAccount
metadata:
name: controller-svc
namespace: controller-ns
2. Create and assign a role to the service account
To grant permissions to a service account via RBAC (Role-Based Access Control), we create a role and assign it to the service account.
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: controller-role-binding
namespace: controller-ns
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: controller-role
subjects:
- kind: ServiceAccount
name: controller-svc
namespace: controller-ns
3. Verify and inspect the token
We read the token from the file in the pod and then use TokenReview to verify and inspect it. We can see the creation and last access time of the token.
kubectl exec -it controller-pod -- /bin/sh
# Inspect the token file
cat /var/run/secrets/kubernetes.io/serviceaccount/token
# Check filesystem timestamps (access, modify, change time)
stat /var/run/secrets/kubernetes.io/serviceaccount/token
Access: 2025-02-08 10:10:19.302900356 +0000
Modify: 2025-01-21 09:58:07.880324983 +0000
Change: 2025-01-21 09:58:07.880324983 +0000
Create a TokenReview resource to ask the API server to validate a token. This is especially useful if you suspect issues with authentication or need to confirm which ServiceAccount identity the token represents.
# tokenreview.yaml
apiVersion: authentication.k8s.io/v1
kind: TokenReview
spec:
token: <token from cat command>
kubectl create -o yaml -f tokenreview.yaml
The API server will respond with details about the token:
apiVersion: authentication.k8s.io/v1
kind: TokenReview
metadata:
creationTimestamp: null
spec:
token: <>
status:
audiences:
- https://kubernetes.default.svc
authenticated: true
user:
extra:
authentication.kubernetes.io/credential-id:
- JTI=d6ccd5f8-8b40-420a-a71e-ac8983d3tr0a
authentication.kubernetes.io/node-name:
- controller-node
authentication.kubernetes.io/node-uid:
- d1ab549f-4a55-4e64-902c-12cf85d2b2cd
authentication.kubernetes.io/pod-name:
- controller-5c8b949fdc-l2t5l
authentication.kubernetes.io/pod-uid:
- 23540e77-4f7a-45ec-8ecc-b85b4f40bfba
groups:
- system:serviceaccounts
- system:serviceaccounts:controller-svc
- system:authenticated
uid: a9962ee2-5d6d-4086-a76b-af5c89c6d829
username: system:serviceaccount:controller-ns:controller-svc
Best Practices and Tips
ServiceAccounts are foundational to Kubernetes security, allowing pods to authenticate to the API server. Bound tokens bring short-lived, auto-rotated credentials that address many of the security weaknesses found in legacy (long-lived) tokens.
References: