'Kubernetes python client

Looking to use the Kubernetes client for Python. After following these directions, I completed first steps and used the sample script.

git clone --recursive https://github.com/kubernetes-client/python.git
cd python
python setup.py install

I use the sample code to test on a minikube cluster and I receive this error. I have not seen a path forward but curious if anyone has suggestions on how to move past it.

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

Once I run the script I receive a google.auth error. I try to add in the package to the script and same result.

import google.auth
ModuleNotFoundError: No module named 'google.auth'


Solution 1:[1]

It seems that your current Kubectl context is a cluster running in the GKE (Google Kubernetes Engine), that's why you it tries to authenticate with the google.auth module (which is missing in your case).

If you want to use the Minikube cluster, check that your current context is pointing to this cluster. You can do it with the following command.

$ kubectl config current-context

Solution 2:[2]

Follow these steps and you will not have any issues with Kubernetes python client:

  1. Install minikube on Linux or in wsl2 on Windows.
$ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
$ sudo install minikube-linux-amd64 /usr/local/bin/minikube
  1. Start minikube
$ minikube start
  1. Verify the current context
$ kubectl config current-context
minikube
  1. Install Kubernetes Python Client either
$ pip3 install kubernetes

or

$ git clone --recursive https://github.com/kubernetes-client/python.git
$ cd python
$ sudo python3 setup.py install

I tested both variants. Both work fine.

  1. Run your code. The output is:
Listing pods with their IPs:
172.17.0.2      kube-system     coredns-78fcd69978-fkhfd
192.168.49.2    kube-system     etcd-minikube
192.168.49.2    kube-system     kube-apiserver-minikube
192.168.49.2    kube-system     kube-controller-manager-minikube
192.168.49.2    kube-system     kube-proxy-wltkp
192.168.49.2    kube-system     kube-scheduler-minikube
192.168.49.2    kube-system     storage-provisioner

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rafał Leszko
Solution 2 mozello