使用kubernetes SDK for python创建pod

2024-09-26 18:15:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用python客户端SDK创建一个pod(类似于kubectl运行),它会给出如下结果:

kubectl run --rm --image $IMAGE --overrides="$OVERRIDES" --generator=run-pod/v1 -ti "$POD"

有好的例子吗?我可以看到一些使用exechttps://github.com/kubernetes-client/python/blob/master/examples/pod_exec.py的示例,但这需要首先创建pod并运行exec,但不运行“Interactive Terminal”作为kubectl run中的-ti标志

下面的代码仅适用于发送远程命令,而不适用于交互式终端,即使我们运行command=['/bin/bash'],其输出如下所示

    try:    
        #resp = stream(api_instance.connect_post_namespaced_pod_exec, podname, namespace='default', stderr=True, stdin=True, stdout=True, tty=True)
        stream(api_instance.connect_get_namespaced_pod_exec, podname, namespace='default', stderr=True, stdin=False, stdout=True, tty=False)
    except ApiException as e:
        print("Exception when calling CoreV1Api->connect_get_namespaced_pod_exec: %s\n" % e)
    print(f"Pod created - cannot exec")

enter image description here


Tags: instancerunapitruedefaultstreamconnectstderr

热门问题