Sub topic 3
Who gets access: users, groups, and service accounts
This chapter assumes you know basic Kubernetes objects (pods, namespaces, secrets) and can apply YAML with kubectl. It covers the "who" half of RBAC: the subjects that permissions are granted to.
Subjects are named by the API server, not created by RBAC
A subject is the identity a rule applies to. RBAC defines three kinds: User, Group, and ServiceAccount. Authentication runs before authorization. The authenticator turns a credential into a username and a list of groups. RBAC then compares those strings with the subjects in your bindings.
Users and groups
Kubernetes has no User or Group object. You cannot run kubectl create user. These names come from the credential:
- In a client certificate, the common name (CN) becomes the username. Each organization (O) field becomes a group.
- With OIDC tokens, claims you configure on the API server supply the username and groups.
- With an authenticating proxy or webhook, the external system supplies both.
This means a binding can name jane before any such user exists. The binding only takes effect when a request arrives that authenticates as jane. A typo in the name fails silently, because RBAC does not check that the user exists.
Groups let you grant access to many people at once. In the earlier binding example, the group manager received secret-reader. Anyone whose credential carries that group gets the permissions. Kubernetes also adds built-in groups, such as system:authenticated for every authenticated request and system:serviceaccounts for all service accounts.
Service accounts
A service account is the one subject type that Kubernetes does manage. It is a namespaced object that gives a workload an identity. Pods use it to call the API server.
Every namespace has a default service account. A pod that names no service account runs as that one. Create a dedicated service account for each workload so that you can grant it only what it needs.
apiVersion: v1
kind: ServiceAccount
metadata:
name: app-sa
namespace: defaultA pod selects it with serviceAccountName:
apiVersion: v1
kind: Pod
metadata:
name: app
namespace: default
spec:
serviceAccountName: app-sa
containers:
- name: app
image: nginx:1.27Kubernetes mounts a short-lived token for that service account into the pod. The API server authenticates it as this username:
system:serviceaccount:<namespace>:<name>It also places the account in two groups: system:serviceaccounts and system:serviceaccounts:<namespace>. Use the full username form with --as when you test permissions.
Writing subjects in a binding
The subjects list takes one entry per subject, and a binding may mix kinds. Fields differ by kind:
| Kind | `name` | `namespace` | `apiGroup` |
|---|---|---|---|
| `User` | username string | not used | `rbac.authorization.k8s.io` |
| `Group` | group string | not used | `rbac.authorization.k8s.io` |
| `ServiceAccount` | account name | required in a ClusterRoleBinding, and set it in a RoleBinding too | omit (core group) |
subjects:
- kind: User
name: jane
apiGroup: rbac.authorization.k8s.io
- kind: Group
name: manager
apiGroup: rbac.authorization.k8s.io
- kind: ServiceAccount
name: app-sa
namespace: defaultCommon errors
- Missing
namespaceon a service account subject. The binding is rejected or never matches. Always set it. - Wrong subject kind. Listing a group as a
Usermatches nobody, because the strings are compared per kind. - Granting to
default. Every pod without an explicit account would inherit the permissions. Bind roles to dedicated accounts instead. - Binding a service account from another namespace. This works, since the subject carries its own namespace. Check that the namespace is the one you intend.
Exercise
A pod in namespace shop runs as the service account billing. What username does RBAC see, and which command checks whether it may list secrets?
Show answer
The username is system:serviceaccount:shop:billing. Run kubectl auth can-i list secrets --as system:serviceaccount:shop:billing --namespace shop.