<p>Im trying to emulate role based access from an external server. My scenario is that I want a user "developer" who only has access to create / delete / list pods and have access persistent volume claims. I have a role and rolebinding. I have tested it by using</p>

<pre><code>[root@K8Smaster ~]# kubectl auth can-i create pods --as=developer
yes
</code></pre>

<p>So I know this part is working. As you can see its being done on the K8S master server.</p>

<p>To get it working on an external server I</p>

<pre><code>openssl genrsa -out developer.key 2048

openssl req -new -key developer.key -subj "/CN=developer/O=User" -out developer.csr
</code></pre>

<p>(I assume the CN and O really dont mean anything...or do they have to match something?, I matched the CN name with the User "developer")</p>

<pre><code>openssl x509 -req -in developer.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out developer.crt
</code></pre>

<p>I copied the ca.cert / developer.crt / developer.key files to my external server.</p>

<p>On my external server I installed kubectl.</p>

<p>I followed
<a href="https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/" rel="nofollow noreferrer">https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/</a></p>

<p>Though its not super clear as to which server I create these files and run the commands on (master or remote) seems like creating the context is on the master and everything else is on the remote</p>

<p>On the remote server if I run</p>

<pre><code> [developer@server ~]$ kubectl get pods
The connection to the server 10.237.107.61 was refused - did you specify the right host or port?
</code></pre>

<p>Or if it run</p>

<pre><code>    curl https://10.237.107.61:6443/api/v1/pods --key developer.key --cert developer.crt --cacert ca.crt
{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {

  },
  "status": "Failure",
  "message": "pods is forbidden: User \"system:anonymous\" cannot list resource \"pods\" in API group \"\" at the cluster scope",
  "reason": "Forbidden",
  "details": {
    "kind": "pods"
  },
</code></pre>

<p>Not sure what Im missing.</p>

encore02
<p>Kubectl is using a config file you must have to connect to the cluster. It is possible that your config file is inconsistent due to a lot of major or minor changes. If further analyses of the issues does not show good results, try to <code>rm -f ~/.kube/config</code> and start it from scratch.</p>

<p>As I see, you suspect that the problem is with the self signed certificates. It may require updating cluster root Certificate Authority (CA) on clients, then refreshing the local list for valid certificates.</p>

<p>Go to your local CA directory, check if ca.crt file exists, then copy it to the clients. For clients, perform the following operations:</p>

<pre><code>$ sudo cp ca.crt /usr/local/share/ca-certificates/kubernetes.crt
$ sudo update-ca-certificates

</code></pre>

<p>cluster: tag accepts either the filename of the CA certificate or an "inline" base64-ed version of the PEM you can see it with:</p>

<pre><code>$ kubectl config set-cluster $foo --certificate-authority=... --embed-certs=true
</code></pre>

<p>During deployment part you have to configure also the cluster and user on external server.</p>

<p>Execute command below:</p>

<p><code>$ kubectl config --kubeconfig=your_kubeconfig_path set-context abc 
 --cluster=10.237.107.61:6443 --user=developer --namespace=default</code> </p>

<p>Context designates namespace/cluster-name - in your case cluster IP/cluster-user.</p>

<pre><code>$ kubectl config use-context abc
</code></pre>

<p>While you will enter a <code>kubectl</code> command, the action will apply to the cluster, and namespace listed in the <code>abc</code> context. And the command will use the credentials of the user listed in the abc` context.</p>

<p>Finally if this doesn't work on external server than try to copy only developer user's related data from kubeconfig file on master k8s and copy it into external server kubeconfig file then run this commad again.</p>

<p>If you run the <a href="http://kubernetes.io/v1.0/docs/getting-started-guides/vagrant.html" rel="nofollow noreferrer">local Kubernetes set up using Vagrant</a> you’ll notice that the <code>~/.kube/config</code> file gets set up automatically after the clusters comes up; you’ll also feel comforted that the scripts which provision kubernetes inside vagrant also use these commands to <a href="https://github.com/christian-posta/kubernetes/blob/master/cluster/common.sh#L49-49" rel="nofollow noreferrer">set up your ~/.kube/config</a>.</p>

<p>Please take a look: <a href="https://stackoverflow.com/questions/50304978/kubectl-connect-with-remote-cluster-from-scratch">connecting-to-cluster</a>, <a href="https://blog.christianposta.com/kubernetes/logging-into-a-kubernetes-cluster-with-kubectl/" rel="nofollow noreferrer">logging-into-cluster</a>.</p>
