In Kubernetes, service accounts are the key to granting pods and processes controlled permissions within the cluster. With kubectl, you can create, inspect, and manage these accounts with precision. They define the identity a workload uses to interact with the API server and other resources.
A service account is bound to a namespace. By default, new pods use the namespace’s default service account unless you specify another. This default account often has minimal privileges, which is good for security. For elevated or specialized access, create custom service accounts and pair them with the right roles.
Creating a Service Account
To create one, run:
kubectl create serviceaccount my-service-account --namespace my-namespace
This writes a resource into the namespace. Kubernetes also creates a secret containing a token tied to the service account.
Assigning Roles to a Service Account
With RBAC (Role-Based Access Control), assign permissions:
kubectl create rolebinding my-binding \
--role=my-role \
--serviceaccount=my-namespace:my-service-account \
--namespace my-namespace
Use roles for namespace-level permissions, cluster roles for cluster-wide. Always bind the least privilege needed.