<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Aopendkar on Medium]]></title>
        <description><![CDATA[Stories by Aopendkar on Medium]]></description>
        <link>https://medium.com/@aopendkar?source=rss-4ad4780de57d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*m-hyE8GIh4y7AYyO</url>
            <title>Stories by Aopendkar on Medium</title>
            <link>https://medium.com/@aopendkar?source=rss-4ad4780de57d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 21:49:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aopendkar/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[RBAC]]></title>
            <link>https://medium.com/@aopendkar/rbac-022994a0efaa?source=rss-4ad4780de57d------2</link>
            <guid isPermaLink="false">https://medium.com/p/022994a0efaa</guid>
            <dc:creator><![CDATA[Aopendkar]]></dc:creator>
            <pubDate>Tue, 30 Sep 2025 05:24:50 GMT</pubDate>
            <atom:updated>2025-09-30T05:24:50.763Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*HwRlOKKg6RtBga6R.png" /></figure><p>RBAC</p><p>Ina Kubernetes cluster, managing access control is crucial for maintaining security and ensuring that only authorized users have access to resources. Kubernetes RBAC (Role-Based Access Control) provides a powerful mechanism for controlling access to Kubernetes resources based on roles and permissions.</p><p>In this article, we’ll explore Kubernetes RBAC in detail, covering its components, how to define roles and permissions, best practices, and advanced examples.</p><p>Press enter or click to view image in full size</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NU9oeeqa_ChqzdoP.gif" /></figure><p>RBAC Flowchart</p><h3>Overview of RBAC in Kubernetes</h3><p>RBAC in Kubernetes allows you to define roles and permissions for users, groups, or service accounts. It consists of four main components:</p><ul><li><strong>Roles</strong>: Define a set of permissions within a namespace.</li><li><strong>RoleBindings</strong>: Associate roles with users, groups, or service accounts within a namespace.</li><li><strong>ClusterRoles</strong>: Similar to roles, but apply across the entire cluster.</li><li><strong>ClusterRoleBindings</strong>: Associate cluster roles with users, groups, or service accounts across the entire cluster.</li></ul><h3>Understand Roles, RoleBindings, ClusterRoles, and ClusterRoleBindings</h3><h3>Roles &amp; Role Bindings:</h3><p>Let’s say we have a Kubernetes namespace called “development” and we want to grant permissions to a user named “developer” to manage pods within that namespace.</p><p><strong>Role Definition (YAML):</strong></p><pre>apiVersion: rbac.authorization.k8s.io/v1<br>kind: Role<br>metadata:<br>  namespace: development<br>  name: pod-manager<br>rules:<br>- apiGroups: [&quot;&quot;]<br>  resources: [&quot;pods&quot;]<br>  verbs: [&quot;get&quot;, &quot;list&quot;, &quot;create&quot;, &quot;delete&quot;]</pre><p>This role (pod-manager) allows the developer to get, list, create, and delete pods within the development namespace.</p><p><strong>Role Binding (YAML):</strong></p><pre>apiVersion: rbac.authorization.k8s.io/v1<br>kind: RoleBinding<br>metadata:<br>  name: pod-manager-binding<br>  namespace: development<br>subjects:<br>- kind: User<br>  name: developer<br>  apiGroup: rbac.authorization.k8s.io<br>roleRef:<br>  kind: Role<br>  name: pod-manager<br>  apiGroup: rbac.authorization.k8s.io</pre><p>This role binding (pod-manager-binding) binds the pod-manager role to the developer user within the development namespace.</p><h3>Cluster Roles &amp; Cluster Role Bindings:</h3><p>Let’s consider a scenario where we want to grant permissions to view nodes across the entire cluster to a group named “ops-team”.</p><p><strong>Cluster Role Definition (YAML):</strong></p><pre>apiVersion: rbac.authorization.k8s.io/v1<br>kind: ClusterRole<br>metadata:<br>  name: node-viewer<br>rules:<br>- apiGroups: [&quot;&quot;]<br>  resources: [&quot;nodes&quot;]<br>  verbs: [&quot;get&quot;, &quot;list&quot;]</pre><p>This cluster role (node-viewer) allows the ops-team group to get and list nodes across the entire cluster.</p><p><strong>Cluster Role Binding (YAML):</strong></p><pre>apiVersion: rbac.authorization.k8s.io/v1<br>kind: ClusterRoleBinding<br>metadata:<br>  name: node-viewer-binding<br>subjects:<br>- kind: Group<br>  name: ops-team<br>  apiGroup: rbac.authorization.k8s.io<br>roleRef:<br>  kind: ClusterRole<br>  name: node-viewer<br>  apiGroup: rbac.authorization.k8s.io</pre><p>This cluster role binding (node-viewer-binding) binds the node-viewer cluster role to the ops-team group, allowing them to view nodes across the entire cluster.</p><p>In summary, <strong>roles</strong> &amp; <strong>role bindings</strong> are used to manage permissions within namespaces, while <strong>cluster roles</strong> &amp; <strong>cluster role bindings</strong> are used to manage permissions across the entire cluster.</p><h3>Create a new user in Kubernetes Cluster</h3><p>To test RBAC we need to a user, so first create a new user. Below is the step-by-step guide to creating a new user named Anvesh in a Kubernetes cluster</p><h3>Step 1: Generate Certificates for the User</h3><p>Generate a private key for Anvesh using RSA algorithm (4096 bits):</p><pre>openssl genrsa -out anvesh.pem</pre><p>This command will generate an RSA private key.</p><p>Create a Certificate Signing Request (CSR) for Anvesh:</p><pre>openssl req -new -key anvesh.pem -out anvesh.csr -subj &quot;/CN=anvesh&quot;</pre><h3>Step 2: Create a Certificate Signing Request (CSR)</h3><ol><li>Obtain the base64-encoded representation of the CSR:</li></ol><pre>cat anvesh.csr | base64 | tr -d &quot;\n&quot;</pre><p>Here we encode the CSR to be used in the <strong>CertificateSigningRequest</strong>.</p><p>2. Use the output to create a CertificateSigningRequest resource:</p><pre>cat &lt;&lt;EOF | kubectl apply -f -<br>apiVersion: certificates.k8s.io/v1<br>kind: CertificateSigningRequest<br>metadata:<br>  name: anvesh<br>spec:<br>  request: &lt;base64_encoded_csr&gt;<br>  signerName: kubernetes.io/kube-apiserver-client<br>  expirationSeconds: 86400  # one day<br>  usages:<br>  - digital signature<br>  - key encipherment<br>  - client auth<br>EOF</pre><p><strong>CSR status</strong></p><pre>$ kubectl get csr<br>NAME     AGE   SIGNERNAME                            REQUESTOR                    REQUESTEDDURATION   CONDITION<br>anvesh   9s    kubernetes.io/kube-apiserver-client   anveshmuppeda@gmail.com      24h                 Pending</pre><h3>Step 3: Sign the Certificate Using the Cluster Certificate Authority</h3><ol><li>Approve the CertificateSigningRequest for Anvesh:</li></ol><pre>$ kubectl certificate approve anvesh</pre><pre>$ kubectl certificate approve anvesh</pre><pre>certificatesigningrequest.certificates.k8s.io/anvesh approved</pre><p>2. Check the CSR status:</p><pre>$ kubectl describe csr/anvesh</pre><pre>$ kubectl get csr                         <br>NAME     AGE   SIGNERNAME                            REQUESTOR                    REQUESTEDDURATION   CONDITION<br>anvesh   31s   kubernetes.io/kube-apiserver-client   anveshmuppeda@gmail.com      24h                 Approved,Issued</pre><h3>Step 4: Create a Configuration Specific to the User</h3><ol><li><strong>Extract the signed certificate from the CertificateSigningRequest:</strong></li></ol><pre>kubectl get csr/anvesh -o jsonpath=&quot;{.status.certificate}&quot; | base64 -d &gt; anvesh.crt</pre><p><strong>2. Create new user config file:</strong></p><p>Use the kubectl config set-cluster command to set up the cluster information:</p><pre>kubectl --kubeconfig ~/.kube/config-anvesh config set-cluster preprod --insecure-skip-tls-verify=true --server=https://KUBERNETES-API-ADDRESS</pre><p>Replace KUBERNETES-API-ADDRESS with the actual API server address of your Kubernetes cluster.</p><p><strong>3. Set user credentials:</strong></p><p>Use the kubectl config set-credentials command to set up the user credentials:</p><pre>kubectl --kubeconfig ~/.kube/config-anvesh config set-credentials anvesh --client-certificate=anvesh-user.crt --client-key=anvesh.pem --embed-certs=true</pre><p>Replace anvesh-user.crt and anvesh.pem with the paths to your user certificate and private key files respectively.</p><p><strong>4. Set context information:</strong></p><p>Use the kubectl config set-context command to set up the context information:</p><pre>kubectl --kubeconfig ~/.kube/config-anvesh config set-context default --cluster=preprod --user=anvesh</pre><p><strong>5. Use the context:</strong></p><p>Finally, use the kubectl config use-context command to use the newly created context:</p><pre>kubectl --kubeconfig ~/.kube/config-anvesh config use-context default</pre><p>Now, your config-anvesh file is configured with the necessary cluster, user, and context information. You can use it with kubectl commands by passing --kubeconfig ~/.kube/config-anvesh. Make sure to replace placeholder values with your actual configuration details.</p><p>Example:</p><pre>kubectl --kubeconfig ~/.kube/config-anvesh get pods</pre><p>We’ve successfully created a new user named “anvesh”. However, when we try to access the pods using this user, we encounter a <strong>Forbidden</strong> error:</p><pre>$ kubectl --kubeconfig ~/.kube/config-anvesh get pods<br>Error from server (Forbidden): pods is forbidden: User &quot;anvesh&quot; cannot list resource &quot;pods&quot; in API group &quot;&quot; in the namespace &quot;default&quot;</pre><p>This error occurs because we haven’t assigned any permissions to the “<strong>anvesh</strong>” user yet; we’ve only created the user.</p><p>Now, let’s proceed to create new roles and role bindings and associate them with the “<strong>anvesh</strong>” user using the following steps.</p><h3>Writing RBAC Rules</h3><p>Let’s create RBAC rules and apply to the above user:</p><h3>Example 1: Allow read-only access to pods in specific namespace</h3><ol><li><strong>Role YAML:</strong></li></ol><p>Create a file named pod-reader-role.yaml with the following content:</p><pre>apiVersion: rbac.authorization.k8s.io/v1<br>kind: Role<br>metadata:<br>  namespace: kube-system<br>  name: pod-reader<br>rules:<br>- apiGroups: [&quot;&quot;]<br>  resources: [&quot;pods&quot;]<br>  verbs: [&quot;get&quot;, &quot;list&quot;]</pre><p>Apply the Role using:</p><pre>kubectl apply -f pod-reader-role.yaml</pre><p>This Role allows getting and listing pods in the kube-system namespace.</p><p><strong>2. RoleBinding YAML:</strong></p><p>Create a file named anvesh-pod-reader-rolebinding.yaml with the following content:</p><pre>apiVersion: rbac.authorization.k8s.io/v1<br>kind: RoleBinding<br>metadata:<br>  name: anvesh-pod-reader<br>  namespace: kube-system<br>subjects:<br>- kind: User<br>  name: anvesh<br>  apiGroup: rbac.authorization.k8s.io<br>roleRef:<br>  kind: Role<br>  name: pod-reader<br>  apiGroup: rbac.authorization.k8s.io</pre><p>Apply the RoleBinding using:</p><pre>kubectl apply -f anvesh-pod-reader-rolebinding.yaml</pre><p>This RoleBinding binds the “anvesh” user to the “pod-reader” Role in the kube-system namespace.</p><p>Now, the “anvesh” user should be able to list and get pods in the <strong>kube-system</strong> namespace using the provided kubeconfig i.e., ~/.kube/config-anvesh .</p><p>We’ve set up a new role and role binding for the user “anvesh”. Now, let’s try accessing the pods in the kube-system namespace with the following command.</p><pre>$ kubectl --kubeconfig ~/.kube/config-anvesh get pods -n kube-system</pre><p>Output:</p><pre>$ kubectl --kubeconfig ~/.kube/config-anvesh get pods -n kube-system<br>NAME                               READY   STATUS    RESTARTS        AGE<br>cilium-operator-66846d4dd8-x2fpv   1/1     Running   2 (2d19h ago)   10d<br>cilium-whkvz                       1/1     Running   0               11d</pre><p>We’ve successfully listed the pods in the kube-system namespace. However, if you attempt to access other resources and resources outside of the kube-system namespace, you’ll receive a Forbidden error.</p><pre>$ kubectl --kubeconfig ~/.kube/config-anvesh get cm -n kube-system<br>Error from server (Forbidden): configmaps is forbidden: User &quot;anvesh&quot; cannot list resource &quot;configmaps&quot; in API group &quot;&quot; in the namespace &quot;kube-system&quot;</pre><h3>Example 2: Allow read-only access to pods in all namespace</h3><p>You can define a ClusterRole and ClusterRoleBinding in YAML format using below steps to allow the “anvesh” user to list pods across all namespaces.</p><ol><li><strong>ClusterRole YAML:</strong></li></ol><p>Create a file named pod-list-clusterrole.yaml with the following content:</p><pre>apiVersion: rbac.authorization.k8s.io/v1<br>kind: ClusterRole<br>metadata:<br>  name: pod-lister<br>rules:<br>- apiGroups: [&quot;&quot;]<br>  resources: [&quot;pods&quot;]<br>  verbs: [&quot;get&quot;, &quot;list&quot;]</pre><p>Apply the <strong>ClusterRole</strong> using:</p><pre>kubectl apply -f pod-list-clusterrole.yaml</pre><p>This ClusterRole allows getting and listing pods across all namespaces.</p><p><strong>2. ClusterRoleBinding YAML:</strong></p><p>Create a file named anvesh-pod-lister-clusterrolebinding.yaml with the following content:</p><pre>apiVersion: rbac.authorization.k8s.io/v1<br>kind: ClusterRoleBinding<br>metadata:<br>  name: anvesh-pod-lister<br>subjects:<br>- kind: User<br>  name: anvesh<br>  apiGroup: rbac.authorization.k8s.io<br>roleRef:<br>  kind: ClusterRole<br>  name: pod-lister<br>  apiGroup: rbac.authorization.k8s.io</pre><p>Apply the <strong>ClusterRoleBinding</strong> using:</p><pre>kubectl apply -f anvesh-pod-lister-clusterrolebinding.yaml</pre><p>This ClusterRoleBinding binds the “anvesh” user to the “pod-lister” ClusterRole, allowing the user to list pods across all namespaces.</p><p>Now, the “anvesh” user should be able to list pods across all namespaces using the provided kubeconfig i.e., ~/.kube/config-anvesh .</p><p>We’ve set up a new <strong>clusterRole</strong> and <strong>clusterRoleBinding</strong> for the user “anvesh”. Now, let’s try accessing the pods in all namespaces with the following command.</p><pre>kubectl --kubeconfig ~/.kube/config-anvesh get po -A</pre><p>Output:</p><pre>$ kubectl --kubeconfig ~/.kube/config-anvesh get pods -A            <br>NAMESPACE        NAME                                        READY   STATUS      RESTARTS        AGE<br>ingress-nginx    ingress-nginx-admission-create-6mbxh        0/1     Completed   0               7d21h<br>ingress-nginx    ingress-nginx-admission-patch-4p82d         0/1     Completed   1               7d21h<br>ingress-nginx    ingress-nginx-controller-7dcdbcff84-kqc2n   1/1     Running     0               7d21h<br>kube-system      cilium-operator-66846d4dd8-x2fpv            1/1     Running     2 (2d19h ago)   10d<br>kube-system      cilium-whkvz                                1/1     Running     0               11d<br>kube-system      coredns-6857f5494d-lq2c8                    1/1     Running     0               10d<br>kube-system      coredns-6857f5494d-xt7rw                    1/1     Running     0               10d<br>kube-system      cpc-bridge-proxy-hdzn6                      1/1     Running     0               11d<br>kube-system      csi-do-node-v52xs                           2/2     Running     0               11d<br>kube-system      do-node-agent-25w77                         1/1     Running     0               11d<br>kube-system      hubble-relay-778856d87d-hkbjf               1/1     Running     0               10d<br>kube-system      hubble-ui-776986f894-l9qnm                  2/2     Running     0               10d<br>kube-system      konnectivity-agent-lhq85                    1/1     Running     0               11d<br>kube-system      kube-proxy-4bk9h                            1/1     Running     0               11d</pre><p>We’ve successfully listed the pods in all namespaces. However, if you attempt to access other resources(other than pods) in any namespace, you’ll receive a Forbidden error.</p><pre>$ kubectl --kubeconfig ~/.kube/config-anvesh get cm -A  <br>Error from server (Forbidden): configmaps is forbidden: User &quot;anvesh&quot; cannot list resource &quot;configmaps&quot; in API group &quot;&quot; at the cluster scope</pre><p>We have successfully created a user in the Kubernetes cluster and granted access at both the namespace and cluster levels using roles, role bindings, cluster roles, and cluster role bindings.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=022994a0efaa" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>