转载:
https://blog.csdn.net/anqixiang/article/details/113763193
https://www.cnblogs.com/zhangb8042/p/11444756.html
https://www.cnblogs.com/wuchangblog/p/14087926.html
推荐阅读:
https://blog.csdn.net/wmj2004/article/details/103527860
源码github地址
https://github.com/kubernetes-client/python
安装
pip install kubernetes
示例
from kubernetes import client, config
config.kube_config.load_kube_config(config_file="./kubeconfig.yaml")
#获取API的CoreV1Api版本对象
v1 = client.CoreV1Api()
#列出 namespaces
# for ns in v1.list_namespace().items:
# if ns.metadata.name == 'it':
# print(ns.metadata.name)
#列出所有的services
# ret = v1.list_service_for_all_namespaces(watch=False)
ret = v1.list_namespaced_service("it")
for i in ret.items:
if i.metadata.name == 'tornado-webssh-svc':
print("%s \t%s \t%s \t%s \t%s \n" % (i.kind, i.metadata.namespace, i.metadata.name, i.spec.cluster_ip, i.spec.ports ))
# #列出所有的pod
# ret = v1.list_namespaced_pod("it")
# # 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))
# #列出所有deploy
# ret = v1.list_namespaced_deployment("it")
# ret = v1.list_deployments_for_all_namespaces(watch=False)
# for i in ret.items:
# print("%s\t%s\t%s" % (i.metadata.namespace, i.metadata.name))
##列出其他资源和以上类似,不懂可以查看(kubectl api-resources)
查看pod日志 api
https://blog.csdn.net/weixin_35541322/article/details/111960882
log_content=corev1api.read_namespaced_pod_log('redis-master-fsx46','default',pretty=True,tail_lines=200,container='redis-server')
这行代码就完成了之前kubectl logs redis-master-fsx46的同样工作,只不过这里需要额外指定了一些参数:
default命名空间
pretty美化输出
tail_lines=200输出最近200行
container=’redis-server’只获取POD内该容器的输出
————————————————
版权声明:本文为CSDN博主「道一云办公」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_35541322/article/details/111960882