Python之K8S API接口 python代码编写

转载:
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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值