Python 版本 3.8.10 Kubernetes 版本 23.3.0
我正在嘗試使用 python 將命令運行到 kubernetes 中的特定 pod 中。我試圖盡可能地減少代碼,所以我正在運行它。
from kubernetes import client, config
config.load_kube_config()
v1 = client.CoreV1Api()
response = v1.connect_get_namespaced_pod_exec(pod_name , namespace, command="df -h", stderr=True, stdin=True, stdout=True, tty=True)
print(response)
但它不起作用。我收到了這個回復。
kubernetes.client.exceptions.ApiException: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Audit-Id': '511c23ce-03bb-4b52-a559-3f354fc80235', 'Cache-Control': 'no-cache, private', 'Content-Type': 'application/json', 'Date': 'Fri, 18 Mar 2022 18:06:11 GMT', 'Content-Length': '139'})
HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Upgrade request required","reason":"BadRequest","code":400}
如果我運行list all pods的典型示例。它作業正常。因此,它不應該是配置問題。我過去在這里和這里讀過這個問題。但我認為不可能是這樣,因為它們是封閉的問題。
如果我運行 k9s shell 請求,我可以毫無問題地連接到 pod。這就是我ps a在執行此操作時看到的/usr/bin/kubectl --context gke_cloudpak_europe-west2-xxxxx exec -it -n namespace_name pod_name -c rt -- sh -c command -v bash >/dev/null && exec bash || exec sh
另一個更新,我找到了這個資訊。在頁面的最后有一段說。
Why Exec/Attach calls doesn’t work
Starting from 4.0 release, we do not support directly calling exec or attach calls. you should use stream module to call them. so instead of resp = api.connect_get_namespaced_pod_exec(name, ... you should call resp = stream(api.connect_get_namespaced_pod_exec, name, ....
Using Stream will overwrite the requests protocol in core_v1_api.CoreV1Api() This will cause a failure in non-exec/attach calls. If you reuse your api client object, you will need to recreate it between api calls that use stream and other api calls.
我試過這樣做,但結果相同:(
Any idea about what I'm doing wrong?
Thanks a lot for your help.
Regards
uj5u.com熱心網友回復:
是的,這個官方指南說你應該resp = **stream**(api.connect_get_namespaced_pod_exec(name, ...改用。
所以你必須像這樣編輯你的代碼:
...
from kubernetes.stream import stream
...
v1 = client.CoreV1Api()
response = stream(v1.connect_get_namespaced_pod_exec, pod_name , namespace, command="df -h", stderr=True, stdin=True, stdout=True, tty=True)
print(response)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/446654.html
標籤:python kubernetes kubernetes-pod kubernetes-container
