All guides

Kubernetes RBAC: five rules for least privilege

Most clusters start with a few people holding cluster-admin and never take it back. These five rules keep access narrow without slowing anyone down.

An open laptop on a wooden table in front of bookshelves

1. Bind roles to groups, not to people

Let your identity provider say who is in payments-developers. The cluster only needs to know what that group may do. People join and leave teams without anyone editing a RoleBinding.

2. Stay inside the namespace

A Role with a RoleBinding grants access in one namespace. A ClusterRoleBinding grants it everywhere, including namespaces created next year. Use the cluster-wide kind only for what is cluster-wide, such as nodes.

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: payments
  name: deployer
rules:
  - apiGroups: ["apps"]
    resources: ["deployments"]
    verbs: ["get", "list", "watch", "patch", "update"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  namespace: payments
  name: deployer
subjects:
  - kind: Group
    name: payments-developers
    apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: deployer
  apiGroup: rbac.authorization.k8s.io

3. Name every verb and resource

A * in a rule also covers resources that do not exist yet. Three grants deserve a second look: secrets (whoever can list them can read them all), pods/exec, and the verbs bind, escalate and impersonate, which let an account raise its own access.

4. One service account for each workload

Pods that share the default service account share its permissions. Give each workload its own, and switch the token off for pods that never call the Kubernetes API:

apiVersion: v1
kind: ServiceAccount
metadata:
  name: api
  namespace: payments
automountServiceAccountToken: false

5. Test what an account can do

kubectl auth can-i --list --namespace payments \
  --as system:serviceaccount:payments:api

Run it for every service account before a release, and for a person's group when someone asks for more access. Then find out who still holds the widest role:

kubectl get clusterrolebindings -o wide | grep cluster-admin

That list should be short enough to read aloud.

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides