Create a pod using kubernetes SDK for python

5/29/2020

I want to create a pod with the python client SDK (similar to kubectl run) that gives me results like the below:

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

Any good examples? I can see some examples which use exec https://github.com/kubernetes-client/python/blob/master/examples/pod_exec.py but this requires to create the pod first and run exec, but does not run "Interactive Terminal" as the -ti flags in kubectl run.

The code below works only with sending remote command but not with interactive terminal even if we run command=['/bin/bash'] which shows the output as below.

    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

-- mohatb
kubernetes
kubernetes-python-client
python
python-3.x

0 Answers