accessing kubernetes python api through a pod

2024/10/2 1:31:44

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_configcert_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_setself._load_config()File "/Users/myname/Library/Python/2.7/lib/python/site-packages/kubernetes/config/incluster_config.py", line 51, in _load_configraise 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.

Answer

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).

https://en.xdnf.cn/q/70902.html

Related Q&A

Understanding DictVectorizer in scikit-learn?

Im exploring the different feature extraction classes that scikit-learn provides. Reading the documentation I did not understand very well what DictVectorizer can be used for? Other questions come to …

Parsing RSS with Elementtree in Python

How do you search for namespace-specific tags in XML using Elementtree in Python?I have an XML/RSS document like:<?xml version="1.0" encoding="UTF-8"?> <rss version=&quo…

String module object has no attribute join

So, I want to create a user text input box in Pygame, and I was told to look at a class module called inputbox. So I downloaded inputbox.py and imported into my main game file. I then ran a function in…

TypeError: the JSON object must be str, not Response with Python 3.4

Im getting this error and I cant figure out what the problem is:Traceback (most recent call last):File "C:/Python34/Scripts/ddg.py", line 8, in <module>data = json.loads(r)File "C:…

Redirect while passing message in django

Im trying to run a redirect after I check to see if the user_settings exist for a user (if they dont exist - the user is taken to the form to input and save them).I want to redirect the user to the app…

Django sorting by date(day)

I want to sort models by day first and then by score, meaning Id like to see the the highest scoring Articles in each day. class Article(models.Model):date_modified = models.DateTimeField(blank=True, n…

ImportError: No module named pynotify. While the module is installed

So this error keeps coming back.Everytime I try to tun the script it returns saying:Traceback (most recent call last):File "cli.py", line 11, in <module>import pynotify ImportError: No …

business logic in Django

Id like to know where to put code that doesnt belong to a view, I mean, the logic.Ive been reading a few similar posts, but couldnt arrive to a conclusion. What I could understand is:A View is like a …

Faster alternatives to Pandas pivot_table

Im using Pandas pivot_table function on a large dataset (10 million rows, 6 columns). As execution time is paramount, I try to speed up the process. Currently it takes around 8 secs to process the whol…

How can I temporarily redirect the output of logging in Python?

Theres already a question that answers how to do this regarding sys.stdout and sys.stderr here: https://stackoverflow.com/a/14197079/198348 But that doesnt work everywhere. The logging module seems to …