Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to connect to kubernetes python api - .kube/config file not found

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.

like image 235
helloworld Avatar asked Jun 25 '18 22:06

helloworld


People also ask

What is the kube config file?

A kubeconfig file is a file used to configure access to Kubernetes when used in conjunction with the kubectl commandline tool (or other clients).

How do I get Kubeconfig file?

A Kubeconfig is a YAML file with all the Kubernetes cluster details, certificate, and secret token to authenticate the cluster. You might get this config file directly from the cluster administrator or from a cloud platform if you are using managed Kubernetes cluster.

How do I reset my kube config?

You can use the same command you used to set the server to change it to the correct server. If you want to wipe the slate clean in terms of client config, you should remove the kubeconfig file(s). In my experience with the gcloud setup, this is just ~/. kube/config .


1 Answers

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.

like image 90
mdaniel Avatar answered Oct 22 '22 05:10

mdaniel