学习利用python写 k8s 第一天笔记
import random
from kubernetes import client, config, watch
try:
config.load_kube_config('kube_config')
k8s = client.CoreV1Api()
w = watch.Watch()
for event in w.stream(k8s.list_namespaced_pod, namespace='default', timeout_seconds=6000):
o = event['object']
# print(str(event))
# print(o.spec.node_name,o.status.phase)
# print(k8s.read_namespaced_pod(o.metadata.name, 'default'))
# print(o.status.phase, o.spec.node_name, type(o.status.phase), type(o.spec.node_name))
# print(o.status.phase == "Pending" or o.status.phase == "Running", o.spec.node_name is None)
if (o.status.phase == "Pending" or o.status.phase == "Running") and o.spec.node_name is None:
# print(o.status.phase, o.spec.node_name, type(o.status.phase), type(o.spec.node_name))
# print(o.status.phase == "Pending" or o.status.phase == "Running", o.spec.node_name is None)
# print(o.metadata.name)
nodes = []
for i in k8s.list_node().items:
for n in i.status.conditions:
if n.status == 'True' and n.type == 'Ready':
nodes.append(i.metadata.name)
nodes = random.choice(nodes)
# print(nodes)
t = client.V1Binding(
metadata=client.V1ObjectReference(name=o.metadata.name),
target=client.V1ObjectReference(kind='Node', api_version='v1', name=nodes)
)
try:
resp = k8s.create_namespaced_binding('default', body=t)
print(str(resp))
except:
pass
print(k8s.read_namespaced_pod(o.metadata.name,'default'))
except Exception as e:
print('err')
print(e)
print('end')
print('master')
- 点赞
- 收藏
- 关注作者
评论(0)