Skip to Content
Asterrr's Handbook
Cheat sheets

RBAC and access

Which RBAC object to use, how to write and test rules, and how users and ServiceAccounts get credentials.

Which object

NeedRules inBindingReach
One namespaceRoleRoleBindingThat namespace
Same rules in several namespacesClusterRoleRoleBinding per namespaceEach bound namespace
All namespacesClusterRoleClusterRoleBindingCluster
Cluster-scoped kinds (nodes, PVs, namespaces)ClusterRoleClusterRoleBindingCluster
Non-resource URLs such as /healthzClusterRole with nonResourceURLsClusterRoleBindingCluster

RBAC is additive. There is no deny rule, and the binding's roleRef cannot be changed after creation.

Imperative forms

k create role r -n ns --verb=get,list,watch --resource=pods,pods/log
k create clusterrole cr --verb=get,list --resource=nodes
k create rolebinding rb -n ns --role=r --user=jana --group=dev --serviceaccount=ns:sa1
k create clusterrolebinding crb --clusterrole=cr --user=jana
k create rolebinding rb2 -n ns --clusterrole=view --serviceaccount=ns:sa1
k auth can-i list pods -n ns --as=jana
k auth can-i get pods --subresource=log -n ns --as=system:serviceaccount:ns:sa1
k auth can-i --list -n ns --as=jana

Verbs and subresources

Verbsget list watch create update patch delete deletecollection
Subresourcespods/log, pods/exec, deployments/scale, nodes/status
API groups"" core, apps, batch, networking.k8s.io, rbac.authorization.k8s.io
NamesresourceNames: ["x"] limits to named objects. It does not work with list and create

Default ClusterRoles

cluster-admin (everything), admin (namespace admin including RBAC), edit (read and write most objects, no RBAC), view (read most, no Secrets). Aggregation labels rbac.authorization.k8s.io/aggregate-to-view, -edit and -admin extend them.

Identities

IdentityCreated byAppears as
UserCertificate CN signed by the cluster CAjana
GroupCertificate O fields, or an OIDC claimdevelopers
ServiceAccountk create sasystem:serviceaccount:<ns>:<name>

New user flow: openssl genrsa, openssl req -new -subj "/CN=u/O=g", a CertificateSigningRequest with signerName: kubernetes.io/kube-apiserver-client and usages: [client auth], k certificate approve, read .status.certificate, k config set-credentials and set-context, then bind a role.

ServiceAccount tokens: k create token sa -n ns --duration=1h. Pods get a projected, expiring token. Set automountServiceAccountToken: false when the Pod never calls the API.

On this page