Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
kubectl create rolebinding devs-view -n shop --clusterrole=view --group=shop-devs
kubectl auth can-i list pods -n shop --as=alice --as-group=shop-devs
yes
kubectl auth can-i delete pods -n shop --as=alice --as-group=shop-devs
no
kubectl auth can-i get secrets -n shop --as=alice --as-group=shop-devs
noThe built-in **view ClusterRole reads almost everything in a namespace except Secrets**.
Step by step
- Use the built-in roles first:
| ClusterRole | Gives |
|---|---|
view | Read most objects (no Secrets, no RBAC objects). |
edit | Read and change most objects (no RBAC). |
admin | Full control in the namespace, also RoleBindings. |
cluster-admin | Everything everywhere — almost never for people. |
- Bind to a group, not to single users — groups come from your identity provider (OIDC, Entra ID, IAM). Binding
viewwith a RoleBinding limits it to that namespace (quick answer). - Logs and port-forward?
viewalready includespods/log. For debugging extras, add a small Role:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: devs-debug
namespace: shop
rules:
- apiGroups: [""]
resources: ["pods/portforward"]
verbs: ["create"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: devs-debug
namespace: shop
subjects:
- kind: Group
name: shop-devs
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: devs-debug
apiGroup: rbac.authorization.k8s.ioDo not give pods/exec in production lightly — it is a shell inside the app (and can read Secrets mounted there).
- Check before telling people "done" — impersonate them:
kubectl auth can-i --list -n shop --as=alice --as-group=shop-devs
kubectl auth can-i get pods/log -n shop --as=alice --as-group=shop-devs- Service accounts (tools, CI): the same idea with
--serviceaccount=NAMESPACE:NAME; give them only what they use, and turn off auto-mounted tokens where not needed. - Review regularly: who has
cluster-admin?
kubectl get clusterrolebindings -o jsonpath='{range .items[?(@.roleRef.name=="cluster-admin")]}{.metadata.name}{" "}{.subjects[*].name}{"\n"}{end}'Check that it worked
The user (or group) can list Pods, read logs and events in shop, and gets Forbidden for delete, edit, Secrets and other namespaces.
Common problems
| Problem | Cause | Fix |
|---|---|---|
Forbidden for everything | The group name in the binding does not match the token's group. | Check the group claim from your IdP; test with --as-group. |
| User can read Secrets | Used edit/admin, or a custom Role with secrets. | Use view; remove secrets from custom roles. |
| Access in all namespaces | A ClusterRoleBinding was used. | Use a RoleBinding per namespace. |
kubectl logs forbidden | Missing pods/log (custom role). | Add pods/log with get. |
| Can not change a RoleBinding later | roleRef can not be edited. | Delete and create the binding again. |
| A ServiceAccount has too many rights | Old bindings / default token. | Review bindings; automountServiceAccountToken: false. |
Go further
- Command pages: kubectl.
- Next how-tos: Switch between clusters and namespaces, Get the kubeconfig of EKS, AKS or GKE, Write a Kubernetes NetworkPolicy.
- Learn it: CKA — RBAC objects, CKA — can-i, CKA — Built-in roles, CKS — RBAC hardening, CKS — ServiceAccounts.
1Which built-in ClusterRole gives read access without Secrets?
view.