accessing kubernetes python api through a pod

6/29/2018

so I need to connect to the python kubernetes client through a pod. I've been trying to use config.load_incluster_config(), basically following the example from here. However it's throwing these errors.

  File "/Users/myname/Library/Python/2.7/lib/python/site-packages/kubernetes/config/incluster_config.py", line 93, in   load_incluster_config
  cert_filename=SERVICE_CERT_FILENAME).load_and_set()
  File "/Users/myname/Library/Python/2.7/lib/python/site- packages/kubernetes/config/incluster_config.py", line 45, in load_and_set
  self._load_config()
  File "/Users/myname/Library/Python/2.7/lib/python/site-packages/kubernetes/config/incluster_config.py", line 51, in _load_config
  raise ConfigException("Service host/port is not set.")

I'm using Python 2.7 and Minikube Any hints or suggestions would be really appreciated. thank you.

-- helloworld
kubernetes
kubernetes-python-client
minikube
python

1 Answer

6/29/2018

so I need to connect to that pod somehow through the python api

I am pretty sure you misunderstood my answer, and/or I misunderstood your question. One should only use load_incluster_config when ... in-cluster ... otherwise it will attempt to use /var/run/secrets/kubernetes.io/etcetc and not find them (above and beyond the missing env-var in the actual error you cited above). However, if you had guarded the load_incluster_config() with the if os.getenv('KUBERNETES_SERVICE_HOST'): as suggested, then it wouldn't run that code and this question here wouldn't be a problem.

If you have built a docker image, but did not deploy it into kubernetes, then that wasn't clear.


If you just want to use the python api to access the cluster, but not from within the cluster, config.load_kube_config() is in fact the correct method call, but you will absolutely need to provide a working kubeconfig, whether at /root/.kube/config or at another place specified by the env-var KUBECONFIG (I mean, usually; I haven't specifically looked into the python library to see if that env-var is honored).

-- mdaniel
Source: StackOverflow