Sub topic 9
Giving workloads access with service accounts
This chapter assumes you can write and apply Roles and RoleBindings, and that you know what a pod spec looks like. People use users and groups to log in with kubectl. Workloads running inside the cluster need an identity too, and that identity is a service account.
What a service account is
A service account is a namespaced Kubernetes object that gives processes in a pod an identity. Kubernetes does not manage users. Service accounts are the only identity type it stores as an API object.
Every namespace has a default service account. A pod that does not name one runs as default. The full username that RBAC sees has this form:
system:serviceaccount:<namespace>:<name>Service accounts also belong to automatic groups, such as system:serviceaccounts (all of them) and system:serviceaccounts:<namespace> (those in one namespace).
Create a service account and grant it access
Creating the account and binding a Role to it takes two objects. The Role pod-reader from earlier chapters is reused here.
apiVersion: v1
kind: ServiceAccount
metadata:
name: app-sa
namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: app-sa-read-pods
namespace: default
subjects:
- kind: ServiceAccount
name: app-sa
namespace: default
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.ioTwo details differ from binding a user. A ServiceAccount subject has no apiGroup field, because it belongs to the core group. It does need a namespace, because the same name can exist in many namespaces.
Run a pod as the service account
Set serviceAccountName in the pod spec. The pod can change this only at creation, not afterward.
apiVersion: v1
kind: Pod
metadata:
name: pod-lister
namespace: default
spec:
serviceAccountName: app-sa
containers:
- name: kubectl
image: bitnami/kubectl:latest
command: ["kubectl", "get", "pods"]Kubernetes mounts a short-lived token into the container at /var/run/secrets/kubernetes.io/serviceaccount/token. Client libraries and kubectl find it there and use it to authenticate. The API server authenticates the token, learns the identity system:serviceaccount:default:app-sa, and then RBAC decides what that identity may do.
Test the identity before deploying
Use impersonation to check what the account can do:
kubectl auth can-i list pods --namespace default \
--as system:serviceaccount:default:app-sa
kubectl auth can-i list secrets --namespace default \
--as system:serviceaccount:default:app-saThe first should print yes. The second should print no, because the binding grants only pod access.
Common errors
- Granting rights to
default. Every pod without aserviceAccountNameuses it. Extra permissions ondefaultspread to all of those pods. Create one account per application. - Omitting
namespacein the subject. A ServiceAccount subject without a namespace is rejected for a RoleBinding or ClusterRoleBinding in most setups, or points at the wrong account. - Binding in the wrong namespace. A RoleBinding grants access only in its own namespace. An account in
team-abound inteam-bcan act only inteam-b. - Leaving the token mounted when it is not needed. Pods that never call the API should not carry a credential.
apiVersion: v1
kind: ServiceAccount
metadata:
name: no-api-sa
namespace: default
automountServiceAccountToken: falseExercise
A pod in namespace team-a runs as service account reporter. You want it to list pods in team-b only. Which objects do you create, and in which namespace does the RoleBinding go?
Show answer
Create a Role that allows list on pods in team-b, and a RoleBinding in team-b. The subject is kind ServiceAccount, name reporter, namespace team-a. The binding sits in the namespace where the access is granted (team-b), and the subject's namespace names where the account lives (team-a).
Exercise
What command checks whether reporter in team-a can list pods in team-b?
Show answer
kubectl auth can-i list pods --namespace team-b --as system:serviceaccount:team-a:reporter