Skip to main content
In this lesson we’ll cover Kubernetes ServiceAccounts — the identities used by workloads and system components to authenticate with the Kubernetes API. You’ll learn what ServiceAccounts are, how tokens are provided to Pods, how to create and attach ServiceAccounts, how to disable automatic token mounting, and how to obtain tokens for external use. There are two account types in Kubernetes:
  • User accounts — for humans (cluster operators, developers).
  • Service accounts — for software (controllers, monitoring, CI/CD).
ServiceAccounts let applications authenticate to the API server. For example, Prometheus uses a ServiceAccount to scrape API metrics, and Jenkins may use one to create or modify resources.
A dark-background diagram showing two columns: "User" on the left with admin and developer role icons, and "Service" on the right with service icons including Prometheus and Jenkins. It illustrates user versus service accounts/roles-diagram.

Example scenario: my-kubernetes-dashboard

Suppose you build a small Python app called my-kubernetes-dashboard that queries the Kubernetes API to list Pods and display them in a UI. If the app runs in-cluster it should use a ServiceAccount to authenticate.
  • A ServiceAccount is an identity object in a namespace.
  • A ServiceAccount has an associated token which the app presents as a Bearer token when calling the API.
  • Think of the ServiceAccount as an ID card and the token as the barcode scanned to verify that ID.
A schematic diagram showing Kubernetes authentication: a token linked to a service account that accesses the kube‑api and a Kubernetes cluster, alongside a "My Kubernetes Dashboard" panel. The image illustrates how the dashboard communicates with the kube‑api to list running pods.

Default ServiceAccount and token mounting

Kubernetes automatically creates a default ServiceAccount in every namespace. When you create a Pod and do not specify a ServiceAccount, the Pod gets the default ServiceAccount. Kubernetes mounts the ServiceAccount token into the Pod at: /var/run/secrets/kubernetes.io/serviceaccount This is provided as a projected volume; the application can read the token file and use it to authenticate. Example: list ServiceAccounts in the current namespace:
Describe a Pod to see the Service Account and the mounted path:
List files mounted into the ServiceAccount directory inside the Pod:
Table — files mounted into the ServiceAccount directory

Creating a custom ServiceAccount

The default ServiceAccount has minimal privileges. For more controlled access, create a custom ServiceAccount and bind RBAC roles to it. Imperative creation:
Declarative YAML:
Verify the new ServiceAccount:
Describe the ServiceAccount (note: newly created SA may show no mounted tokens until a Pod references it):
Bind RBAC roles (Role/ClusterRole + RoleBinding/ClusterRoleBinding) to grant the ServiceAccount the minimal permissions it needs. See Kubernetes RBAC documentation.

Attach a ServiceAccount to a Pod

To use a specific ServiceAccount, set serviceAccountName in the Pod spec: Pod manifest snippet:
After creating the Pod, confirm the association:
Inside the Pod the token is mounted as before:

automountServiceAccountToken

By default, Kubernetes automatically creates and mounts tokens for ServiceAccounts and Pods. You can disable automatic token creation/mounting at either the ServiceAccount or Pod level.
  • Disable at the ServiceAccount:
  • Or disable on an individual Pod:
When automountServiceAccountToken is false for the Pod (or for the associated ServiceAccount and not overridden), the token file will not be mounted into the Pod.

Using ServiceAccount tokens outside the cluster

Sometimes you need a ServiceAccount token for external tools (dashboards, CI/CD, monitoring). Use kubectl create token to generate a token for a ServiceAccount and print it to the console. By default tokens are short-lived (commonly 1 hour). Use --duration to request a longer token. Create a token:
Create with a longer duration (example: 2 hours):
You can decode the token (JWT header and payload) for inspection:
Use the token as a Bearer token in API calls. Example:
Table — common kubectl ServiceAccount commands

Summary

  • ServiceAccounts are Kubernetes identities used by workloads to talk to the API server.
  • Tokens are associated with ServiceAccounts and presented as Bearer tokens.
  • Create ServiceAccounts imperatively (kubectl create serviceaccount) or declaratively with YAML.
  • Attach a ServiceAccount to a Pod using the serviceAccountName field.
  • Tokens are mounted into Pods at /var/run/secrets/kubernetes.io/serviceaccount by default; disable auto-mounting with automountServiceAccountToken: false.
  • For external tools (dashboards, CI/CD) use kubectl create token to generate a short-lived token; extend duration with --duration as needed.
  • Always follow least privilege: grant only the RBAC permissions necessary for the ServiceAccount.
By default, tokens created or auto-mounted are short-lived (often 1 hour). Use kubectl create token --duration=<value> to request a longer-lived token when needed. Always follow least-privilege practices and bind only the necessary RBAC roles to ServiceAccounts.
That’s it for this lesson — practice these concepts on a cluster or in a playground to reinforce your understanding.

Watch Video

Practice Lab