Overview
Steps we’ll follow:- Create the user with
oc create user. - Create a ClusterRole that grants read-only access to pods.
- Bind that ClusterRole to the user with a ClusterRoleBinding.
- Verify the binding and test the user’s permissions.
1) Create the user
Create a user namedmikeuser and confirm it exists:
2) Create a ClusterRole and ClusterRoleBinding (single YAML)
Place both the ClusterRole and ClusterRoleBinding into one YAML file (for examplerole.yaml). This ClusterRole grants only read verbs (get, list, watch) on pods; the binding attaches that role to the mikeuser user.
Remember: subject kinds are case-sensitive. Use
User, Group, or ServiceAccount exactly as shown.kind field under subjects is case-sensitive. Using user (lowercase) will cause the ClusterRoleBinding to be rejected.
3) Apply the YAML
Apply the combined YAML to create the ClusterRole and ClusterRoleBinding:4) Verify the binding and test permissions
-
In the OpenShift Console: go to Users → select
mikeuser→ RoleBindings. You should seemike-can-read-podsreferencingmikesreaduser, which allowsget,list, andwatchonpods. -
Example
oc get/oc describeoutput for the ClusterRoleBinding (this mirrors what you might see in the console or fromoc get clusterrolebinding -o yaml):
- Optional quick permission test (run as a cluster-admin or a user who can impersonate):
yes (if the binding is effective). Try a write verb to confirm denial:
no
5) Restricting access to a single namespace
If you want the user limited to a single namespace instead of cluster-wide pod access, create aRole and a RoleBinding in that namespace (namespace-scoped). Replace kind: ClusterRole / ClusterRoleBinding with Role / RoleBinding and include the target metadata.namespace in both objects.
Quick reference
Links and references
- OpenShift user management: https://docs.openshift.com/
- Kubernetes RBAC: https://kubernetes.io/docs/reference/access-authn-authz/rbac/
- oc CLI reference: https://docs.openshift.com/container-platform/latest/cli_reference/openshift_cli/getting-started-cli.html
mikeuser can view pods cluster-wide (or, if you prefer, only in a namespace) without being able to modify resources.