What is Service Account in Kubernetes: Explained Simply
service account in Kubernetes is a special user identity assigned to pods to allow them to interact securely with the Kubernetes API. It provides credentials and permissions so pods can perform actions like reading secrets or accessing other resources without using human user accounts.How It Works
Think of a service account as a badge that a pod wears to prove who it is when talking to the Kubernetes system. Just like you need an ID card to enter a building, a pod needs a service account to access certain parts of the Kubernetes environment.
When a pod runs, Kubernetes automatically attaches a service account token to it. This token acts like a key, letting the pod call the Kubernetes API safely. The service account defines what the pod is allowed to do, such as reading configuration data or managing other resources.
This system keeps pods from having too much power and helps protect the cluster by limiting access based on what each pod really needs.
Example
This example shows how to create a service account and use it in a pod specification.
apiVersion: v1
kind: ServiceAccount
metadata:
name: my-service-account
---
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
serviceAccountName: my-service-account
containers:
- name: my-container
image: busybox
command: ["sleep", "3600"]When to Use
Use service accounts whenever your pods need to interact with the Kubernetes API or other cluster resources securely. For example:
- Pods that read secrets or config maps to get sensitive data.
- Pods that need to create or update other Kubernetes objects.
- Automated jobs or controllers that manage cluster state.
This helps keep your cluster safe by giving each pod only the permissions it needs, following the principle of least privilege.
Key Points
- Service accounts provide identities for pods to access the Kubernetes API.
- They use tokens mounted inside pods for authentication.
- Permissions are controlled via roles and role bindings linked to service accounts.
- Using service accounts improves security by limiting pod access.