Intro
Kubernetes Role-Based Access Control (RBAC) governs what users and service accounts can do inside a cluster. Role Bindings are the mechanism that grants a Role's permissions to specific subjects within a namespace. For developers, DevOps consultants, and technical startup teams, setting up a local lab to experiment with Role Bindings is essential for understanding and testing access control scenarios safely.
This guide walks through creating a repeatable local Kubernetes environment for practicing Role Bindings. You will learn how to install a lightweight cluster, configure a namespace and subjects, apply Role and RoleBinding manifests, verify permissions, and handle common failure modes. By the end, you will have a portable lab setup that you can use for learning, troubleshooting, and developing RBAC policies before applying them to real clusters.
A local lab reduces risk and accelerates learning. Instead of experimenting on production or shared development clusters, you can safely test permissions, observe denials, and iterate quickly. This approach aligns with the principle that a clear Kubernetes Role Binding setup reduces rework and confusion by giving you a controlled environment to explore.
Version and Environment Inventory
Before starting, establish the versions and components you will use. A consistent environment ensures that commands and outputs match your expectations and simplifies troubleshooting.
Required Tools
You need a Kubernetes cluster running locally. For this guide, we use kind (Kubernetes IN Docker) because it is lightweight and easy to reset. Alternatively, you can use minikube, k3s, or any cluster you already have. The steps are similar, but commands may vary slightly for cluster creation.
Below is a table of the tools and versions used in this guide. The versions are examples; you can substitute newer or older versions as long as they are compatible.
| Tool | Version | Purpose |
|---|---|---|
| kind | v0.20.0 | Create local Kubernetes cluster |
| kubectl | v1.27.3 | Interact with the cluster |
| Docker | 24.0.2 | Container runtime for kind |
| Kubernetes (via kind node image) | v1.27.3 | Cluster version |
Cluster Topology
For a Role Binding lab, you only need a single-node cluster. Role Bindings are namespace-scoped, so a single node is sufficient. If you want to test multi-node behavior, kind can create a multi-node cluster, but it is not necessary for basic RBAC experiments.
Create a simple kind cluster with the following command:
kind create cluster --name rbac-lab
Expected output (similar):
Creating cluster "rbac-lab" ...
â Ensuring node image (kindest/node: v1.27.3) đŧ
â Preparing nodes đĻ
â Writing configuration đ
â Starting control-plane đšī¸
â Installing CNI đ
â Installing StorageClass đž
Set kubectl context to "kind-rbac-lab"
You can now use your cluster with:
kubectl cluster-info --context kind-rbac-lab
Verify the cluster is running:
kubectl get nodes
Expected output:
NAME STATUS ROLES AGE VERSION
rbac-lab-control-plane Ready control-plane 1m v1.27.3
Prerequisites
- Docker or a compatible container runtime is installed and running.
- kubectl is installed and configured to access the cluster.
- Basic familiarity with Kubernetes concepts like Pods, Namespaces, and ServiceAccounts.
Now that the environment is ready, you can proceed to set up a safe configuration path.
Safe Configuration Path
When experimenting with RBAC, it is important to scope your changes to isolated namespaces and use dedicated service accounts. This prevents accidental modifications to cluster-wide resources and makes cleanup easier.
Create a Dedicated Namespace
Create a namespace for your lab. This will contain all test resources and keep them separate from default namespaces.
kubectl create namespace rbac-test
Expected output:
namespace/rbac-test created
Define a Service Account
Role Bindings can bind to users, groups, or service accounts. For local testing, service accounts are easy to create and impersonate or use in Pods.
Create a service account named lab-user:
kubectl create serviceaccount lab-user -n rbac-test
Expected output:
serviceaccount/lab-user created
Create a Role with Limited Permissions
A Role defines permissions within a namespace. Start with a minimal role that only allows listing pods. This will be used to demonstrate both allowed and denied operations.
For reference, the equivalent YAML manifest for the role is shown below, but we will create the role using the documented imperative command.
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: rbac-test
name: pod-reader
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]
Create the role using kubectl create role:
kubectl create role pod-reader --verb=get --verb=list --verb=watch --resource=pods -n rbac-test
Expected output:
role.rbac.authorization.k8s.io/pod-reader created
Create a RoleBinding
Now bind the pod-reader role to the lab-user service account. This grants the service account the permissions defined in the role.
For reference, the equivalent YAML manifest for the RoleBinding is shown below, but we will create the binding using the documented imperative command.
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-pods
namespace: rbac-test
subjects:
- kind: ServiceAccount
name: lab-user
namespace: rbac-test
roleRef:
kind: Role
name: pod-reader
apiGroup: rbac.authorization.k8s.io
Create the RoleBinding using kubectl create rolebinding:
kubectl create rolebinding read-pods --role=pod-reader --serviceaccount=rbac-test:lab-user -n rbac-test
Expected output:
rolebinding.rbac.authorization.k8s.io/read-pods created
Verify the Binding Configuration
Check that the RoleBinding exists and is correctly associated:
kubectl get rolebinding read-pods -n rbac-test -o yaml
Look for the roleRef and subjects sections in the output. It should match your manifest.
Now that the basic configuration is in place, you can test permissions.
Verification and Diagnostics
Verification is crucial to ensure that the Role Binding works as intended. You can test permissions using kubectl auth can-i with impersonation or by running commands inside a Pod using the service account.
Using kubectl auth can-i
The kubectl auth can-i command checks if a specific action is allowed for a user or service account. Use the --as flag to impersonate the service account.
First, test if the service account can list pods in the rbac-test namespace:
kubectl auth can-i list pods --as=system:serviceaccount:rbac-test:lab-user -n rbac-test
Expected output:
yes
Next, test if the service account can create pods, which should be denied:
kubectl auth can-i create pods --as=system:serviceaccount:rbac-test:lab-user -n rbac-test
Expected output:
no
Test access in a different namespace (e.g., default):
kubectl auth can-i list pods --as=system:serviceaccount:rbac-test:lab-user -n default
Expected output:
no
This confirms that the Role Binding only grants permissions within its namespace.
Testing from Within a Pod
A more realistic test is to run a Pod that uses the service account and attempt to access the Kubernetes API. This simulates an application checking its permissions.
Create a Pod that uses the lab-user service account and includes kubectl or curl to query the API. For simplicity, we will use a Pod with curl and the service account's token mounted.
First, get the token for the service account. In Kubernetes 1.24+, service account tokens are not automatically mounted as secrets; you need to create a token manually or use the projected token. For testing, you can use kubectl create token:
kubectl create token lab-user -n rbac-test
This outputs a token (a long string). Save it for later use.
Create a Pod manifest test-pod.yaml:
apiVersion: v1
kind: Pod
metadata:
name: test-pod
namespace: rbac-test
spec:
serviceAccountName: lab-user
containers:
- name: test-container
image: curlimages/curl:latest
command: ["sleep", "3600"]
Apply and wait for it to be running:
kubectl apply -f test-pod.yaml
kubectl wait --for=condition=Ready pod/test-pod -n rbac-test --timeout=60s
Now exec into the pod and use curl to query the Kubernetes API. First, get the API server address and token from inside the pod:
kubectl exec -it test-pod -n rbac-test -- sh
Inside the pod, run:
# Point to the internal API server
APISERVER=https://kubernetes.default.svc
# Read the service account token from the default mount location
TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
# List pods in the rbac-test namespace
curl -s -k -H "Authorization: Bearer $TOKEN" $APISERVER/api/v1/namespaces/rbac-test/pods
If the request succeeds, you will see a JSON response with the list of pods. If it fails, you will see a 403 Forbidden error. This confirms the API-level enforcement.
Using kubectl describe for Diagnostics
If permissions are not working as expected, use kubectl describe to inspect the Role and RoleBinding.
kubectl describe role pod-reader -n rbac-test
Expected output includes the rules:
Name: pod-reader
Namespace: rbac-test
Labels: <none>
Annotations: <none>
PolicyRule:
Resources Non-Resource URLs Resource Names Verbs
--------- ----------------- -------------- -----
pods [] [] [get list watch]
kubectl describe rolebinding read-pods -n rbac-test
Expected output shows the role and subjects:
Name: read-pods
Namespace: rbac-test
Labels: <none>
Annotations: <none>
Role:
Kind: Role
Name: pod-reader
Subjects:
Kind Name Namespace
---- ---- ---------
ServiceAccount lab-user rbac-test
These commands help you verify that the correct permissions are bound.
Common Pitfalls and How to Avoid Them
RBAC misconfigurations can lead to access denials or overly permissive access. In a local lab, you can safely explore common pitfalls and learn how to avoid them without affecting production.
Pitfall 1: RoleBinding References a Non-Existent Role
If the roleRef points to a Role that does not exist, the binding is created but grants no permissions. This is a common mistake when the Role manifest has not been applied yet or the name is misspelled.
Why it happens: The RoleBinding object is created successfully because the Kubernetes API does not validate the existence of the referenced Role at creation time. The binding is effectively inert.
How to avoid: Always apply the Role before the RoleBinding and double-check the roleRef.name matches an existing Role. Use kubectl get roles -n <namespace> to verify.
How to recover: Delete the erroneous RoleBinding and create a correct one after ensuring the Role exists.
Pitfall 2: Subject Mismatch
If the subject's name or namespace is incorrect, the binding will not apply. For example, a service account in a different namespace than the binding's subject namespace will not receive the permissions.
Why it happens: Users often assume that service accounts are cluster-scoped, but they are namespace-scoped. A RoleBinding in namespace A can only grant permissions to subjects in namespace A (unless using a ClusterRoleBinding, which is different).
How to avoid: Always specify the service account's namespace in the subject section if it differs from the RoleBinding's namespace, but remember that the permission scope is still limited to the RoleBinding's namespace. Verify with kubectl auth can-i.
How to recover: Correct the subject's namespace or name in the RoleBinding manifest and reapply.
Pitfall 3: Using ClusterRoleBinding Instead of RoleBinding
RoleBindings can also reference ClusterRoles, but the permissions are limited to the namespace. If you accidentally use a ClusterRoleBinding, the permissions become cluster-wide, which is a security risk.
Why it happens: Confusion between Role/ClusterRole and RoleBinding/ClusterRoleBinding. ClusterRoleBinding grants access across all namespaces (or cluster-scoped resources), which is often not intended.
How to avoid: Always start with Role and RoleBinding for namespace-scoped permissions. Only use ClusterRole and ClusterRoleBinding when you need cluster-wide access, and review such bindings carefully.
How to recover: Delete the ClusterRoleBinding immediately and replace it with a RoleBinding in the appropriate namespace.
Pitfall 4: Incorrect Verbs or Resources
Misspelling a resource or verb (e.g., pod instead of pods) results in no permissions. The Kubernetes API is case-sensitive and plural for most resources.
Why it happens: Human error or relying on memory rather than checking the API reference.
How to avoid: Use kubectl api-resources to see the correct resource names and understand the verbs available. For RBAC, common verbs are get, list, watch, create, update, patch, delete.
How to recover: Correct the resource or verb in the Role manifest and reapply. The RoleBinding does not need to be changed.
Testing Failure: Denied Access
Let's simulate a common mistake: creating a RoleBinding that references a non-existent Role.
Create a file bad-rolebinding.yaml:
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: bad-binding
namespace: rbac-test
subjects:
- kind: ServiceAccount
name: lab-user
namespace: rbac-test
roleRef:
kind: Role
name: non-existent-role
apiGroup: rbac.authorization.k8s.io
Apply it:
kubectl apply -f bad-rolebinding.yaml
It will be created without error, but the service account still cannot list pods because no permissions are granted via this binding. Test:
kubectl auth can-i list pods --as=system:serviceaccount:rbac-test:lab-user -n rbac-test
Expected output: no (assuming you have deleted the good binding as described below; otherwise, the good binding may still grant access).
To truly test, first delete the good binding:
kubectl delete rolebinding read-pods -n rbac-test
Now test again:
kubectl auth can-i list pods --as=system:serviceaccount:rbac-test:lab-user -n rbac-test
Expected: no
This demonstrates that a binding to a non-existent role grants nothing. To recover, delete the bad binding and recreate the correct one:
kubectl delete rolebinding bad-binding -n rbac-test
kubectl create rolebinding read-pods --role=pod-reader --serviceaccount=rbac-test:lab-user -n rbac-test
Test again: yes.
Rollback and Recovery
In a local lab, recovery is often as simple as deleting the incorrect resource and reapplying the correct manifest. Because everything is under version control (in your lab files), you can quickly revert.
If you accidentally grant overly broad permissions (e.g., using a ClusterRoleBinding), you should delete the ClusterRoleBinding immediately:
kubectl delete clusterrolebinding <name>
The local environment is disposable; you can also reset the entire cluster with kind delete cluster --name rbac-lab and recreate it from scratch.
Auditing and Logging
For more advanced diagnostics, enable Kubernetes audit logging or use kubectl get events to see RBAC-related denials. In kind, audit logging is not enabled by default, but you can configure it if needed. For basic labs, kubectl auth can-i and direct API calls are sufficient.
Operations Checklist
Use the following checklist to ensure your Role Binding lab is set up correctly and remains consistent.
Setup Checklist
- [ ] Install kind, kubectl, and Docker.
- [ ] Create a kind cluster with
kind create cluster --name rbac-lab. - [ ] Verify cluster access with
kubectl cluster-info. - [ ] Create a dedicated namespace (
rbac-test). - [ ] Create a service account for testing (
lab-user). - [ ] Define a minimal Role with specific permissions.
- [ ] Create a RoleBinding that links the Role to the service account.
- [ ] Test permissions with
kubectl auth can-iusing proper impersonation.
Verification Checklist
- [ ]
kubectl auth can-i list pods --as=system:serviceaccount:rbac-test:lab-user -n rbac-testreturnsyes. - [ ]
kubectl auth can-i create pods --as=system:serviceaccount:rbac-test:lab-user -n rbac-testreturnsno. - [ ]
kubectl auth can-i list pods --as=system:serviceaccount:rbac-test:lab-user -n defaultreturnsno. - [ ] Describe Role and RoleBinding to confirm correct rules and subjects.
- [ ] (Optional) Test from inside a Pod using the service account token.
Cleanup Checklist
- [ ] Delete the RoleBinding:
kubectl delete rolebinding read-pods -n rbac-test. - [ ] Delete the Role:
kubectl delete role pod-reader -n rbac-test. - [ ] Delete the service account:
kubectl delete serviceaccount lab-user -n rbac-test. - [ ] Delete the namespace:
kubectl delete namespace rbac-test. - [ ] Delete the kind cluster:
kind delete cluster --name rbac-lab.
Review Checklist
- [ ] Review all RBAC manifests for least privilege.
- [ ] Ensure RoleBindings are namespace-scoped and do not accidentally use ClusterRoleBinding unless intended.
- [ ] Document any custom rules for team reference.
- [ ] Keep manifests in version control for reproducibility.
This checklist helps maintain a repeatable and safe lab environment. For team settings, assign a single owner (e.g., the DevOps lead) to review RBAC policies weekly or before each release.
Conclusion
Setting up a local Kubernetes Role Binding lab provides a safe, controlled space to learn and test RBAC configurations. By following this guide, you have created a kind cluster, defined a namespace and service account, applied a Role and RoleBinding, and verified permissions using kubectl auth can-i and direct API calls. You also explored common pitfalls and recovery steps, and you have a checklist to keep your lab organized.
The next step is to experiment with more complex scenarios. Try creating multiple roles with different permissions, bind them to different service accounts, and test combinations. Use kubectl describe to inspect the effective permissions. When you are comfortable, you can apply the same patterns to a real cluster, always starting with least privilege and verifying with kubectl auth can-i before deployment.
Remember, a clear Kubernetes Role Binding setup reduces rework and increases confidence. Use this lab to prototype policies, train team members, and validate changes before they reach production.
Now that you have a verified environment, continue iterating and expanding your RBAC knowledge in a safe, local sandbox.