Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid kube-config file. No configuration found when i use kubernetes client python in pod

I am trying to get kubernetes api server from a pod.

Here is my pod configuration

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: test
spec:
  schedule: "*/5 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: test
            image: test:v5
            env:
            imagePullPolicy: IfNotPresent
            command: ['python3']
            args: ['test.py']
          restartPolicy: OnFailure

And here is my kubernetes-client python code inside test.py

from kubernetes import client, config

# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config()

v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
    print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))

But i am getting this error:

kubernetes.config.config_exception.ConfigException: Invalid kube-config file. No configuration found.
like image 317
karlos Avatar asked May 20 '21 21:05

karlos


People also ask

How do I set the kube config file?

By default, kubectl looks for a file named config in the $HOME/. kube directory. You can specify other kubeconfig files by setting the KUBECONFIG environment variable or by setting the --kubeconfig flag.

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

When operating in-cluster you want load_incluster_config() instead.

like image 108
coderanger Avatar answered Nov 03 '22 13:11

coderanger