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.
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.
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 .
When operating in-cluster you want load_incluster_config()
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With