<p>In a container inside a pod, how can I run a command using kubectl? For example, if i need to do something like this inside a container: </p>

<blockquote>
  <p>kubectl get pods</p>
</blockquote>

<p>I have tried this : In my dockerfile, I have these commands : </p>

<pre><code>RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
RUN chmod +x ./kubectl
RUN sudo mv ./kubectl /usr/local/bin/kubectl
</code></pre>

<blockquote>
  <p>EDIT : I was trying the OSX file, I have corrected it to the linux binary file. (corrected by @svenwltr</p>
</blockquote>

<p>While creating the docker file, this is successful, but when I run the kubectl get pods inside a container, </p>

<pre><code>kubectl get pods
</code></pre>

<p>I get this error :</p>

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

<p>When I was deploying locally, I was encountering this error if my docker-machine was not running, but inside a container how can a docker-machine be running? </p>

<p>Locally, I get around this error by running the following commands:
(dev is the name of the docker-machine)</p>

<pre><code>docker-machine env dev
eval $(docker-machine env dev)
</code></pre>

<p>Can someone please tell me what is it that I need to do?</p>

Dreams
<p>I would use kubernetes api, you just need to install curl, instead of <code>kubectl</code> and the rest is restful.</p>

<pre><code>curl http://localhost:8080/api/v1/namespaces/default/pods
</code></pre>

<p>Im running above command on one of my apiservers. Change the <strong>localhost</strong> to <strong>apiserver ip address/dns name</strong>.</p>

<p>Depending on your configuration you may need to use ssl or provide client certificate.</p>

<p>In order to find api endpoints, you can use <code>--v=8</code> with <code>kubectl</code>.</p>

<p>example:</p>

<pre><code>kubectl get pods --v=8
</code></pre>

<p>Resources:</p>

<p>Kubernetes <a href="https://kubernetes.io/docs/api-reference/v1.5/" rel="noreferrer">API documentation</a></p>

<p><strong>Update for RBAC:</strong></p>

<p>I assume you already configured rbac, created a service account for your pod and run using it. This service account should have list permissions on pods in required namespace. In order to do that, you need to create a role and role binding for that service account.</p>

<p>Every container in a cluster is populated with a token that can be used for authenticating to the API server. To verify, Inside the container run:</p>

<pre><code>cat /var/run/secrets/kubernetes.io/serviceaccount/token
</code></pre>

<p>To make request to apiserver, inside the container run:</p>

<pre><code>curl -ik \
     -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \
     https://kubernetes.default.svc.cluster.local/api/v1/namespaces/default/pods
</code></pre>
