π Kubernetes RBAC (Role-Based Access Control) β A Deep Dive¶
RBAC (Role-Based Access Control) is one of the most crucial topics in Kubernetes administration, especially for CKA certification. It controls who can do what in a Kubernetes cluster.
π§ Table of Contents¶
- What is RBAC?
- Key Concepts
- RBAC Resources
- Types of Subjects
- How Kubernetes Authenticates Users
- Creating Roles and RoleBindings
- Step 4: Understanding API Groups
- Step 5: Understanding Subresources
- ServiceAccount vs User vs Group
- Tips for CKA and Practice Ideas
π What is RBAC?¶
RBAC (Role-Based Access Control) lets you control access to Kubernetes resources based on a userβs role (i.e., their job/responsibility). You define who can perform which actions on which resources.
Example: - Devs can read pods. - Admins can delete deployments. - A CI pipeline can create and update secrets.
π§© Key Concepts¶
| Term | Meaning |
|---|---|
Role | Defines permissions within a namespace |
ClusterRole | Defines cluster-wide permissions or permissions across namespaces |
RoleBinding | Binds a Role to users/groups/serviceaccounts in a namespace |
ClusterRoleBinding | Binds a ClusterRole to users/groups/serviceaccounts cluster-wide |
π§± RBAC Resources¶
Kubernetes uses the following resources to implement RBAC:
Role: Defines allowed actions within a namespace.RoleBinding: Grants access to aRolefor a user/group/service account in a namespace.ClusterRole: Same as Role, but for cluster-wide use.ClusterRoleBinding: Grants access to aClusterRolefor subjects across the whole cluster.
π§ββοΈ Types of Subjects¶
A Role/ClusterRole is applied to a subject, which can be of three types:
| Subject Type | Created In | Managed By Kubernetes? | Example |
|---|---|---|---|
User | External (IAM, OIDC) | β No | john@example.com |
Group | External (IAM/OIDC) | β No | devs, admins |
ServiceAccount | Internal (Kubernetes) | β Yes | default:my-sa |
π How Kubernetes Authenticates Users¶
Kubernetes does NOT manage users and groups internally.
It relies on external authentication systems: - β TLS Certificates (self-signed or signed by a CA) - β Cloud IAM (AWS IAM, Azure AD, GCP IAM) - β OIDC (Google, Okta, etc.) - β Webhooks (Custom user systems)
π§ That's why you can use --user or --group in RoleBindings, even though you never created those inside the cluster!
π― Creating RBAC Rules β Examples¶
1. Create a Role¶
kubectl create role pod-reader \
--verb=get --verb=list --verb=watch \
--resource=pods
2. Create a RoleBinding¶
kubectl create rolebinding read-pods-binding \
--role=pod-reader \
--user=john@example.com \
--namespace=dev
3. Using API Groups¶
kubectl create role foo \
--verb=get,list,watch \
--resource=rs.apps
4. Using SubResources¶
kubectl create role foo \
--verb=get,list,watch \
--resource=pods,pods/status
π Step 4: Understanding API Groups¶
Kubernetes resources are split into API Groups:
| API Group | Resources |
|---|---|
"" (core) | pods, services, configmaps, secrets |
apps | deployments, replicasets, statefulsets |
batch | jobs, cronjobs |
rbac.authorization.k8s.io | roles, rolebindings, etc. |
To know the group for a resource:
kubectl api-resources
Example:
kubectl create role foo --verb=get --resource=deployments.apps
deployments - API Group: apps Format: --resource=resourceName.groupName
π Step 5: Understanding Subresources¶
Some Kubernetes resources have subresources. These are attached to the main resource but represent a sub-functionality:
| Main Resource | Subresource | Use Case |
|---|---|---|
pods | status | View pod status info |
pods | log | Read container logs |
pods | exec | Execute commands inside containers |
deployments | scale | Scale deployment replicas |
Format:
--resource=resource/subresource
--resource=resource.group/subresource
Example:
kubectl create role viewer --verb=get --resource=pods/status
Understanding --resource=resource.group/subresource¶
When using the --resource flag in kubectl create role, you're defining the exact API target the role will apply to. This flag can have three components:
-
resourceβ The main Kubernetes object.
Examples:pods,deployments,services -
groupβ The API group the resource belongs to.
Examples:apps,batch,rbac.authorization.k8s.io -
subresource(optional) β A more specific part or action related to the resource.
Examples: pods/logβ to access logs from a poddeployments/scaleβ to allow scaling of deploymentspods/statusβ to read or modify the status subresource of a pod
π Format:
--resource=resource.group/subresource
All three components are not always required. For core resources (likepods), thegroupmay be empty. Andsubresourceis only used when needed.
Examples:¶
# Targeting the core 'pods' resource
--resource=pods
# Targeting the 'replicasets' resource in the 'apps' API group
--resource=replicasets.apps
# Targeting the 'status' subresource of pods
--resource=pods/status
π€ ServiceAccount vs User vs Group¶
| Feature | User | Group | ServiceAccount |
|---|---|---|---|
| Created by you? | β (external) | β (external) | β
(via kubectl) |
| Stored in Kubernetes | β | β | β |
| Used for scripts | β (harder) | β | β (preferred) |
| RoleBinding compatible | β | β | β |
π Tips for CKA and Practice¶
-
Use
kubectl auth can-ito test permissions:kubectl auth can-i delete pods --as john@example.com -
Practice creating:
- Role and RoleBinding
- ClusterRole and ClusterRoleBinding
- With API group, subresources, and resource names
-
For service accounts and users
-
Use
kubectl explainto dig into any resource:kubectl explain role
π¦ Real-World Use Case: CI/CD Pipeline¶
You have a CI/CD tool running as a Pod with a ServiceAccount. You want it to only create/update deployments.
1. Create Role:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: cicd-role
namespace: dev
rules:
- apiGroups: ["apps"]
resources: ["deployments"]
verbs: ["create", "update"]
2. Create RoleBinding:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: cicd-bind
namespace: dev
subjects:
- kind: ServiceAccount
name: cicd-sa
namespace: dev
roleRef:
kind: Role
name: cicd-role
apiGroup: rbac.authorization.k8s.io
π οΈ All Verbs Explained (with Real-Life Examples)¶
| Verb | Meaning in Kubernetes | Real-life analogy (π§) |
|---|---|---|
get | Read a specific object | Viewing a file or record |
list | List all objects of a type | Listing all documents in a folder |
watch | Subscribe to live updates about objects | Getting live notifications about folder changes |
create | Create a new object | Making a new Google Doc |
update | Change an existing object | Editing an existing document |
patch | Modify part of an object | Suggesting a small fix (e.g., typo) |
delete | Remove an object | Deleting a document |
deletecollection | Delete a group of objects at once | Deleting all documents in a folder at once |
bind | Bind a Role to a user (only for RoleBindings) | Assigning someone a job role |
impersonate | Act as another user/resource | Logging in as someone else (if authorized) |
escalate | Allow assigning higher permissions | Letting someone assign admin rights |
approve | Approve CSR (certificate signing request) | Approving someone's access request |
deny | Deny CSR (rare, specific usage) | Rejecting access |
use | Use a named resource (like PSP or SCC) | Being allowed to wear a specific uniform |
controlplane ~ β k describe clusterrole cluster-admin
Name: cluster-admin
Labels: kubernetes.io/bootstrapping=rbac-defaults
Annotations: rbac.authorization.kubernetes.io/autoupdate: true
PolicyRule:
Resources Non-Resource URLs Resource Names Verbs
--------- ----------------- -------------- -----
*.* [] [] [*]
[*] [] [*]
π kubectl describe clusterrole cluster-admin Explained¶
You ran:
kubectl describe clusterrole cluster-admin
And got this core info:
| Field | Value |
|---|---|
| Resources | *.* (All API groups, all resources) |
| Non-Resource URLs | [*] (All API paths that aren't resources) |
| Resource Names | [] (Applies to all resource names) |
| Verbs | [*] (All actions: get, list, create, update, etc.) |
π§ What This Means in Layman's Terms¶
Imagine Kubernetes is a city, and every API call is like accessing a building or facility (hospital, bank, etc.).
- π§ββοΈ ClusterRole:
cluster-admin= The Mayor + Police Chief + Super Admin β has full access to all buildings, all services, and all paths in the entire city (cluster).
π Field-by-Field Breakdown¶
1. Resources: *.*¶
This means:
*= All API groups (like core, apps, networking.k8s.io, etc.)*= All resources in those API groups (pods, services, secrets, etc.)
π¦ Effect: "You can access every resource in the cluster regardless of API group."
2. Non-Resource URLs: [*]¶
These are URLs like:
/healthz/metrics/api/version/logs
π¦ Effect: "You can also call any non-resource endpoints that are outside the Kubernetes object model."
3. Resource Names: []¶
Empty means "applies to all objects".
Example:
- If this was
[my-secret], it would only apply to one specific resource. - But since itβs empty, it applies to all resource names of every kind.
4. Verbs: [*]¶
This means:
["get", "list", "watch", "create", "update", "patch", "delete", "deletecollection", "bind", "impersonate", ...]
π¦ Effect: "You can do everything possible with any resource."
π‘οΈ Summary: Why This Role Is So Powerful¶
π₯ cluster-admin has:
- All verbs
- On all resources
- Across all namespaces
- Including non-resource URLs
This is the equivalent of root on Linux.
β Where This Is Used¶
By default:
- The user who bootstraps the cluster (like
kubeadm init) getscluster-admin. - You can assign this role using a
ClusterRoleBinding.
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: my-admin-binding
subjects:
- kind: User
name: ibtisam
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io