Skip to main content

RBAC (Role-Based Access Control)

RBAC regulates access to Kubernetes resources based on the roles of individual users or service accounts.

  • Enabled by default in most Kubernetes distributions
  • Uses four resource types: Role, ClusterRole, RoleBinding, ClusterRoleBinding

Role vs ClusterRoleโ€‹

  • Role: grants permissions within a specific namespace
  • ClusterRole: grants permissions cluster-wide or across all namespaces

Role Exampleโ€‹

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: my-namespace
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "watch", "list"]

ClusterRole Exampleโ€‹

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: node-reader
rules:
- apiGroups: [""]
resources: ["nodes"]
verbs: ["get", "watch", "list"]

RoleBinding vs ClusterRoleBindingโ€‹

  • RoleBinding: binds a Role or ClusterRole to subjects within a specific namespace
  • ClusterRoleBinding: binds a ClusterRole to subjects across the entire cluster

RoleBinding Exampleโ€‹

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
namespace: my-namespace
subjects:
- kind: User
name: jane
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io

ClusterRoleBinding Exampleโ€‹

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: read-nodes
subjects:
- kind: ServiceAccount
name: monitoring-sa
namespace: monitoring
roleRef:
kind: ClusterRole
name: node-reader
apiGroup: rbac.authorization.k8s.io

Subjectsโ€‹

Bindings can reference three types of subjects:

  • User: a human user (authenticated externally)
  • Group: a set of users
  • ServiceAccount: an identity for processes running in Pods

Common Verbsโ€‹

VerbDescription
getRead a single resource
listList resources
watchWatch for changes
createCreate a resource
updateModify an existing resource
patchPartially modify a resource
deleteDelete a resource

Default ClusterRolesโ€‹

Kubernetes provides built-in ClusterRoles:

  • cluster-admin: full access to all resources
  • admin: full access within a namespace (no resource quotas or namespace itself)
  • edit: read/write access to most resources in a namespace (no roles or bindings)
  • view: read-only access to most resources in a namespace (no secrets)