14

I'm having trouble connecting to the kubernetes python client even though I'm following the examples here in the api.

Basically this line can't connect to the kubernetes client:

config.load_kube_config()

What I'm doing:

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

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" ]

This is my requirements.txt:

Flask==1.0.2
gunicorn==19.8.1
kubernetes==6.0.0
requests # Apache-2.0

After building the Dockerfile it outputs:

    Successfully built a2590bae9fd9
    Successfully tagged testapp:latest

but when I do docker run a2590bae9fd9 I receive an error:

Traceback (most recent call last):
  File "./app.py", line 10, in <module>
    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'

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

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

helloworld
  • 399
  • 3
  • 9
  • 21
  • 1
    It's trying to load `'/root/.kube/config'` and that file doesn't exist in your container. Were you expecting it to exist? Where are you expecting it to load your config from? – johnharris85 Jun 25 '18 at 23:15
  • I have the ~/.kube/config file locally so I guess I wrongly assumed it could use that when kubernetes installs through requirements.txt. how can I make it read the config file I have locally on my computer, is that possible through the Dockerfile? – helloworld Jun 26 '18 at 00:58

1 Answers1

21

You don't want config.load_kube_config(), you want config.load_incluster_config()

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

mdaniel
  • 31,240
  • 5
  • 55
  • 58
  • 1
    I just surround load_kube_config in try/catch and then use the load_incluster_config but your way is better – in need of help Jun 26 '18 at 06:06
  • 1
    that worked! I used the incluster one before but didn't have the if statement so I guess that was what I was missing. thank you! – helloworld Jun 26 '18 at 13:05
  • 2
    I've actually been getting "kubernetes.config.config_exception.ConfigException: Service host/port is not set" error now on this config.load_incluster_config() line. do you have any suggestions on what I can do to fix it? – helloworld Jun 29 '18 at 00:49
  • 1
    under what circumstances do you get that error, since my suggestion was to use 50% of what that exact error is complaining about: `$KUBERNETES_SERVICE_HOST` (with the other 50% being `_PORT') – mdaniel Jun 29 '18 at 06:47