<p>I'm having a trouble setting up access to kubernetes cluster from the outside. This is what I'm trying to achieve:
- Have ability to access to kube cluster from the outside (from nodes that are not "master" and even from any remote) to be able to do kube actions only on specific namespace.</p>

<p>My logic was to do following:</p>

<ul>
<li>Create new namespace (let's call it testns)</li>
<li>Create service account (testns-account)</li>
<li>Create role which gives access for creating any type of kube resource inside testns namespace</li>
<li>Create role binding which binds service account with role</li>
<li>Generate token from service account</li>
</ul>

<p>Now, my logic was that I need to have token + api server URL to access kube cluster with limited "permissions" but that doesnt seem like it is enough.</p>

<p>What would be the easiest way to achieve this? For start, I could have access with kubectl just to verify that limited permissions on namespace work but eventually, I would have some client side code which doing the access and creates kube resources with these limited permissions.</p>

Bakir Jusufbegovic
<p>You need to generate a kubeconfig from the token. There are <a href="https://github.com/zlabjp/kubernetes-scripts/blob/master/create-kubeconfig" rel="nofollow noreferrer">scripts</a> to handle this. Here it is for posterity:</p>
<h1>!/usr/bin/env bash</h1>
<pre><code># Copyright 2017, Z Lab Corporation. All rights reserved.
# Copyright 2017, Kubernetes scripts contributors
#
# For the full copyright and license information, please view the LICENSE
# file that was distributed with this source code.

set -e

if [[ $# == 0 ]]; then
  echo &quot;Usage: $0 SERVICEACCOUNT [kubectl options]&quot; &gt;&amp;2
  echo &quot;&quot; &gt;&amp;2
  echo &quot;This script creates a kubeconfig to access the apiserver with the specified serviceaccount and outputs it to stdout.&quot; &gt;&amp;2

  exit 1
fi

function _kubectl() {
  kubectl $@ $kubectl_options
}

serviceaccount=&quot;$1&quot;
kubectl_options=&quot;${@:2}&quot;

if ! secret=&quot;$(_kubectl get serviceaccount &quot;$serviceaccount&quot; -o 'jsonpath={.secrets[0].name}' 2&gt;/dev/null)&quot;; then
  echo &quot;serviceaccounts \&quot;$serviceaccount\&quot; not found.&quot; &gt;&amp;2
  exit 2
fi

if [[ -z &quot;$secret&quot; ]]; then
  echo &quot;serviceaccounts \&quot;$serviceaccount\&quot; doesn't have a serviceaccount token.&quot; &gt;&amp;2
  exit 2
fi

# context
context=&quot;$(_kubectl config current-context)&quot;
# cluster
cluster=&quot;$(_kubectl config view -o &quot;jsonpath={.contexts[?(@.name==\&quot;$context\&quot;)].context.cluster}&quot;)&quot;
server=&quot;$(_kubectl config view -o &quot;jsonpath={.clusters[?(@.name==\&quot;$cluster\&quot;)].cluster.server}&quot;)&quot;
# token
ca_crt_data=&quot;$(_kubectl get secret &quot;$secret&quot; -o &quot;jsonpath={.data.ca\.crt}&quot; | openssl enc -d -base64 -A)&quot;
namespace=&quot;$(_kubectl get secret &quot;$secret&quot; -o &quot;jsonpath={.data.namespace}&quot; | openssl enc -d -base64 -A)&quot;
token=&quot;$(_kubectl get secret &quot;$secret&quot; -o &quot;jsonpath={.data.token}&quot; | openssl enc -d -base64 -A)&quot;

export KUBECONFIG=&quot;$(mktemp)&quot;
kubectl config set-credentials &quot;$serviceaccount&quot; --token=&quot;$token&quot; &gt;/dev/null
ca_crt=&quot;$(mktemp)&quot;; echo &quot;$ca_crt_data&quot; &gt; $ca_crt
kubectl config set-cluster &quot;$cluster&quot; --server=&quot;$server&quot; --certificate-authority=&quot;$ca_crt&quot; --embed-certs &gt;/dev/null
kubectl config set-context &quot;$context&quot; --cluster=&quot;$cluster&quot; --namespace=&quot;$namespace&quot; --user=&quot;$serviceaccount&quot; &gt;/dev/null
kubectl config use-context &quot;$context&quot; &gt;/dev/null

cat &quot;$KUBECONFIG&quot;
</code></pre>
