liveBook · Manning

Service account

As explained in Acing the Certified Kubernetes Administrator Exam, Second Edition

A Service Account is a Kubernetes API resource representing an identity usable by workloads and other cluster clients.

A service account is a namespace-scoped Kubernetes resource that gives a workload an identity for authenticating to the Kubernetes API. Unlike a human user, it is managed as a Kubernetes object. Pods can use service-account tokens to authenticate; roles and role bindings determine what that identity may do. Give workloads only the permissions they need. section 3.1 section 3.3

Check the identity and its credentials separately:

  • Create a namespace, then run `kubectl get sa -n web`: a `default` service account is created automatically. A pod uses that account unless another is specified with `serviceAccountName`. section 3.3
  • Inspect a pod with `kubectl get po nginx -o yaml`. Its service-account access appears as a read-only mount at `/var/run/secrets/kubernetes.io/serviceaccount`. Recent Kubernetes versions obtain credentials through the TokenRequest API and mount them using a projected volume, rather than automatically creating long-lived token Secrets. section 3.3
  • If a pod does not need API access, set `automountServiceAccountToken: false` on its service account or in its pod specification. Then inspect the pod YAML to verify that no service-account volume mount appears. section 3.3
root@kind-control-plane:/# kubectl get po nginx -o yaml |
 grep volumeMounts -A14
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: kube-api-access-nhxf2
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: kind-worker2
  preemptionPolicy: PreemptLowerPriority
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30

The pod YAML shows both `serviceAccountName: default` and the read-only API-access volume mount. Check both fields when verifying which identity a pod uses and whether its token is mounted. section 3.3

Grant and test a narrow permission

Create a role with `kubectl create role pod-list --verb=list --resource=pods`, then bind it with `kubectl create rolebinding pod-list-bind --role=pod-list --serviceaccount=default:nomount-sa`. Verify authorization using `kubectl auth can-i list pods --as system:serviceaccount:default:nomount-sa`. A `yes` confirms that RBAC permits this identity to list pods; it does not mean a pod with token automount disabled has a token available to make the request. section 3.3

How Acing the Certified Kubernetes Administrator Exam, Second Edition covers it

How deep it goes
Works through default-account discovery, token mounts, automount disablement, role binding, and permission checks without detailing token generation internals.
Examples
Uses namespace and pod inspections, a disabled-automount manifest, a role binding, and `kubectl auth can-i`.
What it assumes
Assumes familiarity with pods, namespaces, and the Kubernetes API while demonstrating service-account commands and manifests.
What you can do after
Operate/configure it with `kubectl` and YAML to assign pod identities, control token mounting, grant permissions, and verify access.
How it connects
Connects pod authentication to the TokenRequest API, projected volumes, RBAC roles, role bindings, and least privilege.

Also explained in

Compare how 10 books explain service account →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Acing the Certified Kubernetes Administrator Exam, Second Edition. The section links point to the parts of the book it draws on.