<p>With a Kubernetes cluster up and running and the ability to go to the master over ssh with ssh-keys and run kubectl commands there; I want to run kubectl commands on my local machine. So I try to setup the configuration, following the <a href="https://kubernetes-v1-4.github.io/docs/user-guide/kubectl/kubectl_config_set-cluster/" rel="nofollow noreferrer">kubectl config</a>:</p>

<pre><code>kubectl config set-cluster mykube --server=https://&lt;master-ip&gt;:6443
kubectl config set-context mykube --cluster=mykube --user=mykube-adm
kubectl config set-credentials mykube-adm --client-key=path/to/private/keyfile 
</code></pre>

<p>Activate the context:</p>

<pre><code>kubectl config use-context mykube
</code></pre>

<p>When I run a kubectl command:</p>

<pre><code>kubectl get nodes
</code></pre>

<p>It returns:</p>

<blockquote>
  <p>The connection to the server localhost:8080 was refused - did you specify the right host or port?</p>
</blockquote>

<p><strong>The output of <code>kubectl config view</code></strong></p>

<pre><code>apiVersion: v1
clusters:
- cluster:
    server: https://&lt;master-ip&gt;:6443
  name: mykubecontexts:
- context:
    cluster: mykube
    user: mykube-adm
  name: mykube
current-context: mykube
kind: Config
preferences: {}
users:
- name: mykube-adm
  user:
    client-key: path/to/private/keyfile
</code></pre>

<p>Unfortunately, above <code>kubectl</code> config file is incorrect. It seems an error appeared due to manual formatting or something else. </p>

<p>New line is missing in this part (<code>name: mykubecontexts:</code>):</p>

<pre><code>clusters:
- cluster:
    server: https://&lt;master-ip&gt;:6443
  name: mykubecontexts:
- context:
    cluster: mykube
    user: mykube-adm
  name: mykube
</code></pre>

<p>Correct one is:</p>

<pre><code>clusters:
- cluster:
    server: https://&lt;master-ip&gt;:6443
  name: mykube
contexts:
- context:
    cluster: mykube
    user: mykube-adm
  name: mykube
</code></pre>

<p>That's why cluster's name is <code>mykubecontexts:</code>:</p>

<pre><code>clusters:
- cluster:
    server: https://&lt;master-ip&gt;:6443
  name: mykubecontexts:
</code></pre>

<p>and that's why there is no context in it, because <code>contexts:</code> is not defined.</p>

<p><code>kubectl</code> cannot find context <code>mykube</code> and switches to default one where <code>server=localhost:8080</code> is by default.</p>

<p><code>kubectl</code> config is located in <code>${HOME}/.kube/config</code> file by default if <code>--kubeconfig</code> flag or <code>$KUBECONFIG</code> environment variable are not set.</p>

<p>Please correct it to the following one:</p>

<pre><code>apiVersion: v1
clusters:
- cluster:
    server: https://&lt;master-ip&gt;:6443
  name: mykube
contexts:
- context:
    cluster: mykube
    user: mykube-adm
  name: mykube
current-context: mykube
kind: Config
preferences: {}
users:
- name: mykube-adm
  user:
    client-key: path/to/private/keyfile
</code></pre>
