from kubernetes import client,config
config.kube_config.load_kube_config(config_file="C:\\Users\\WEI\\Desktop\\kubernetes-python\\config")
#获取API的CoreV1Api版本对象
v1 = client.CoreV1Api()
#列出namespace
for ns in v1.list_namespace().items:
print(ns.metadata.name)
#列出所有的services
ret = v1.list_service_for_all_namespaces(watch=False)
for i in ret.items:
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_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))
#根据yaml创建Deployment资源
from os import path
import yaml
from kubernetes import client,config
config.kube_config.load_kube_config(config_file="C:\\Users\\WEI\\Desktop\\kubernetes-python\\config")
def main():
with open(path.join(path.dirname(__file__),"C:\\Users
Python调用KubernetesAPI
最新推荐文章于 2023-12-10 19:02:29 发布
本文详细介绍了如何使用Python的client库与Kubernetes API进行交互,包括创建、查询、更新和删除Pod、Service等核心资源,帮助开发者高效管理Kubernetes集群。
摘要由CSDN通过智能技术生成