<p>So I have a basic setup of kubernetes dashboard as per the official instructions. It works perfectly with cluser-admin role serviceaccount token. But when I create another serviceaccount with it's own ClusterRole and CluserRoleBinding, I cannot log in to the dashboard with the "Authentication failed. Please try again." message.</p>

<p>Here are the steps I take. </p>

<p>1 <code>kubectl create serviceaccount dashboard-reader -n kube-system</code></p>

<p>2 </p>

<pre><code>apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: dashboard-reader
rules:
- apiGroups: ["*"]
  resources: ["*"]
  verbs: ["get", "watch", "list"]
EOF
</code></pre>

<p>3</p>

<pre><code>kubectl apply -f - &lt;&lt;EOF
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: dashboard-reader
  labels:
    k8s-app: kubernetes-dashboard
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: dashboard-reader
subjects:
- kind: ServiceAccount
  name: dashboard-reader
  namespace: kube-system
EOF
</code></pre>

<p>Then I take the token from dashboard-reader-xyz secret and apply it on Dashboard login page.
What I try to achieve is to have separate tokens with various permission for say administrators can log in to dashboard with one token and have full permissions, developers may log in with different token and can only see the resources, etc.</p>

<p>The dashboard version is 1.10.1. 
Kubernetes version is 1.13.5</p>

Mher Harutyunyan
<p>Its possible to create <a href="https://kubernetes.io/docs/reference/access-authn-authz/service-accounts-admin/" rel="nofollow noreferrer">service-account</a> in k8s and restrict it to specific namespace.</p>

<p>Follow these steps:</p>

<ul>
<li>I assume k8s-dashboard is installed on your k8s cluster.</li>
<li>I also assume you have created admin-user to access k8s-dashboard by following <a href="https://github.com/kubernetes/dashboard/wiki/Creating-sample-user" rel="nofollow noreferrer">these</a> steps.</li>
<li>Now to restrict developers to specific namespace on k8s, create a service-account with following contents:</li>
</ul>

<pre><code>---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: mynamespace-user
  namespace: mynamespace

---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: mynamespace-user-full-access
  namespace: mynamespace
rules:
- apiGroups: ["", "extensions", "apps"]
  resources: ["*"]
  verbs: ["*"]
- apiGroups: ["batch"]
  resources:
  - jobs
  - cronjobs
  verbs: ["*"]

---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: mynamespace-user-view
  namespace: mynamespace
subjects:
 - kind: ServiceAccount
  name: mynamespace-user
  namespace: mynamespace
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: mynamespace-user-full-access
</code></pre>

<p>Replace <code>mynamespace</code> with the name of the namespace to which you want to restrict developers.</p>

<ul>
<li>You can login to k8s-dashboard using access token which can be retrieved using this command. </li>
</ul>

<pre><code>kubectl -n mynamespace describe secret $(kubectl -n flow get secret | grep mynamespace-user | awk '{print $1}')
</code></pre>

<ul>
<li>You can also login to k8s-dashboard using kube config. The kube config contents will be:</li>
</ul>

<pre><code>apiVersion: v1
kind: Config
preferences: {}

# Define the cluster
clusters:
- cluster:
    certificate-authority-data: PLACE CERTIFICATE HERE
    # You'll need the API endpoint of your Cluster here:
    server: https://YOUR_KUBERNETES_API_ENDPOINT
  name: my-cluster

# Define the user
users:
- name: mynamespace-user
  user:
    as-user-extra: {}
    client-key-data: PLACE CERTIFICATE HERE
    token: PLACE USER TOKEN HERE

# Define the context: linking a user to a cluster
contexts:
- context:
    cluster: my-cluster
    namespace: mynamespace
    user: mynamespace-user
  name: mynamespace

# Define current context
current-context: mynamespace
</code></pre>

<ul>
<li>The certificate can be retrieved using this command </li>
</ul>

<pre><code>kubectl -n mynamespace get secret $(kubectl -n flow get secret | grep mynamespace-user | awk '{print $1}') -o "jsonpath={.data['ca\.crt']}"
</code></pre>

<p>I have tried these steps in my environment and it works perfectly.</p>

<p>Refer <a href="https://jeremievallee.com/2018/05/28/kubernetes-rbac-namespace-user.html" rel="nofollow noreferrer">this</a> for more info.</p>

<p>Hope this helps.</p>
