import os
import time
from kubernetes import client, config
# 指定配置文件路径
config.load_kube_config(config_file='config')
# 创建 Kubernetes API 客户端
v1 = client.AppsV1Api()
v2 = client.CoreV1Api()
# 指定命名空间
namespace = 'default'
# 指定 Deployments 名称列表
deployment_names = ['nginx', 'test-pods', 'myadmin']
# 重启 Deployment
print('正在重启 Deployment...')
for deploy in deployment_names:
print('重启的Deployment-------------')
print(deploy)
for deployment_name in deployment_names:
v1.patch_namespaced_deployment(deployment_name, namespace, body={'spec': {'replicas': 0}})
time.sleep(3)
# 获取 Pod 列表
print('获取 Pod 列表...')
for namespace in ["default"]:
ret = v2.list_namespaced_pod(namespace=namespace, watch=False)
for i in ret.items:
print("%s \t%s \t%s \t%s \t%s" %
(i.status.host_ip, i.status.pod_ip, i.status.phase, i.metadata.namespace, i.metadata.name))
获取状态
from kubernetes import client, config
def main():
config.load_kube_config(config_file='config')
v1 = client.CoreV1Api()
namespaces = ['openim']
for namespace in namespaces:
ret = v1.list_namespaced_pod(namespace=namespace, watch=False)
for i in ret.items:
print("%s \t%s \t%s \t%s \t%s" %
( i.status.host_ip,
i.status.pod_ip, i.status.phase, i.metadata.namespace, i.metadata.name))
if __name__ == '__main__':
main()