我刚刚复制了你的场景,它在 python 2.7 上完美运行
这是代码:
from kubernetes import client
configuration = client.Configuration()
configuration.host = 'https://10.132.0.25:6443'
configuration.ssl_ca_cert = './ca.crt'
configuration.cert_file = './client.crt'
configuration.key_file = './client.key'
configuration.verify_ssl = True
v1 = client.CoreV1Api(client.ApiClient(configuration))
ret = v1.list_pod_for_all_namespaces()
for i in ret.items:
print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
...结果:
$ python test.py
192.168.171.66 kube-system calico-kube-controllers-65b8787765-h7qv7
10.132.0.25 kube-system calico-node-t4r4v
10.132.0.26 kube-system calico-node-zbtjm
192.168.171.65 kube-system coredns-5c98db65d4-rm2qh
192.168.171.67 kube-system coredns-5c98db65d4-sr67s
10.132.0.25 kube-system etcd-master
10.132.0.25 kube-system kube-apiserver-master
10.132.0.25 kube-system kube-controller-manager-master
10.132.0.26 kube-system kube-proxy-759gn
10.132.0.25 kube-system kube-proxy-v5hvc
10.132.0.25 kube-system kube-scheduler-master
我用 kubeadm 创建了集群。它正在运行 1.15.3
$ kubectl get no
NAME STATUS ROLES AGE VERSION
master Ready master 41m v1.15.3
worker Ready worker 41m v1.15.3
最后,客户端库:
$ pip freeze | grep -E 'kubernetes|requests'
kubernetes==10.0.1
requests==2.22.0
要排除更多故障,需要更多信息;特别是关于版本。但是你的代码工作正常。
编辑:它也适用于 python3:
$ python3 test.py
192.168.171.66 kube-system calico-kube-controllers-65b8787765-h7qv7
10.132.0.25 kube-system calico-node-t4r4v
10.132.0.26 kube-system calico-node-zbtjm
192.168.171.65 kube-system coredns-5c98db65d4-rm2qh
192.168.171.67 kube-system coredns-5c98db65d4-sr67s
10.132.0.25 kube-system etcd-master
10.132.0.25 kube-system kube-apiserver-master
10.132.0.25 kube-system kube-controller-manager-master
10.132.0.26 kube-system kube-proxy-759gn
10.132.0.25 kube-system kube-proxy-v5hvc
10.132.0.25 kube-system kube-scheduler-master