<?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: Rohin Pandey</title>
    <description>The latest articles on DEV Community by Rohin Pandey (@rohin21).</description>
    <link>https://dev.to/rohin21</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%2F1035709%2F2206ad83-8c7d-4975-b087-bb79c92d446b.png</url>
      <title>DEV Community: Rohin Pandey</title>
      <link>https://dev.to/rohin21</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rohin21"/>
    <language>en</language>
    <item>
      <title>How add users in Microk8s ?</title>
      <dc:creator>Rohin Pandey</dc:creator>
      <pubDate>Wed, 19 Jun 2024 07:24:22 +0000</pubDate>
      <link>https://dev.to/rohin21/how-add-users-in-microk8s--5fg7</link>
      <guid>https://dev.to/rohin21/how-add-users-in-microk8s--5fg7</guid>
      <description>&lt;p&gt;MicroK8s is a minimal, low-operations Kubernetes designed for production environments. As an open-source platform, it automates the deployment, scaling, and management of containerized applications. It includes core Kubernetes components with a small footprint and can scale from a single node to a high-availability production cluster. Essentially, MicroK8s offers production-grade support for all key Kubernetes features, including advanced networking and storage configurations.&lt;/p&gt;

&lt;p&gt;In this post, we will create a user for microk8s cluster apart from it's default admin user, create a kubeconfig file for the user and use it in kubectl to access the cluster. This is important for a devops engineer to create separate user with appropriate RBAC to insure that a user can access only the required resources in the cluster.&lt;/p&gt;

&lt;p&gt;Make sure you have microk8s running in your system, if not you can install it by running the following command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo snap install microk8s --classic&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we will create user credentials, microk8s uses kubeconfig files to manage user credentials.&lt;/p&gt;

&lt;p&gt;We will use "openssl" to generate certificate and key for the new user. Replace "username" with the desired username.&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 username.key 2048

openssl req -new -key username.key -out username.csr -subj "/CN=username/O=group"

openssl x509 -req -in username.csr -CA /var/snap/microk8s/current/certs/ca.crt -CAkey /var/snap/microk8s/current/certs/ca.key -CAcreateserial -out username.crt -days 365
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this, we are now going to create the kubeconfig file for the new user.&lt;br&gt;
Replace the username, cluster-name and cluster-server with appropriate values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: v1
kind: Config
clusters:
- cluster:
    certificate-authority: /var/snap/microk8s/current/certs/ca.crt
    server: https://&amp;lt;cluster-server&amp;gt;:16443
  name: &amp;lt;cluster-name&amp;gt;
contexts:
- context:
    cluster: &amp;lt;cluster-name&amp;gt;
    user: username
  name: username-context
current-context: username-context
users:
- name: username
  user:
    client-certificate: /path/to/username.crt
    client-key: /path/to/username.key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a role and role binding for the new user in Kubernetes. For example, you can create a role that grants read-only access to all resources in a namespace.&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: default
  name: read-only
rules:
- apiGroups: [""]
  resources: ["pods", "services", "deployments"]
  verbs: ["get", "list", "watch"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, create a role binding to bind the role to the new 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-only-binding
  namespace: default
subjects:
- kind: User
  name: username
  apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: read-only
  apiGroup: rbac.authorization.k8s.io
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also create a cluster role and cluster role binding to provide the user cluster wide access.&lt;/p&gt;

&lt;p&gt;Apply the above RBAC configuration using kubectl&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;microk8s kubectl apply -f role.yaml
microk8s kubectl apply -f rolebinding.yaml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Provide the generated kubeconfig file to the new user. The user can then use this file to access the Kubernetes cluster with the permissions defined by the RBAC configuration.&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>microk8s</category>
      <category>ubuntu</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
