列表
core_api = client.CoreV1Api() # 管理核心资源(Pod, Service, ConfigMap 等)
apps_api = client.AppsV1Api() # 管理应用资源(Deployment, StatefulSet, DaemonSet 等)
batch_api = client.BatchV1Api() # 管理批处理任务资源(Job, CronJob)
rbac_api = client.RbacAuthorizationV1Api() # 管理角色和权限绑定资源
networking_api = client.NetworkingV1Api() # 管理网络资源(Ingress, NetworkPolicy)
custom_objects_api = client.CustomObjectsApi() # 管理自定义资源(CRD)
查看所有的命名空间
from kubernetes import client,config
config.kube_config.load_kube_config(config_file='C:/Users/4407/Desktop/config')
v1 = client.CoreV1Api()
for namespace in v1.list_namespace().items:
print(namespace.metadata.name)
查看pod的名字
from kubernetes import client,config
config.kube_config.load_kube_config('C:/Users/4407/Desktop/config')
v1 = client.CoreV1Api()
for pod in v1.list_pod_for_all_namespaces().items:
print(pod.metadata.name)
基本操作
# 对于查看pod 分为 查看指定名称空间里的pod 查看指定标签的pod 查看pod的状态
import kubernetes.config
from kubernetes import client, config
kubernetes.config.kube_config.load_kube_config('C:\\Users\\4407\\Desktop\\config')
core_v1 = kubernetes.client.CoreV1Api()
ns = 'ingress-nginx'
a = core_v1.list_namespaced_service(namespace=ns)
for i in a.items:
print(i)
import kubernetes.config
from kubernetes import client, config
# 加载 Kubernetes 配置
kubernetes.config.kube_config.load_kube_config('C:\\Users\\4407\\Desktop\\config')
# 实例化 CoreV1Api 类
core_v1 = kubernetes.client.CoreV1Api()
# 指定命名空间
namespace = 'ingress-nginx'
# 获取指定命名空间下的所有服务
service_list = core_v1.list_namespaced_service(namespace=namespace)
# 遍历服务列表并打印每个服务的名字
for service in service_list.items:
print(service.metadata.name)
# 遍历服务列表并打印每个服务的名字
for service in service_list.items:
print('hhhhh' + service.metadata.name)
# 查看pod的名字
from kubernetes import client, config
config.kube_config.load_kube_config('C:\\Users\\4407\\Desktop\\config')
ret = client.CoreV1Api()
a = ret.list_namespaced_pod('dev')
for i in ret.list_namespaced_pod('dev').items:
print(i.metadata.name)
# 不可以写成 for i in ret.list_namespaced_pod.items('dev'):
创建pod
from kubernetes import client,config
config.load_kube_config('C:\\Users\\4407\\Desktop\\config')
pod = client.V1Pod(metadata=client.V1ObjectMeta(name="my-test01"),
spec=client.V1PodSpec(containers=[
client.V1Container(
name="my-test",
ports=[client.V1ContainerPort(name="nginx", container_port=8088, host_port=8088)],
image="centos"
)
]
)
)
ret = client.CoreV1Api()
try:
a = ret.create_namespaced_pod("dev", pod)
print("pod create sussess , pod status is '%s' " % str(a.status))
except Exception as e:
print("pod create error and status is '%s'" %e)
pod = client.V1Pod(
metadata=client.V1ObjectMeta(name="my-pod"),
spec=client.V1PodSpec(
containers=[
client.V1Container(
name="my-container",
image="nginx",
ports=[client.V1ContainerPort(container_port=80)]
)
]
)
)
ret = client.CoreV1Api()
ns = "dev"
try:
api_response = ret.create_namespaced_pod(ns, pod)
print("Pod created. Status='%s'" % str(api_response.status))
except Exception as e:
print("Error: %s" % e)
# 创建deployment
from kubernetes import config,client
config.kube_config.load_kube_config(r"C:\Users\4407\Desktop\yun\config")
deploy = client.V1Deployment(
metadata=client.V1ObjectMeta(
name="my-pod",
namespace="default"),
spec=client.V1DeploymentSpec(
replicas=1,
selector=client.V1LabelSelector(
match_labels={
"app": "nginx"
}
),
template=client.V1PodTemplateSpec(
metadata=client.V1ObjectMeta(labels={"app" : "nginx"}),
spec=client.V1PodSpec(
containers=[
client.V1Container(
name="nginx",
image="nginx",
ports=[
client.V1ContainerPort(container_port=8089)
]
)
]
)
)
)
)
deplo_get = client.AppsV1Api()
try:
resource = deplo_get.create_namespaced_deployment(namespace="default",body=deploy)
print("deployment create sussessfully. status: '%s'" % resource.status)
except Exception as e:
print("deployment create error. status is '%s'" %e)
# 创建job
import yaml
from kubernetes import config,client
config.kube_config.load_kube_config(r"C:\Users\4407\Desktop\yun\config")
def creat_job():
job = client.BatchV1Api()
with open('f.yaml','r') as f:
job_yaml = yaml.safe_load(f)
job_create = job.create_namespaced_job(namespace="default", body=job_yaml)
return job_create
try:
print("job create sussessful. status is '%s'" %str(creat_job().status))
except Exception as e:
print("job create error. status is '%s" %e)