
Kubernetes RBAC - Controlling Access Inside Your EKS Cluster
Who can do what - Role-Based Access Control from scratch
A Kubernetes cluster with no access control is a security incident waiting to happen. Every developer has full access to everything. A CI/CD pipeline can delete production workloads. A misconfigured pod can read secrets it was never supposed to see.
Kubernetes RBAC (Role-Based Access Control) solves this by giving every identity exactly the permissions it needs — and nothing more. This is the principle of least privilege applied to your cluster.
In this demo, I set up RBAC from scratch on Amazon EKS — service accounts, roles, bindings, and cluster-wide access control — then verified every permission with
kubectl auth can-i .
RBAC Core Concepts
ServiceAccount → Identity (who is making the request?)Role → Permissions scoped to one namespaceClusterRole → Permissions scoped to the entire clusterRoleBinding → Links a ServiceAccount to a RoleClusterRoleBinding → Links a ServiceAccount to a ClusterRoleThe key rule: if a permission is not explicitly granted, it is denied. Kubernetes RBAC is deny-by-default.
Architecture — Three Personas
developer-sa → dev-team namespace only ✅ pods, deployments, services (full) ✅ configmaps (read only) ❌ secrets (no access) ❌ prod-team namespace (no access)
readonly-sa → dev-team namespace only ✅ everything: list/get/watch ❌ create, update, delete (no write)
monitoring-sa → entire cluster (ClusterRole) ✅ nodes, namespaces, pods across all namespaces ✅ metrics ❌ delete anything (read only cluster-wide)Step 1 — Namespaces and ServiceAccounts
kubectl create namespace dev-teamkubectl create namespace prod-team
kubectl create serviceaccount developer-sa --namespace dev-teamkubectl create serviceaccount readonly-sa --namespace dev-teamkubectl create serviceaccount monitoring-sa --namespace dev-teamStep 2 — Developer Role
The developer role grants full pod and deployment access within
dev-team — but explicitly excludes secrets:apiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata: name: developer-role namespace: dev-teamrules: - apiGroups: [""] resources: ["pods", "pods/log", "pods/exec"] verbs: ["get", "list", "watch", "create", "delete"] - apiGroups: ["apps"] resources: ["deployments", "replicasets"] verbs: ["get", "list", "watch", "create", "update", "patch"] - apiGroups: [""] resources: ["services"] verbs: ["get", "list", "watch", "create", "update"] - apiGroups: [""] resources: ["configmaps"] verbs: ["get", "list", "watch"] # Secrets are NOT listed — denied by defaultStep 3 — Read-Only Role
apiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata: name: readonly-role namespace: dev-teamrules: - apiGroups: ["", "apps", "autoscaling"] resources: - pods - deployments - replicasets - services - configmaps - horizontalpodautoscalers verbs: ["get", "list", "watch"]Step 4 — RoleBindings
apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata: name: developer-binding namespace: dev-teamsubjects: - kind: ServiceAccount name: developer-sa namespace: dev-teamroleRef: kind: Role apiGroup: rbac.authorization.k8s.io # ← must be apiGroup, not apiRef name: developer-roleReal error encountered: The field is
apiGroup — not apiRef . Using apiRef throws a strict decoding error at apply time. Always double-check the RoleBinding roleRef field name.
Step 5 — Testing Permissions with kubectl auth can-i
This command is one of the most useful debugging tools in Kubernetes — it lets you check what any service account can or cannot do without actually performing the action:

# Developer — can list pods in dev-team?kubectl auth can-i list pods \ --namespace dev-team \ --as system:serviceaccount:dev-team:developer-sa# yes ✅
# Developer — can access secrets?kubectl auth can-i get secrets \ --namespace dev-team \ --as system:serviceaccount:dev-team:developer-sa# no ✅ (correctly denied)
# Developer — can access prod-team?kubectl auth can-i list pods \ --namespace prod-team \ --as system:serviceaccount:dev-team:developer-sa# no ✅ (namespace isolation works)
# ReadOnly — can delete pods?kubectl auth can-i delete pods \ --namespace dev-team \ --as system:serviceaccount:dev-team:readonly-sa# no ✅
Step 6 — ClusterRole for Monitoring
A ClusterRole provides read access across the entire cluster — useful for monitoring tools that need to see all namespaces:
apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRolemetadata: name: cluster-viewerrules: - apiGroups: [""] resources: ["nodes", "namespaces", "pods", "services"] verbs: ["get", "list", "watch"] - apiGroups: ["apps"] resources: ["deployments", "daemonsets", "replicasets"] verbs: ["get", "list", "watch"] - apiGroups: ["metrics.k8s.io"] resources: ["nodes", "pods"] verbs: ["get", "list"]# Monitoring — can list nodes cluster-wide?kubectl auth can-i list nodes \ --as system:serviceaccount:dev-team:monitoring-sa# yes ✅
# Monitoring — can list pods in prod-team?kubectl auth can-i list pods \ --namespace prod-team \ --as system:serviceaccount:dev-team:monitoring-sa# yes ✅ (cluster-wide access)
# Monitoring — can delete nodes?kubectl auth can-i delete nodes \ --as system:serviceaccount:dev-team:monitoring-sa# no ✅ (read-only)
Role vs ClusterRole
Permission Test Results Summary
Every result is exactly what was intended — this is what proper RBAC looks like.
Key Lessons
RBAC is deny-by-default. You do not need to explicitly deny permissions — anything not listed in a Role is automatically denied. This makes RBAC easier to reason about than allowlist/blocklist systems.
kubectl auth can-i is your best friend. Before deploying anything to production, verify every service account's permissions using this command. It is fast, safe, and gives you exact yes/no answers without performing any actual operations.The field is
apiGroup — not apiRef . This is a simple but easy-to-make typo in RoleBinding and ClusterRoleBinding manifests. Kubernetes will reject the resource with a strict decoding error. Always check roleRef.apiGroup .Never grant
cluster-admin to application service accounts. The built-in cluster-admin ClusterRole has unrestricted access to everything. It should only be used by human administrators during initial setup — never by pods, CI/CD pipelines, or operators.Namespace isolation is one of the most valuable RBAC patterns. By scoping a Role to a single namespace and verifying that the service account cannot access other namespaces, you create hard boundaries between teams and environments within the same cluster.
Full Source Code
All RBAC manifests from this demo are available on GitHub:
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article