<p>I'm having trouble connecting to the kubernetes python client even though I'm following the examples <a href="https://github.com/kubernetes-client/python/blob/master/examples/example1.py" rel="noreferrer">here</a> in the api. </p>

<p>Basically this line can't connect to the kubernetes client: </p>

<pre><code>config.load_kube_config()
</code></pre>

<p><strong>What I'm doing:</strong> </p>

<p>I have a Dockerfile file like this that I'm building my image with. This is just a simple python/flask app. </p>

<pre><code>FROM python:2

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

COPY requirements.txt /usr/src/app/

RUN pip install --no-cache-dir -r requirements.txt

COPY . /usr/src/app

EXPOSE 5000

CMD [ "python", "./app.py" ]
</code></pre>

<p>This is my requirements.txt:</p>

<pre><code>Flask==1.0.2
gunicorn==19.8.1
kubernetes==6.0.0
requests # Apache-2.0
</code></pre>

<p>After building the Dockerfile it outputs: </p>

<pre><code>    Successfully built a2590bae9fd9
    Successfully tagged testapp:latest
</code></pre>

<p>but when I do <code>docker run a2590bae9fd9</code> I receive an error:</p>

<pre><code>Traceback (most recent call last):
  File "./app.py", line 10, in &lt;module&gt;
    config.load_kube_config()
  File "/usr/local/lib/python2.7/site-     packages/kubernetes/config/kube_config.py", line 470, in load_kube_config
    config_persister=config_persister)
   File "/usr/local/lib/python2.7/site-   packages/kubernetes/config/kube_config.py", line 427, in     _get_kube_config_loader_for_yaml_file
    with open(filename) as f:
 IOError: [Errno 2] No such file or directory: '/root/.kube/config'
</code></pre>

<p>I thought it might've been my python directory but I checked and its running in /usr/local/bin/python. </p>

<p>I'm really stumped - any suggestions/tips? thank you. </p>

<p>You don't want <code>config.load_kube_config()</code>, you want <a href="https://github.com/kubernetes-client/python/blob/6.0.0/examples/in_cluster_config.py#L21" rel="noreferrer"><code>config.load_incluster_config()</code></a></p>

<p>If you need to distinguish between your setup and when it's running in a <code>Pod</code>, one mechanism is <code>if os.getenv('KUBERNETES_SERVICE_HOST'): config.load_incluster_config()</code> since that for sure will be in the environment while in a <code>Pod</code>, and is unlikely to be in your local environment.</p>
