繁体   English   中英

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

[英]Running `connect_get_namespaced_pod_exec` using kubernetes client corev1api gives bad request

kubernetes 客户端 corev1api connect_get_namespaced_pod_exec 无法为 python 运行。

我检查了 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 - 不是有很大帮助。

按照此处的建议尝试使用流 - https://github.com/kubernetes-client/python/blob/master/examples/exec.py

冉:

api_response = stream(core_v1_api.connect_get_namespaced_pod_exec,
                      name, namespace,
                      command=exec_command,
                      stderr=True, stdin=False,
                      stdout=True, tty=False)

得到这个错误:

ApiException:(0)原因:主机名'10.47.7.95'与'','cluster.local'中的任何一个都不匹配

没有直接使用 CoreV1Api 的流 -

冉:

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)

得到这个错误:

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}

我写了一个简单的程序来检查:

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)

它对我来说非常好。

我相信您将minikube用于开发目的。 它无法识别您的主机名。 您可以通过在程序中禁用assert_hostname来使其工作,例如:

from kubernetes.client import configuration 
config.load_kube_config()                                                 
configuration.assert_hostname = False

这应该可以解决您的问题。

将 container='name' 添加到调用中会起作用,特别是如果您有任何像 istio-proxy 这样的 sidecar 容器正在 POD 上运行。

使用Prafull Ladha answer中的 stream() 建议时要小心。 它有很多陷阱:

  • 如果命令失败,不会抛出异常。 只需返回空字符串。 为此,需要设置 stream(..., _preload_content=False) 然后手动检查 resp.returncode 并在循环中手动调用 resp.readline_stdout 。
  • 如果命令运行时间超过 5 分钟而不产生任何输出,则会超时。 要解决这个问题,您需要在并行线程中调用 resp.sock.ping() 。

恐怕我没有更好的替代解决方案,也没有将其添加为评论的声誉。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM