【问题标题】:Kubernetes python client equivalent of "kubectl wait --for " command等效于“kubectl wait --for”命令的 Kubernetes python 客户端
【发布时间】:2021-05-02 10:25:25
【问题描述】:

我正在使用 kubernetes-client/python 并想编写一个方法来阻止控制,直到一组 Pod 处于就绪状态(运行状态)。我发现 kubernetes 支持 wait --for 命令通过命令执行相同的操作。有人可以帮助我如何使用 kubernetes python 客户端实现相同的功能。

确切地说,我最感兴趣的是等价的-

kubectl wait --for condition=Ready pod -l 'app in (kafka,elasticsearch)'

【问题讨论】:

    标签: python kubernetes kubernetes-python-client


    【解决方案1】:

    您可以使用客户端库中提供的watch 功能。

    from kubernetes import client, config, watch
    
    config.load_kube_config()
    w = watch.Watch()
    core_v1 = client.CoreV1Api()
    for event in w.stream(func=core_v1.list_namespaced_pod,
                              namespace=namespace,
                              label_selector=label,
                              timeout_seconds=60):
        if event["object"].status.phase == "Running":
            w.stop()
            end_time = time.time()
            logger.info("%s started in %0.2f sec", full_name, end_time-start_time)
            return
        # event.type: ADDED, MODIFIED, DELETED
        if event["type"] == "DELETED":
            # Pod was deleted while we were waiting for it to start.
            logger.debug("%s deleted before it started", full_name)
            w.stop()
            return
    

    【讨论】:

    • 这有一个错字,但我不能建议编辑,k8s 应该是 client
    • 感谢@Bessey 的指点。我还对代码做了进一步的小改进。
    猜你喜欢
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 2022-10-21
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    • 2015-09-11
    • 2018-08-31
    相关资源
    最近更新 更多