This guide explains Kubernetes service accounts, their creation, usage, and security enhancements in recent versions.
Welcome to this comprehensive guide on Kubernetes service accounts. In this lesson, we focus on how service accounts function—primarily from an application developer’s perspective. While advanced security topics such as authentication, authorization, and role-based access control (RBAC) are covered extensively in the Kubernetes Administrators Course, this guide is dedicated solely to working with service accounts.There are two main types of accounts in Kubernetes:
User Accounts: Designed for human users like administrators or developers.
Service Accounts: Intended for machine-to-machine interactions or application-specific tasks. For instance, monitoring tools like Prometheus use a service account to query the Kubernetes API for performance metrics, while Jenkins uses one for deploying applications.
Consider an example: “my Kubernetes dashboard,” a basic dashboard application built with Python. This application retrieves a list of Pods from a Kubernetes cluster by sending API requests and subsequently displays the results on a web page. To authenticate its API requests, the application uses a dedicated service account.
To create a service account named dashboard-sa, run:
Copy
Ask AI
kubectl create serviceaccount dashboard-sa
To view all service accounts, use:
Copy
Ask AI
kubectl get serviceaccount
The output will appear similar to:
Copy
Ask AI
NAME SECRETS AGEdefault 1 218ddashboard-sa 1 4d
Upon creation, Kubernetes automatically generates a service account token stored as a Secret and links it to the account. To inspect the details of your service account and its token, execute:
When deploying third-party applications (such as a custom dashboard or Prometheus) on a Kubernetes cluster, you can have Kubernetes automatically mount the service account token as a volume into the Pod. This token is typically available at the path: /var/run/secrets/kubernetes.io/serviceaccount.
Every namespace includes a default service account that is automatically injected into Pods. For example, consider the following simple Pod manifest using a custom dashboard image:
By default, Pods use the default service account. To assign a different service account—like the previously created dashboard-sa—update your Pod definition to include the serviceAccountName field:
Remember that you cannot modify the service account of an existing Pod. To use a new service account, delete and recreate the Pod. Deployments will automatically roll out new Pods when changes are made to the Pod template.
After deploying the updated manifest, running:
Copy
Ask AI
kubectl describe pod my-kubernetes-dashboard
will show that the new service account is now in effect, with volume mounting information reflecting the token for dashboard-sa (e.g., dashboard-sa-token-kbbdm).If you wish to disable the automatic mounting of the service account token, set automountServiceAccountToken to false in the Pod specification:
Prior to Kubernetes v1.22, service account tokens were automatically mounted from Secrets without an expiration date. Starting with v1.22, the TokenRequest API (KEP-1205) was introduced to generate tokens that are audience-bound, time-bound, and object-bound—enhancing security significantly.Below is an example Pod definition using a projected volume sourced from the TokenRequest API:
Starting with Kubernetes v1.24, Kubernetes no longer automatically creates non-expiring service account tokens stored as Secrets. Instead, after creating a new service account, you must generate a token explicitly with:
Copy
Ask AI
kubectl create token dashboard-sa
This command produces a token with an expiry (by default, one hour from creation). You can verify and decode this token using tools like jq or jwt.io:
If necessary (though not recommended), you can still create a non-expiring token by manually creating a Secret. Ensure the service account exists first:
It is highly recommended to use the TokenRequest API to generate tokens, as API-generated tokens provide additional security features such as expiry, audience restrictions, and improved manageability.
Service Accounts vs. User Accounts: Service accounts are meant for applications (or machines), whereas user accounts are for human users.
Token Generation: Creating a service account automatically generates a token stored in a Secret, which is used for API authentication.
Automatic Token Mounting: Pods can automatically mount the service account token at /var/run/secrets/kubernetes.io/serviceaccount, though this behavior can be modified.
Enhanced Security: Since Kubernetes v1.22, tokens are generated using the TokenRequest API, making them audience-bound, time-bound, and more secure.
Kubernetes v1.24 Changes: With v1.24, Kubernetes no longer provisions non-expiring tokens automatically via Secrets; use the kubectl create token command to generate tokens as needed.
For additional insights, refer to the Kubernetes enhancement proposals and the official documentation on service accounts and tokens.