concept clusterrolebinding in category kubernetes

appears as: ClusterRoleBindings, ClusterRoleBinding, ClusterRoleBinding, clusterrolebindings
Kubernetes in Action

This is an excerpt from Manning's book Kubernetes in Action.

  • RoleBindings and ClusterRoleBindings, which bind the above roles to specific users, groups, or ServiceAccounts.
  • Although you can create a RoleBinding and have it reference a ClusterRole when you want to enable access to namespaced resources, you can’t use the same approach for cluster-level (non-namespaced) resources. To grant access to cluster-level resources, you must always use a ClusterRoleBinding.

    Luckily, creating a ClusterRoleBinding isn’t that different from creating a Role-Binding, but you’ll clean up and delete the RoleBinding first:

    $ kubectl delete rolebinding pv-test
    rolebinding "pv-test" deleted
    
    
    !@%STYLE%@!
    {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":36}]]"}
    !@%STYLE%@!

    Now create the ClusterRoleBinding:

    $ kubectl create clusterrolebinding pv-test --clusterrole=pv-reader
    ➥  --serviceaccount=foo:default
    clusterrolebinding "pv-test" created
    
    
    !@%STYLE%@!
    {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":67}],[{\"line\":1,\"ch\":3},{\"line\":1,\"ch\":31}]]"}
    !@%STYLE%@!

    As you can see, you replaced rolebinding with clusterrolebinding in the command and didn’t (need to) specify the namespace. Figure 12.8 shows what you have now.

    As expected, the pod can get a list of all the pods in the cluster. To summarize, combining a ClusterRoleBinding with a ClusterRole referring to namespaced resources allows the pod to access namespaced resources in any namespace, as shown in figure 12.9.

    Figure 12.9. A ClusterRoleBinding and ClusterRole grants permission to resources across all namespaces.

    Now, let’s see what happens if you replace the ClusterRoleBinding with a RoleBinding. First, delete the ClusterRoleBinding: