avatarrouterhan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4303

Abstract

rd">namespace</span> <span class="hljs-title">ns1</span></pre></div><h2 id="8e3a">ClusterRole:</h2><ul><li>ClusterRoles are designed for cluster-wide authorization.</li><li>Unlike Roles, they are <b>applicable to the entire cluster</b>.</li><li>ClusterRoles provide access control for cluster-wide resources like nodes, persistent volumes, or custom resources.</li></ul><p id="7760">Example:</p><ul><li>create a ClusterRole named <code>democlusterrole</code> with permissions to get and list nodes. This allows the user assigned with this ClusterRole to perform operations like retrieving and listing nodes across the entire cluster:</li></ul><div id="75e3"><pre> kubectl <span class="hljs-built_in">create</span> clusterrole democlusterrole <span class="hljs-comment">--verb=get,list --resource=nodes</span></pre></div><h1 id="c4e5">RoleBinding and ClusterRoleBinding</h1><p id="8e92">RBAC in Kubernetes revolves the following key resources to bind Role(with allowed operations) to users:</p><h2 id="7b84">RoleBinding:</h2><ul><li>A RoleBinding in Kubernetes binds a Role to a user, group, or service account within a specific namespace.</li><li>It <b>specifies who has the permissions defined by the Role</b>.</li></ul><p id="f448">Example:</p><ul><li>Create a RoleBinding named <code>demorolebinding</code> in the <code>ns1</code> namespace, binding the <code>demorole</code> Role to the <code>demouser</code> user. This allows the <code>demouser</code> to have the permissions defined by the <code>demorole</code> within the <code>ns1</code> namespace:</li></ul><div id="c863"><pre> kubectl <span class="hljs-built_in">create</span> rolebinding demorolebinding <span class="hljs-comment">--role=demorole --user=demouser --namespace ns1</span></pre></div><h2 id="ae97">ClusterRoleBinding:</h2><ul><li>A ClusterRoleBinding is similar to a RoleBinding but applies to the entire cluster instead of a specific namespace.</li><li>It binds a ClusterRole to a user, group, or service account across all namespaces.</li></ul><p id="0434">Example:</p><ul><li>Create a ClusterRoleBinding named <code>democlusterrolebinding</code>, binding the <code>democlusterrole</code> ClusterRole to the <code>demouser</code>. This allows the <code>demouser</code> to have the permissions defined by the <code>democlusterrole</code> across all namespaces in the cluster:</li></ul><div id="5ef5"><pre> kubectl <span class="hljs-built_in">create</span> clusterrolebinding democlusterrolebinding <span class="hljs-comment">--clusterrole=democlusterrole --user=demouser</span></pre></div><p id="2dfd"><code>RoleBindings</code> and <code>ClusterRoleBindings</code> are essential for managing and controlling access to resources within a Kubernetes cluster.</p><p id="4dd1">They define who has the permissions specified by the associated Roles or ClusterRoles, allowing for fine-grained authorization control.</p><h1 id="9763">Validating Role and RoleBinding</h1><p id="42da">To ensure that the Role and RoleBinding are functioning as expected, we can perform various <code>kubectl</code> commands to test the permissions and access for the demouser.</p><p id="27d3">Let's go through some examples:</p><p id="775f">1.First, let’s switch to the cluster administrator context to create some resources:</p><div id="7a3c"><pre> kubectl <span class="hljs-built_in">config</span> use-context kubernetes-admin@kubernetes kubectl <span class="hljs-built_in">create</span> namespace ns1 kubectl <span class="hljs-built_in">create</span> deployment web1 <span class="hljs-comment">--namespace=ns1 --image=gcr.io/google-samples/hello-app:1.0 --port=8080 --replicas=2</span></pre></div><p id="4a82">2.Now, let’s test the demouser’s permissions using the <code>auth can-i</code> command:</p><div id="464b"><pre><span class="hljs-variable"> </span>kubectl auth can-i list pod yes <span class="hljs-variable"> </span>kubectl auth can-i list pod --as demouser no</pre></div><p id="452c">3.Next, we will create the Role and RoleBinding:</p><div id="ba91"><pre>kubectl <span class="hljs-built_in">create</span> role demorole <span class="hljs-comment">--verb=get,list --resource=pods --namespace ns1</span> role.rbac.authorization.k8s.<span class="hljs-built_in">io</span>/demorole created kubectl <span class="hljs-built_in">create<

Options

/span> rolebinding demorolebinding <span class="hljs-comment">--role=demorole --user=demouser --namespace ns1</span> rolebinding.rbac.authorization.k8s.<span class="hljs-built_in">io</span>/demorolebinding created</pre></div><p id="f641">4.Now, let’s test the demouser’s permissions again:</p><div id="ed1d"><pre>kubectl auth can<span class="hljs-operator">-</span>i list pod <span class="hljs-comment">--as demouser</span> <span class="hljs-keyword">no</span> kubectl auth can<span class="hljs-operator">-</span>i list pod <span class="hljs-comment">--as demouser --namespace ns1</span> yes kubectl <span class="hljs-keyword">get</span> pods <span class="hljs-comment">--namespace ns1 --as demouser</span> NAME READY STATUS RESTARTS AGE web1<span class="hljs-number">-7</span>f6c665f7d<span class="hljs-number">-65</span>h6v <span class="hljs-number">1</span><span class="hljs-operator">/</span><span class="hljs-number">1</span> <span class="hljs-keyword">Running</span> <span class="hljs-number">0</span> <span class="hljs-number">9</span>m38s web1<span class="hljs-number">-7</span>f6c665f7d<span class="hljs-operator">-</span>n54t5 <span class="hljs-number">1</span><span class="hljs-operator">/</span><span class="hljs-number">1</span> <span class="hljs-keyword">Running</span> <span class="hljs-number">0</span> <span class="hljs-number">9</span>m38s</pre></div><p id="d1f6">5.We can also test other actions and resources:</p><div id="66a8"><pre> kubectl auth can-i <span class="hljs-keyword">delete</span> pod --<span class="hljs-keyword">as</span> demouser --<span class="hljs-keyword">namespace</span> ns1 no kubectl auth can-i list node --as demouser --<span class="hljs-keyword">namespace</span> ns1 <span class="hljs-title class_">Warning</span>: resource 'nodes' is not <span class="hljs-keyword">namespace</span> scoped no kubectl auth can-i list deployment --as demouser --<span class="hljs-keyword">namespace</span> ns1 no</pre></div><p id="e2a5">By performing these commands, we can see that the <code>demouser</code> has limited access based on the defined <code>Role</code> and <code>RoleBinding</code>. The demouser can list pods in the <code>ns1</code> namespace but does not have permissions for other actions or resources.</p><p id="75b3">This validates that the Role and RoleBinding have been successfully configured and are working as expected.</p><h1 id="53e1">Key Takeaway</h1><p id="bbd6">In this article, we explored the concept of authorization in Kubernetes and focused on Role and RoleBinding. Here are the key takeaways:</p><ul><li>Roles define <b>what actions a user or group can perform on specific resources within a namespace</b>.</li><li>RoleBindings <b>associate a user or group with a Role</b>, granting them the defined permissions.</li><li>ClusterRole and ClusterRoleBinding are used for defining and binding permissions across all namespaces.</li><li>By creating Roles and RoleBindings, we can ensure that users have the appropriate access levels and restrictions within the cluster.</li><li>Validating the configuration with kubectl commands helps confirm that the RBAC settings are working as intended.</li></ul><p id="4d9a">By leveraging RBAC and properly configuring Roles and RoleBindings, you can ensure that only authorized users have access to resources and actions within the cluster.</p><p id="e0c1">Remember, authorization plays a vital role in securing your Kubernetes cluster, so it’s important to understand and implement RBAC effectively.</p><blockquote id="4255"><p>🔔 Stay tuned or <a href="https://routerhan.medium.com/subscribe">subscribe </a>to my series: “<a href="https://routerhan.medium.com/a-beginners-guide-to-kubernetes-e0f1c2866d36"><b>Understanding Kubernetes — A Beginner’s Guide</b></a>” to explore everything about Kubernetes. 🚀</p></blockquote><blockquote id="7375"><p>➕Join the <a href="https://medium.com/@routerhan/membership">Medium Membership Program</a> to support my work and connect with other writers.</p></blockquote><blockquote id="1daa"><p>📝 Have questions or suggestions? Leave a comment or message me through Medium. Let’s connect!</p></blockquote><blockquote id="2dea"><p>Thank you for your support! 🌟</p></blockquote></article></body>

Understanding Kubernetes Authorization — A Beginner’s Guide

What are RBAC and Access Control in Kubernetes?

Find Complete mind map of A Beginner’s Guide to Kubernetes

In our previous articles on authentication and the kubeconfig file, we explored how to establish trust and authenticate users in a Kubernetes cluster.

However, you may recall encountering an error when trying to perform certain actions, such as running the kubectl get nodes command. This error highlights the importance of authorization in Kubernetes, which controls the permissions and access levels granted to users.

In this article, we will delve into the world of authorization and focus on one of its key components: Role-Based Access Control (RBAC). By understanding RBAC, we can enable fine-grained control over user permissions and ensure secure operations within the cluster.

Now, let’s delve deeper into the RBAC authorization mode and its key involved api objects.

Check out “Understanding Kubernetes — A Beginner’s Guide” for the comprehensive series🚀

Roles Based Access Control

Once a client’s identity is authenticated, authorization comes into play to determine what actions and resources they are allowed to access within the cluster. It is an essential layer of security that ensures only authorized entities can perform actions on specific resources.

RBAC (Role-Based Access Control) in Kubernetes extends the RBAC model and introduces the following key objects:

  • Role and ClusterRole
  • RoleBinding and ClusterRoleBinding

Let’s check these out!

Role and ClusterRole

RBAC in Kubernetes revolves the following key resources to define the actions:

Role:

  • A Role in Kubernetes defines the permissions and access levels for a specific namespace.
  • It allows fine-grained control over resources within the namespace, such as pods, services, and deployments.
  • Roles are created within a specific namespace and provide authorization within that namespace only.
  • They grant or restrict access to various operations like creating, reading, updating, or deleting resources.

Example:

  • create a Role named demorole in the ns1 namespace with permissions to get and list pods. This allows the user assigned with this Role to perform operations like retrieving and listing pods within the ns1 namespace:
$ kubectl create role demorole --verb=get,list --resource=pods --namespace ns1

ClusterRole:

  • ClusterRoles are designed for cluster-wide authorization.
  • Unlike Roles, they are applicable to the entire cluster.
  • ClusterRoles provide access control for cluster-wide resources like nodes, persistent volumes, or custom resources.

Example:

  • create a ClusterRole named democlusterrole with permissions to get and list nodes. This allows the user assigned with this ClusterRole to perform operations like retrieving and listing nodes across the entire cluster:
$ kubectl create clusterrole democlusterrole --verb=get,list --resource=nodes

RoleBinding and ClusterRoleBinding

RBAC in Kubernetes revolves the following key resources to bind Role(with allowed operations) to users:

RoleBinding:

  • A RoleBinding in Kubernetes binds a Role to a user, group, or service account within a specific namespace.
  • It specifies who has the permissions defined by the Role.

Example:

  • Create a RoleBinding named demorolebinding in the ns1 namespace, binding the demorole Role to the demouser user. This allows the demouser to have the permissions defined by the demorole within the ns1 namespace:
$ kubectl create rolebinding demorolebinding --role=demorole --user=demouser --namespace ns1

ClusterRoleBinding:

  • A ClusterRoleBinding is similar to a RoleBinding but applies to the entire cluster instead of a specific namespace.
  • It binds a ClusterRole to a user, group, or service account across all namespaces.

Example:

  • Create a ClusterRoleBinding named democlusterrolebinding, binding the democlusterrole ClusterRole to the demouser. This allows the demouser to have the permissions defined by the democlusterrole across all namespaces in the cluster:
$ kubectl create clusterrolebinding democlusterrolebinding --clusterrole=democlusterrole --user=demouser

RoleBindings and ClusterRoleBindings are essential for managing and controlling access to resources within a Kubernetes cluster.

They define who has the permissions specified by the associated Roles or ClusterRoles, allowing for fine-grained authorization control.

Validating Role and RoleBinding

To ensure that the Role and RoleBinding are functioning as expected, we can perform various kubectl commands to test the permissions and access for the demouser.

Let's go through some examples:

1.First, let’s switch to the cluster administrator context to create some resources:

$ kubectl config use-context kubernetes-admin@kubernetes
$ kubectl create namespace ns1
$ kubectl create deployment web1 --namespace=ns1 --image=gcr.io/google-samples/hello-app:1.0 --port=8080 --replicas=2

2.Now, let’s test the demouser’s permissions using the auth can-i command:

$ kubectl auth can-i list pod
yes
$ kubectl auth can-i list pod --as demouser
no

3.Next, we will create the Role and RoleBinding:

$ kubectl create role demorole --verb=get,list --resource=pods --namespace ns1
role.rbac.authorization.k8s.io/demorole created
$ kubectl create rolebinding demorolebinding --role=demorole --user=demouser --namespace ns1
rolebinding.rbac.authorization.k8s.io/demorolebinding created

4.Now, let’s test the demouser’s permissions again:

$ kubectl auth can-i list pod --as demouser
no
$ kubectl auth can-i list pod --as demouser --namespace ns1
yes
$ kubectl get pods --namespace ns1 --as demouser
NAME                    READY   STATUS    RESTARTS   AGE
web1-7f6c665f7d-65h6v   1/1     Running   0          9m38s
web1-7f6c665f7d-n54t5   1/1     Running   0          9m38s

5.We can also test other actions and resources:

$ kubectl auth can-i delete pod --as demouser --namespace ns1
no
$ kubectl auth can-i list node --as demouser --namespace ns1
Warning: resource 'nodes' is not namespace scoped
no
$ kubectl auth can-i list deployment --as demouser --namespace ns1
no

By performing these commands, we can see that the demouser has limited access based on the defined Role and RoleBinding. The demouser can list pods in the ns1 namespace but does not have permissions for other actions or resources.

This validates that the Role and RoleBinding have been successfully configured and are working as expected.

Key Takeaway

In this article, we explored the concept of authorization in Kubernetes and focused on Role and RoleBinding. Here are the key takeaways:

  • Roles define what actions a user or group can perform on specific resources within a namespace.
  • RoleBindings associate a user or group with a Role, granting them the defined permissions.
  • ClusterRole and ClusterRoleBinding are used for defining and binding permissions across all namespaces.
  • By creating Roles and RoleBindings, we can ensure that users have the appropriate access levels and restrictions within the cluster.
  • Validating the configuration with `kubectl` commands helps confirm that the RBAC settings are working as intended.

By leveraging RBAC and properly configuring Roles and RoleBindings, you can ensure that only authorized users have access to resources and actions within the cluster.

Remember, authorization plays a vital role in securing your Kubernetes cluster, so it’s important to understand and implement RBAC effectively.

🔔 Stay tuned or subscribe to my series: “Understanding Kubernetes — A Beginner’s Guide” to explore everything about Kubernetes. 🚀

➕Join the Medium Membership Program to support my work and connect with other writers.

📝 Have questions or suggestions? Leave a comment or message me through Medium. Let’s connect!

Thank you for your support! 🌟

Kubernetes
Cloud Computing
DevOps
Authorization
Software Development
Recommended from ReadMedium