[英]Running `connect_get_namespaced_pod_exec` using kubernetes client corev1api gives bad request
kubernetes client corev1api connect_get_namespaced_pod_exec fails to run for python. kubernetes 客户端 corev1api connect_get_namespaced_pod_exec 无法为 python 运行。
I have checked the python version == 2.7 and pip freeze - ipaddress==1.0.22, urllib3==1.24.1 and websocket-client==0.54.0 are the versions which satisfy the requirement - as mentioned here: https://github.com/kubernetes-client/python/blob/master/README.md#hostname-doesnt-match followed the issue on this thread - https://github.com/kubernetes-client/python/issues/36 - not much help.我检查了 python 版本 == 2.7 和 pip freeze - ipaddress==1.0.22、urllib3==1.24.1 和 websocket-client==0.54.0 是满足要求的版本 - 如此处所述: https:/ /github.com/kubernetes-client/python/blob/master/README.md#hostname-doesnt-match关注此线程上的问题 - https://github.com/kubernetes-client/python/issues/36 - 不是有很大帮助。
Tried usings stream as suggested here - https://github.com/kubernetes-client/python/blob/master/examples/exec.py按照此处的建议尝试使用流 - https://github.com/kubernetes-client/python/blob/master/examples/exec.py
Ran:冉:
api_response = stream(core_v1_api.connect_get_namespaced_pod_exec,
name, namespace,
command=exec_command,
stderr=True, stdin=False,
stdout=True, tty=False)
Got this error:得到这个错误:
ApiException: (0) Reason: hostname '10.47.7.95' doesn't match either of '', 'cluster.local'
ApiException:(0)原因:主机名'10.47.7.95'与'','cluster.local'中的任何一个都不匹配
Without stream using directly the CoreV1Api -没有直接使用 CoreV1Api 的流 -
Ran :冉:
core_v1_api = client.CoreV1Api()
api_response = core_v1_api.connect_get_namespaced_pod_exec(name=name,namespace=namespace,command=exec_command,stderr=True, stdin=False,stdout=True, tty=False)
Got this error:得到这个错误:
ApiException: (400) Reason: Bad Request HTTP response headers: HTTPHeaderDict({'Date': 'Sat, 05 Jan 2019 08:01:22 GMT', 'Content-Length': '139', 'Content-Type': 'application/json'}) HTTP response body: {"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"Upgrade request required","reason":"BadRequest","code":400}
ApiException:(400)原因:错误请求 HTTP 响应标头:HTTPHeaderDict({'Date':'Sat,2019 年 1 月 5 日 08:01:22 GMT','Content-Length':'139','Content-Type': 'application/json'}) HTTP 响应正文:{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"升级请求需要","原因":"BadRequest","code":400}
I wrote a simple program to check that:我写了一个简单的程序来检查:
from kubernetes import client, config
from kubernetes.stream import stream
# create an instance of the API class
config.load_kube_config()
api_instance = client.CoreV1Api()
exec_command = [
'/bin/sh',
'-c',
'echo This is Prafull Ladha and it is test function']
resp = stream(api_instance.connect_get_namespaced_pod_exec, "nginx-deployment-76bf4969df-467z2", 'default',
command=exec_command,
stderr=True, stdin=False,
stdout=True, tty=False)
print("Response: " + resp)
It is working perfectly fine for me.它对我来说非常好。
I believe you're using minikube
for development purpose.我相信您将
minikube
用于开发目的。 It is not able to recognise your hostname.它无法识别您的主机名。 You can make it work by disabling
assert_hostname
in your program like:您可以通过在程序中禁用
assert_hostname
来使其工作,例如:
from kubernetes.client import configuration
config.load_kube_config()
configuration.assert_hostname = False
This should resolve your issue.这应该可以解决您的问题。
将 container='name' 添加到调用中会起作用,特别是如果您有任何像 istio-proxy 这样的 sidecar 容器正在 POD 上运行。
Watch out when using the stream() suggestion from Prafull Ladha answer .使用Prafull Ladha answer中的 stream() 建议时要小心。 It has a lot of pitfalls:
它有很多陷阱:
I'm afraid i don't have a better alternative solution, and not the reputation to add this as comment.恐怕我没有更好的替代解决方案,也没有将其添加为评论的声誉。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.