AI Hints
Kubernetes · Lesson 44 of 86

Service Accounts

Identities for pods.

⏱ 2 min read▶ Video at 1:48:37🎓 Free
Watch this lesson in the Kubernetes courseWatch on YouTube ↗Full Kubernetes course

People log in to a cluster. But programs inside the cluster sometimes need to call the A P I too. A deployment tool, a monitoring agent, or an operator. Pods get their identity from a service account. In this lesson, you will learn what it is, and how to keep it minimal.

Service Accounts
From the video: Service Accounts
  • Pods need identities too
  • Every pod has a service account
  • Give each app only what it needs

How a pod proves who it is

Every namespace has a service account called default. Unless you choose another, every pod runs as it. Kubernetes gives the pod a short lived token, and places it inside the container, as a file. The app reads the token, and sends it with its A P I calls. The A P I server checks the token, and treats the pod as that service account. Then R B A C decides what the account may do. Since the default account has almost no rights, a typical app is harmless, unless you give it more.

  1. Pod startsas a service account
  2. Token mounteda file in the container
  3. App calls the APIsends the token
  4. RBAC decideswhat it may do
Default account: almost no rights
How a pod proves who it is
From the video: How a pod proves who it is

Looking inside a pod

Let's see it. We create a service account called reader, and ask what it can do. Right now, nothing, the answer is no. Then, inside a normal pod, we list the folder, where the credentials are mounted. There is a certificate to trust the A P I server, the pod's namespace, and the token itself. That is everything a program needs, to talk to the cluster.

kubectl
kubectl create namespace dev
namespace/dev created
kubectl create serviceaccount reader -n dev
serviceaccount/reader created
kubectl auth can-i list pods --as system:serviceaccount:dev:reader -n dev
no
kubectl run probe --image=busybox:1.36 -- sleep 600
pod/probe created
kubectl exec probe -- ls /var/run/secrets/kubernetes.io/serviceaccount
ca.crt
namespace
token

Service account good habits

A few habits keep things safe. Create one service account per application, so you can give each exactly the rights it needs. Never bind powerful roles, like cluster admin, to a service account, unless you absolutely must. If your app never calls the A P I, turn off the token with, automount service account token, false. And prefer short lived tokens, which Kubernetes now issues by default.

HabitWhy
One account per apprights fit each app
Avoid cluster-admina leaked token is not a disaster
Disable the token if unusednothing to steal
Use short-lived tokensthey expire by themselves

Summary

Excellent. Pods act as service accounts, carrying a token that R B A C checks. Make one per app, grant the least, and turn off tokens that you do not need. Next, we harden the pods themselves, with security contexts.

Key takeaways
  • Pods run as a ServiceAccount
  • Token mounted as a file
  • One account per app, least rights
Next up: Pod Security — hardening containers