简体   繁体   English

使用 kubernetes 客户端 corev1api 运行 `connect_get_namespaced_pod_exec` 会产生错误的请求

[英]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:它有很多陷阱:

  • Will not throw exception in case the command fails.如果命令失败,不会抛出异常。 Just return empty string.只需返回空字符串。 To cover that, one need to set stream(..., _preload_content=False) and then manually check resp.returncode and manually call resp.readline_stdout in a loop.为此,需要设置 stream(..., _preload_content=False) 然后手动检查 resp.returncode 并在循环中手动调用 resp.readline_stdout 。
  • Will timeout if the command takes more than 5min to run without producing any output.如果命令运行时间超过 5 分钟而不产生任何输出,则会超时。 To solve that you need to call resp.sock.ping() in a parallel thread.要解决这个问题,您需要在并行线程中调用 resp.sock.ping() 。

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.

相关问题 如何在 python k8s 客户端中以 root 身份运行“connect_get_namespaced_pod_exec” - How to run 'connect_get_namespaced_pod_exec' as root in python k8s client connect_get_namespaced_pod_exec 不返回任何内容 - connect_get_namespaced_pod_exec returns nothing 如何使用 Kubernetes Python CoreV1API 删除 statefulsets.apps - How to delete statefulsets.apps using Kubernetes Python CoreV1API 在 Kubernetes Python 客户端中使用 create_namespaced_secret API - Using create_namespaced_secret API in Kubernetes Python client 如何在python的kubernetes-client中从AutoscalingV2beta2Api create_namespaced_horizo​​ntal_pod_autoscaler()? - How to create_namespaced_horizontal_pod_autoscaler() from AutoscalingV2beta2Api in kubernetes-client in python? Kubernetes pod exec 使用 python 客户端不是很有交互性 - Kubernetes pod exec using python client not very interactive 使用 kubernetes 客户端在 pod 上运行脚本并退出 - Using kubernetes client to run a script on pod and get outout 如何在 Kubernetes 的 Pod 中分别连接 Python 服务器和 Python 客户端? - How to connect a Python server and Python client each in their pod on Kubernetes? Python kubernetes 客户端获取 pod Ready 状态时间 - Python kubernetes client Get pod Ready state time 如何使用kube.netes python客户端获取pod所在节点? - How can I get the node where the pod is located using kubernetes python client?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM