Python调用KubernetesAPI

本文详细介绍了如何使用Python的client库与Kubernetes API进行交互,包括创建、查询、更新和删除Pod、Service等核心资源,帮助开发者高效管理Kubernetes集群。
摘要由CSDN通过智能技术生成
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值