Kubernetes Python客户端错误创建\u命名空间\u绑定:(409)原因:冲突
我使用的是k8v1.7和v2.0。我的自定义调度程序检测到一个挂起的pod并成功地对其进行调度。但是,在将pod分配给节点后,它会抱怨pod已经分配给节点,尽管它只是由调度器本身分配的。这有什么值得担心的吗?或者我该如何解决这个问题 错误消息Kubernetes Python客户端错误创建\u命名空间\u绑定:(409)原因:冲突,python,api,kubernetes,http-headers,scheduling,Python,Api,Kubernetes,Http Headers,Scheduling,我使用的是k8v1.7和v2.0。我的自定义调度程序检测到一个挂起的pod并成功地对其进行调度。但是,在将pod分配给节点后,它会抱怨pod已经分配给节点,尽管它只是由调度器本身分配的。这有什么值得担心的吗?或者我该如何解决这个问题 错误消息 create_namespaced_binding: (409) Reason: Conflict HTTP response headers: HTTPHeaderDict({'Date': 'Tue, 19 Jun 2018 16:14:57 GMT'
create_namespaced_binding: (409)
Reason: Conflict
HTTP response headers: HTTPHeaderDict({'Date': 'Tue, 19 Jun 2018 16:14:57 GMT', 'Content-Length': '289', 'Content-Type': 'application/json'})
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Operation cannot be fulfilled on pods/binding \"ps0-16-r935x\": pod ps0-16-r935x is already assigned to node \"blipp65\"","reason":"Conflict","details":{"name":"ps0-16-r935x","kind":"pods/binding"},"code":409}
调度程序.py
from kubernetes import client, config, watch
from kubernetes.client.rest import ApiException
config.load_kube_config()
v1 = client.CoreV1Api()
scheduler_name = 'my-custom-scheduler-v1'
def nodes_available():
ready_nodes = []
for n in v1.list_node().items:
for status in n.status.conditions:
if status.status == 'True' and status.type == 'Ready':
ready_nodes.append(n.metadata.name)
return ready_nodes
def scheduler(name, node, namespace='default'):
body = client.V1Binding()
target = client.V1ObjectReference()
target.kind = 'Node'
target.apiVersion = 'v1'
target.name = node
meta = client.V1ObjectMeta()
meta.name = name
body.target = target
body.metadata = meta
return v1.create_namespaced_binding_binding(name, namespace, body)
def main():
w = watch.Watch()
for event in w.stream(v1.list_namespaced_pod, 'default'):
if event['object'].status.phase == 'Pending' and event['object'].spec.scheduler_name == scheduler_name:
print "Pending Found"
try:
res = scheduler(event['object'].metadata.name,random.choice(nodes_available()))
print "success"
except Exception as a:
print ("Exception when calling CoreV1Api->create_namespaced_binding: %s\n" % a)
POD YML文件
apiVersion: v1
kind: Pod
metadata:
name: shoeb-pod
spec:
schedulerName: my-custom-scheduler-v1
containers:
- name: redis
image: redis
于2019-06-03更新
我刚刚添加了更新的main方法(根据@VAS的回答,谢谢),以查找尚未计划的正确的
PENDING
pod。请看我的答案 创建pod时,调度程序将获得三个“挂起”事件:
'node_name':无,'status':{'conditions':无,…}
)'node\u name':'some\u node\u name','status':{'conditions':[…,'status':True,'type':'PodScheduled'],…}
)“节点名称”:“minikube”,“状态”:{“条件”:[…,“状态”:True,“类型”:“初始化”],…,“状态”:False,“类型”:“就绪”]}
)在您的情况下,调度程序会像第一个事件一样威胁所有三个事件,并尝试一次又一次地调度pod。这就是为什么您会看到“
pod xxx已分配给节点yyy
”错误。以下是更新的主要方法(根据@VAS的回答,谢谢)以查找尚未计划的正确的挂起的
pod
def main():
w = watch.Watch()
for event in w.stream(v1.list_namespaced_pod, 'default'): # default == namespace name
# All pending pods have 3 states (not scheduled, scheduled, initialized but not ready yet)
# We look for NOT SCHEDULED pod and conditions==None
if event['object'].status.phase == 'Pending' and event['object'].status.conditions == None and event['object'].spec.scheduler_name == CUSTOM_SCHEDULER_NAME:
print "Pending and Not Scheduled POD Found "+event['object'].metadata.name
try:
res = scheduler(event['object'].metadata.name,random.choice(nodes_available())) # nodes_available() returns all available nodes
print "success"
except Exception as a:
print ("Exception when calling CoreV1Api->create_namespaced_binding: %s\n" % a)
您可以附加一个Pod YAML文件作为调度对象吗?@VAS刚刚添加到问题的末尾,谢谢!谢谢。根据您的信息,我的调度程序现在能够跟踪所有3个案例。我将很快发布更新的代码。