<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: AshutoshM49</title>
    <description>The latest articles on DEV Community by AshutoshM49 (@ashutoshm10).</description>
    <link>https://dev.to/ashutoshm10</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3072180%2F162d5ac4-09fa-41dd-baef-f3472004f868.jpg</url>
      <title>DEV Community: AshutoshM49</title>
      <link>https://dev.to/ashutoshm10</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ashutoshm10"/>
    <language>en</language>
    <item>
      <title>Understanding Kubernetes RBAC</title>
      <dc:creator>AshutoshM49</dc:creator>
      <pubDate>Mon, 21 Apr 2025 17:37:14 +0000</pubDate>
      <link>https://dev.to/ashutoshm10/understanding-kubernetes-rbac-1g9m</link>
      <guid>https://dev.to/ashutoshm10/understanding-kubernetes-rbac-1g9m</guid>
      <description>&lt;h2&gt;
  
  
  What Is RBAC ?
&lt;/h2&gt;

&lt;p&gt;RBAC stands for Role Base Access Control. It's How k8s decides who can do what and where in your cluster. &lt;/p&gt;

&lt;p&gt;We will think like, We are going to a school. There will be teachers, We will be the students, and there will others, for example: Clerk, Head Masters and Many other people. &lt;/p&gt;

&lt;p&gt;There are certain rules for every job in a school, &lt;br&gt;
For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Teachers can take classes&lt;/li&gt;
&lt;li&gt;Students can attend classes&lt;/li&gt;
&lt;li&gt;Only the Principal can expel someone 👀&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Just as a school has its own rules and regulations that everyone must follow, in Kubernetes, no user is above the system's governance. Role-Based Access Control (RBAC) allows us to define specific roles and permissions for users, dictating what actions they can perform within a cluster. For instance, a student's role might be limited to studying and listening to teachers, while a teacher's responsibility is to educate students. Similarly, in many organizational environments, teams, developers, and testers have distinct roles in application deployment and infrastructure management. To monitor application statistics, we assign specific roles to individuals or developers, granting them a defined set of permissions to perform actions within a Kubernetes cluster.&lt;/p&gt;
&lt;h2&gt;
  
  
  RBAC components
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Roles/ClusterRoles&lt;/li&gt;
&lt;li&gt;RoleBindings/ClusterRoleBindings&lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  ClusterRole v/s Roles
&lt;/h2&gt;

&lt;p&gt;The scope of role is within the namespaces&lt;br&gt;
and the scope of clusterRole is in the entire cluster&lt;/p&gt;
&lt;h2&gt;
  
  
  FLOW OF KUBERNETES AUTHENTICATION
&lt;/h2&gt;

&lt;p&gt;User and Service account -&amp;gt; KubeAPI -&amp;gt; Allow/Deny&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Here a User/Service Account requests Kube API, &lt;/li&gt;
&lt;li&gt;Kube API will check if it's authenticated, if any role or clusterRole is present, and if there is a binding role to the user. &lt;/li&gt;
&lt;li&gt;then it will allow or deny &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;NOTE: in kubernetes, there are not any built in concepts of users. instead the authentication handled outside kubernetes typically via: x509, OpenID connect, StaticTokens etc&lt;/p&gt;

&lt;p&gt;But we can proceed with a demo of creating a user by using a certificate. &lt;br&gt;
We’ll use openssl to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a private key&lt;/li&gt;
&lt;li&gt;Generate a certificate signing request (CSR)&lt;/li&gt;
&lt;li&gt;Sign it with the Kubernetes cluster CA&lt;/li&gt;
&lt;li&gt;Create a kubeconfig for the new user&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Step 1: Generate a private key&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl genrsa -out &amp;lt;username&amp;gt;.key 2048
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 2: Create a CSR&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl req -new -key &amp;lt;username&amp;gt;.key -out &amp;lt;username&amp;gt;.csr -subj "/CN=&amp;lt;username&amp;gt;"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 3: Encode the CSR in base64 (no line breaks)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat &amp;lt;username&amp;gt;.csr | base64 | tr -d '\n'

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copy the base64 output.&lt;br&gt;
Step 4: Create a Kubernetes &lt;code&gt;CertificateSigningRequest&lt;/code&gt;resource&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: certificates.k8s.io/v1
kind: CertificateSigningRequest
metadata:
  name: &amp;lt;username&amp;gt;
spec:
  request: &amp;lt;paste-base64-output-here&amp;gt;
  signerName: kubernetes.io/kube-apiserver-client
  expirationSeconds: 31536000  # Optional: 1 year (365 days)
  usages:
    - client auth
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Save it as csr.yaml and apply:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl apply -f csr.yaml

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 5: Approve the CSR&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl certificate approve &amp;lt;username&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once approved, fetch the signed certificate:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl get csr &amp;lt;username&amp;gt; -o jsonpath='{.status.certificate}' | base64 --decode &amp;gt; &amp;lt;username&amp;gt;.crt

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You now have:&lt;br&gt;
.crt → Signed client certificate&lt;br&gt;
.key → Private key&lt;/p&gt;

&lt;p&gt;Step 6: Create a user entry in kubeconfig&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl config set-credentials &amp;lt;username&amp;gt; \
  --client-certificate=&amp;lt;username&amp;gt;.crt \
  --client-key=&amp;lt;username&amp;gt;.key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Step 7: Create a new context for your username&lt;br&gt;
Replace  with the actual cluster name from:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl config get-clusters
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl config set-context your-context \
  --cluster=&amp;lt;your-cluster-name&amp;gt; \
  --namespace=dev \
  --user=&amp;lt;username&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then switch to this context:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl config use-context youruser-context
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point, the user is created and active, but has no permissions yet.&lt;/p&gt;

&lt;p&gt;You need to switch back to kubernetes admin for continuing our demo for RBAC. &lt;br&gt;
Step 1: Create a Role in the dev namespace&lt;br&gt;
Here we created a role, which is applicable for "dev" namespace. and it will access pods to list and watch them. basically a readonly role.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: dev
  name: pod-reader
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list", "watch"]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;apply it.&lt;br&gt;
Step 2: Create a RoleBinding for your user&lt;br&gt;
Here we are creating a roleBinding to bind the role we created with the user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: read-pods
  namespace: dev
subjects:
- kind: User
  name: &amp;lt;username&amp;gt; # this matches CN=username in cert
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: pod-reader # this should be the role which is created to attach with the user. 
  apiGroup: rbac.authorization.k8s.io # it's like a category for a set of APIs in k8s (will read about GVK and GVR for learning more about these)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will eventually try to run command in other namespaces it will give you a failure, and if you try to run the commands to see pods then it will work in the "dev" namespace. &lt;/p&gt;

&lt;p&gt;Why RBAC Matters&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;🛡️ Security first: Least-privilege is the best policy.&lt;/li&gt;
&lt;li&gt;👨‍💻 Multi-team clusters: Keeps teams in their lanes.&lt;/li&gt;
&lt;li&gt;🧪 CI/CD pipelines: Our deployments can only do what they’re allowed to.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>kubernetes</category>
      <category>devops</category>
      <category>k8s</category>
      <category>security</category>
    </item>
  </channel>
</rss>
