<p>I am getting the following error when trying to run <code>kubectl</code> locally.</p>

<p><code>error: couldn't read version from server: Get http://localhost:8080/api: dial tcp 127.0.0.1:8080: connection refused</code></p>

<p>I know this relates to the kubectl config but I'm at a loss in how to resolve it.  2 days ago, I was experimenting with GKE and did set the config to point to GCE.  I tried deleting this config file and then getting <a href="https://github.com/pires/kubernetes-vagrant-coreos-cluster">Vagrant with CoreOS locally</a>.  This <code>vagrant up</code> throws a similar error complaining about not being able to connect.  </p>

<p>What is the appropriate way to instrument <code>kubectl</code> so it can connect to the API and return information?</p>

<p><strong>tl;dr</strong> <code>gcloud container get-credentials --cluster=CLUSTER_ID --zone=YOURZONE</code></p>

<hr>

<p>So a little background: the kubectl tool is developed by google but isn't actually integrated into google cloud directly, the google cloud just helps you get a compatible version with it when you tell it to install the component.</p>

<p>If you're getting the <code>Get http://localhost:8080/api: dial tcp 127.0.0.1:8080: connection refused</code> it is likely due to the <code>kubectl</code> tool not being configured at all or misconfigured. What I believe it's trying to do is assuming you have kubernetes somehow setup locally only, which you don't in this case since it's all on the google cloud (hence the cryptic error).</p>

<p>You can verify your kubectl is misconfigured by running <code>kubectl config view</code>. If it's correctly configured you should see things like a few entries in cluster, with ip addresses, and in users you should see a user for each project, etc. If you see nothing of the sort (ie. empty clusters, and empty users) then you are misconfigured; you will also encounter cryptic issues if you dont see entries for the specific cluster you are trying to work on.</p>

<p>Annoyingly a lot of <code>gcloud</code> commands will silently auto-configure it for you, so if you follow something like a hello wordpress tutorial it will look like you dont have to do this and that somehow kubectl communicates with gcloud, but nothing of the sort happens. It's also very easy to lose that configuration.</p>

<p>To tell gcloud to give you the kubectl config run the following:</p>

<pre><code>gcloud container get-credentials --cluster=CLUSTER_ID --zone=YOURZONE
</code></pre>

<p>For cluster id run <code>gcloud container clusters list</code></p>

<p>Zone is "europe-west1-d" or whatever you've chosen.</p>
