简介
本文总结了Kubernetes的Python Client的安装及使用示例,以帮助读者能够快速了解Pyhont Client的接口使用。
安装
如果是安装的Anaconda的环境,可以使用下列命令来进行安装Client库:
conda install -c conda-forge kubernetes
使用PyPi,直接运行下列命令:
pip install kubernetes
使用config配置文件
如果在运行环境中,把config配置文件拷贝到~/.kube/config下,可以通过加载config配置的方式来调用接口进行配置查询:
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))
但这种方式有安全问题,将config文件拷贝到本地后,用户可以使用kubectl进行对整个集群进行管理,误操作会给集群造成一些问