Skip to content

Kubernetes RBAC: Roles, ClusterRoles, and Bindings

Lock down the cluster with RBAC — Role vs ClusterRole, ServiceAccounts, group claims from OIDC, and patterns that scale beyond two engineers.

RBAC is the only effective access control in Kubernetes — every other mechanism rests on it. The model is small: four object kinds (Role, ClusterRole, RoleBinding, ClusterRoleBinding) and three subject kinds (User, Group, ServiceAccount). The hard part is keeping it tractable across many teams and many namespaces without ending up with one role per person. This article walks the model, the verbs that matter, the bindings that surface in production, and the patterns that scale.

How to verify

What permissions does a given subject have?

kubectl auth can-i --list --as alice@example.com
kubectl auth can-i create pods -n team-payments --as alice@example.com
kubectl auth can-i '*' '*' --all-namespaces --as system:serviceaccount:argocd:argocd-application-controller

can-i --list enumerates every rule that applies. can-i <verb> <resource> is a yes/no check. --as impersonates — useful for testing without logging in as the user. The kube-apiserver must grant your current identity impersonate on the target; cluster-admin has it.

To see who’s bound to what:

kubectl get rolebinding,clusterrolebinding -A
kubectl describe clusterrolebinding cluster-admin

What’s happening

A Role is a list of rules — combinations of API groups, resources, resource names, and verbs (get, list, watch, create, update, patch, delete, deletecollection). Roles are namespaced; rules in a Role only apply within the Role’s namespace. A ClusterRole is the same thing at cluster scope; it can also be referenced from a RoleBinding to grant cluster-scoped permissions inside a single namespace.

A RoleBinding ties a Role (or ClusterRole) to a set of subjects within a namespace. A ClusterRoleBinding ties a ClusterRole to subjects cluster-wide. Subjects are Users (string-typed; comes from the authenticator), Groups (also string-typed; same source), and ServiceAccounts (namespaced; the in-cluster identity for Pods).

The authenticator is what turns a credential into a User and a list of Groups. With OIDC, the apiserver flags --oidc-issuer-url, --oidc-username-claim, and --oidc-groups-claim map JWT fields to RBAC subjects. With static certificates, the certificate’s CN is the User and the O fields are Groups. With ServiceAccount tokens, the User is system:serviceaccount:<ns>:<name> and the Groups are system:serviceaccounts plus system:serviceaccounts:<ns>.

The aggregation feature stitches together ClusterRoles via labels — useful when extensions (cert-manager, operators) need to add permissions to the built-in admin/edit/view roles without forking them.

The procedure

  1. A read-only Role in one namespace for a developer who needs to inspect production:

    apiVersion: rbac.authorization.k8s.io/v1
    kind: Role
    metadata:
      namespace: team-payments
      name: prod-reader
    rules:
      - apiGroups: [""]
        resources: ["pods", "services", "configmaps"]
        verbs: ["get", "list", "watch"]
      - apiGroups: [""]
        resources: ["pods/log"]
        verbs: ["get"]
      - apiGroups: ["apps"]
        resources: ["deployments", "replicasets"]
        verbs: ["get", "list", "watch"]
  2. Bind the Role to a group claimed by your OIDC provider:

    apiVersion: rbac.authorization.k8s.io/v1
    kind: RoleBinding
    metadata:
      namespace: team-payments
      name: payments-readers
    subjects:
      - kind: Group
        name: payments-developers       # from OIDC `groups` claim
        apiGroup: rbac.authorization.k8s.io
    roleRef:
      kind: Role
      name: prod-reader
      apiGroup: rbac.authorization.k8s.io
  3. A ServiceAccount with the permissions a Pod actually needs. Default ServiceAccounts get automountServiceAccountToken: true and no permissions — fine for read-the-API workloads, useless for controllers. Be explicit.

    apiVersion: v1
    kind: ServiceAccount
    metadata: { name: backup-runner, namespace: ops }
    automountServiceAccountToken: true
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: Role
    metadata: { name: backup-runner, namespace: ops }
    rules:
      - apiGroups: [""]
        resources: ["pods/exec"]
        verbs: ["create"]
      - apiGroups: [""]
        resources: ["pods"]
        verbs: ["get", "list"]
    ---
    apiVersion: rbac.authorization.k8s.io/v1
    kind: RoleBinding
    metadata: { name: backup-runner, namespace: ops }
    subjects:
      - kind: ServiceAccount
        name: backup-runner
        namespace: ops
    roleRef:
      kind: Role
      name: backup-runner
      apiGroup: rbac.authorization.k8s.io
  4. Use the built-in user-facing ClusterRoles instead of writing your own from scratch where possible. The four to know: view (read most things, no secrets), edit (read + write most things, can read secrets), admin (edit + RBAC within a namespace), and cluster-admin (everything everywhere).

    kubectl create rolebinding payments-admins \
      --namespace=team-payments \
      --clusterrole=admin \
      --group=payments-leads
  5. Aggregate a custom role into view so all readers get the new resource without you editing every RoleBinding:

    apiVersion: rbac.authorization.k8s.io/v1
    kind: ClusterRole
    metadata:
      name: ourapp-crd-view
      labels:
        rbac.authorization.k8s.io/aggregate-to-view: "true"
    rules:
      - apiGroups: ["ourapp.example.com"]
        resources: ["widgets"]
        verbs: ["get", "list", "watch"]
  6. Audit the dangerous edges periodically — anyone with cluster-admin, anyone with * on Secrets, anyone with pods/exec in production:

    kubectl get clusterrolebinding -o json | \
      jq -r '.items[] | select(.roleRef.name=="cluster-admin") | .metadata.name'

Common pitfalls

  • The built-in edit ClusterRole grants read access to Secrets — a “developer can edit their namespace” role is not “developer cannot read credentials”; write a tighter custom role when that matters.
  • pods/exec, pods/portforward, and pods/attach give shell access to running containers; with these, RBAC limits to the namespace don’t constrain what runs inside the Pod.
  • A RoleBinding can reference a ClusterRole — useful for reusing a definition but giving permissions only within one namespace; don’t mistake this for granting cluster-wide access.
  • ServiceAccount tokens projected into Pods are JWTs with cluster-wide validity until the Pod restarts (modern projected tokens have shorter TTL and are bound to the Pod); use audience-scoped projected tokens for callouts to external systems.
  • system:masters (granted by client certs with O=system:masters) bypasses RBAC entirely — the only group that does — so guard the cluster CA carefully and never issue admin certs casually.

We operate RBAC for clients with OIDC-backed identity, group claims that map to role bindings, ServiceAccount-per-workload least privilege, and a quarterly audit that catches drift. See /en/services/managed-operations/.