无法将命令执行到 kubernetes pod
Unable to exec command into kubernetes pod
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 . It's working fine. So, it should not be a configuration issue. I've read about this problem in the past here and here。但我认为这不可能,因为它们是已关闭的问题。
如果我 运行 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
另一个更新,我找到了这个 info。页面最后有一段说.
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.
我试过这样做,但结果相同:(
知道我做错了什么吗?
非常感谢您的帮助。
此致
是的,这个 official guide 说你应该改用 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)
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 . It's working fine. So, it should not be a configuration issue. I've read about this problem in the past here and here。但我认为这不可能,因为它们是已关闭的问题。
如果我 运行 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
另一个更新,我找到了这个 info。页面最后有一段说.
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.
我试过这样做,但结果相同:(
知道我做错了什么吗?
非常感谢您的帮助。
此致
是的,这个 official guide 说你应该改用 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)