Kubernetes – Cluster Security & Role-Based Access Control (RBAC)
When you run a Kubernetes cluster in production, you cannot let every developer, CI/CD pipeline, or application Pod do whatever it wants. One accidental kubectl delete command or one hacked container could wipe out your entire database or steal your secrets.
Kubernetes solves this with Role-Based Access Control (RBAC). RBAC lets you enforce the Principle of Least Privilege—meaning every user and bot only gets the exact permissions they need to do their job, and nothing more.
- How the Principle of Least Privilege protects your Kubernetes cluster.
- The difference between
ServiceAccount,Role,ClusterRole,RoleBinding, andClusterRoleBinding. - How to write clean YAML manifests to give a Pod read-only access to other Pods.
- How to test and verify permissions using
kubectl auth can-i. - Real-world security best practices and common Kubernetes interview questions.
1. Core Building Blocks of Kubernetes RBAC
Think of RBAC like an office building security system. Every employee or visitor gets an ID badge (Subject). There is a rulebook that says which rooms can be opened (Role). Finally, the security desk links your ID badge to a specific rulebook (Binding).
In Kubernetes, RBAC is strictly deny-by-default. If you do not explicitly grant permission to do something, the Kubernetes API server will block the request with a 403 Forbidden error.
| RBAC Object | Scope | What It Does |
|---|---|---|
ServiceAccount |
Namespace | An identity for applications, Pods, and bots running inside the cluster. |
Role |
Namespace | A set of permission rules (what actions are allowed on which resources) inside one specific namespace. |
ClusterRole |
Cluster-wide | A set of permission rules that can apply across all namespaces or to cluster-level resources like Nodes. |
RoleBinding |
Namespace | Connects a user, group, or ServiceAccount to a Role (or ClusterRole) within a single namespace. |
ClusterRoleBinding |
Cluster-wide | Connects a user, group, or ServiceAccount to a ClusterRole across the entire cluster. |
User object. Human users are managed externally (like AWS IAM, Google Cloud, or TLS certificates). Only ServiceAccount objects live inside Kubernetes for apps and bots.2. Practical Implementation: Creating a Read-Only Pod Monitor
Imagine you are building a monitoring bot that runs inside the production namespace. Its only job is to list and read Pods to check their health. It should never be allowed to delete Pods or read Secrets.
Let’s build this step by step using three YAML files.
Step 1: Create the Namespace and ServiceAccount
First, create a dedicated identity for our monitoring bot instead of using the default ServiceAccount.
Example 1: serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: pod-monitor-sa
namespace: production
Step 2: Create the Role (Define the Rules)
Next, define what actions (verbs) are allowed on which resources. Notice that we only allow get, list, and watch on pods.
Example 2: pod-reader-role.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-reader-role
namespace: production
rules:
- apiGroups: [""] # "" indicates the core API group
resources: ["pods"]
verbs: ["get", "list", "watch"]
Step 3: Create the RoleBinding (Connect Identity to Rules)
Now, attach the pod-reader-role to our pod-monitor-sa ServiceAccount.
Example 3: pod-reader-binding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: pod-reader-binding
namespace: production
subjects:
- kind: ServiceAccount
name: pod-monitor-sa
namespace: production
roleRef:
kind: Role
name: pod-reader-role
apiGroup: rbac.authorization.k8s.io
Step 4: Apply and Test Permissions
Apply the manifests and use kubectl auth can-i to test permissions immediately without even deploying a Pod yet.
Example 4: Applying manifests and testing RBAC
kubectl create namespace production
kubectl apply -f serviceaccount.yaml
kubectl apply -f pod-reader-role.yaml
kubectl apply -f pod-reader-binding.yaml
# Test if the ServiceAccount can list pods
kubectl auth can-i list pods \
--as=system:serviceaccount:production:pod-monitor-sa \
-n production
# Test if the ServiceAccount can delete pods
kubectl auth can-i delete pods \
--as=system:serviceaccount:production:pod-monitor-sa \
-n production
Expected output
namespace/production created serviceaccount/pod-monitor-sa created role.rbac.authorization.k8s.io/pod-reader-role created rolebinding.rbac.authorization.k8s.io/pod-reader-binding created yes no
Step 5: Attach the ServiceAccount to a Pod
Finally, tell your Pod to use this new ServiceAccount by setting serviceAccountName in the Pod spec.
Example 5: monitor-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: monitor-bot
namespace: production
spec:
serviceAccountName: pod-monitor-sa
containers:
- name: monitor
image: bitnami/kubectl:latest
command: ["sleep", "3600"]
3. Architecture & RBAC Request Flow Visualized
Whenever a Pod or user sends a request to the Kubernetes API server, the request goes through a strict security pipeline before anything happens. Watch how the request flows from the Subject through the RBAC check below.
How Kubernetes Evaluates an RBAC Request
Every API call is authenticated first, then checked against RoleBindings and Roles. If no rule explicitly permits the action, the API server blocks it.
4. Step-by-Step Execution Breakdown
When your monitor-bot Pod runs kubectl get pods inside the cluster, here is exactly what happens behind the scenes:
/var/run/secrets/kubernetes.io/serviceaccount/token.kube-apiserver. The API server verifies the token signature and identifies the caller as system:serviceaccount:production:pod-monitor-sa.RoleBinding in the production namespace (or any ClusterRoleBinding) that mentions pod-monitor-sa.pod-reader-binding, which points to pod-reader-role. It checks if the requested verb (list) and resource (pods) exist in the rules. Because they match, the API server returns the list of Pods.- Never use wildcards (
*) in production: Writingverbs: ["*"]orresources: ["*"]gives an attacker full control if the container is compromised. - Disable token auto-mounting when not needed: If your web app does not talk to the Kubernetes API, set
automountServiceAccountToken: falsein the Pod or ServiceAccount spec. - Avoid
cluster-adminfor apps: Never bind an application or CI/CD bot to the built-incluster-adminClusterRole.
5. Hands-On Practice
Test your understanding of Kubernetes RBAC with these three practical exercises.
Create a Role named secret-reader in the staging namespace that only allows get access to secrets (not list or delete).
Hint: Set apiGroups: [""], resources: ["secrets"], and verbs: ["get"].
Create a ClusterRole that can read Deployments (apiGroups: ["apps"]), and bind it to a ServiceAccount using a RoleBinding so it only works inside the dev namespace.
Expected result: kubectl auth can-i get deployments -n dev returns yes, while checking -n production returns no.
Modify a Role so that a ServiceAccount can only read one specific ConfigMap named app-settings and cannot read any other ConfigMaps in the same namespace.
Hint: Use the resourceNames: ["app-settings"] field inside your Role’s rules block.
6. Interview Questions
RBAC is one of the most frequently tested topics in Kubernetes, DevOps, and Cloud Architect interviews. Here is what hiring managers look for.
Answer: The permissions defined in the ClusterRole are restricted strictly to the namespace where the RoleBinding lives. This is a great pattern in real-world clusters because you can define one reusable ClusterRole (like view or edit) and assign it to different teams in their own namespaces without duplicating YAML files.
Answer: No. Kubernetes RBAC is strictly additive (allow-only). There are no "deny" rules. Because everything is denied by default, you simply omit the permissions you do not want a user or ServiceAccount to have.
Answer: Every Pod in a namespace that does not specify a serviceAccountName automatically uses the default ServiceAccount. If you grant permissions to the default ServiceAccount, every single Pod in that namespace inherits those permissions, which breaks the Principle of Least Privilege.
Conclusion
Securing a Kubernetes cluster starts with controlling who can talk to the API server and what they are allowed to touch. By creating dedicated ServiceAccounts, writing tight Roles, and verifying them with kubectl auth can-i, you dramatically reduce the blast radius if an application is ever compromised.
Make least-privilege RBAC a habit from day one of every project. Your future self—and your security team—will thank you.
Official Documentation
- Kubernetes Documentation – Using RBAC Authorization
- Kubernetes Documentation – Configure Service Accounts for Pods
Upgradix by Shrash Studio – Learn Technology. Build Your Future.