Python:kubernetes exec失败,需要升级升级消息

时间:2017-01-21 01:12:08

标签: python python-2.7 kubernetes

情境:

我试图通过ls使用kubernetes包运行基本的cli.connect_post_namespaced_pod_exec()命令但是我得到了一个我不知道如何调试的堆栈跟踪。是的我试过四处寻找,但我不确定问题是什么,因为我正在使用来自here的文档示例

OS:

macOS Sierra 10.12.2

代码:

#!/usr/local/bin/python2.7

import logging

from pprint import pprint
from kubernetes import client, config

FORMAT = "[%(filename)s:%(lineno)s - %(funcName)s() ] %(message)s"
level = logging.DEBUG
logging.basicConfig(format=FORMAT, level=level)

def main():
    path_to_config = "/Users/acabrer/.kube/config"
    config.load_kube_config(config_file=path_to_config)
    ns = "default"
    pod = "nginx"
    cmd = "ls"
    cli = cli = client.CoreV1Api()
    response = cli.connect_post_namespaced_pod_exec(pod, ns, stderr=True, stdin=True, stdout=True, command=cmd)
    pprint(response)

if __name__ == '__main__':
    main()

堆栈追踪:

Traceback (most recent call last):
  File "/Users/acabrer/kube.py", line 16, in <module>
    main()
  File "/Users/acabrer/kube.py", line 12, in main
    response = cli.connect_post_namespaced_pod_exec(pod, ns, stderr=True, stdin=True, stdout=True)
  File "/usr/local/lib/python2.7/site-packages/kubernetes/client/apis/core_v1_api.py", line 3605, in connect_post_namespaced_pod_exec
    (data) = self.connect_post_namespaced_pod_exec_with_http_info(name, namespace, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/kubernetes/client/apis/core_v1_api.py", line 3715, in connect_post_namespaced_pod_exec_with_http_info
    collection_formats=collection_formats)
  File "/usr/local/lib/python2.7/site-packages/kubernetes/client/api_client.py", line 328, in call_api
    _return_http_data_only, collection_formats, _preload_content, _request_timeout)
  File "/usr/local/lib/python2.7/site-packages/kubernetes/client/api_client.py", line 152, in __call_api
    _request_timeout=_request_timeout)
  File "/usr/local/lib/python2.7/site-packages/kubernetes/client/api_client.py", line 373, in request
    body=body)
  File "/usr/local/lib/python2.7/site-packages/kubernetes/client/rest.py", line 257, in POST
    body=body)
  File "/usr/local/lib/python2.7/site-packages/kubernetes/client/rest.py", line 213, in request
    raise ApiException(http_resp=r)
kubernetes.client.rest.ApiException: (400)
Reason: Bad Request
HTTP response headers: HTTPHeaderDict({'Date': 'Sat, 21 Jan 2017 00:55:28 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}

任何输入都会有很大的影响。

修改1:

abrahams-mbp:.kube acabrer$ curl --help |grep TLSv
 -1, --tlsv1         Use >= TLSv1 (SSL)
     --tlsv1.0       Use TLSv1.0 (SSL)
     --tlsv1.1       Use TLSv1.1 (SSL)
     --tlsv1.2       Use TLSv1.2 (SSL)
abrahams-mbp:.kube acabrer$ python2.7 -c "import ssl; print ssl.OPENSSL_VERSION_INFO"
(1, 0, 2, 10, 15)

编辑2:

abrahams-mbp:.kube acabrer$ curl --tlsv1.2 https://x.x.x.x -k
Unauthorized
abrahams-mbp:.kube acabrer$ curl --tlsv1.1 https://x.x.x.x -k
curl: (35) Unknown SSL protocol error in connection to x.x.x.x:-9836

编辑3: 我在api_client.py中放了一些打印语句来查看完整的请求信息,这就是我所看到的。

注意:为了安全起见,我将ip-address删除到了我的端点。

bash-3.2# vim /usr/local/lib/python2.7/site-packages/kubernetes/client/api_client.py
bash-3.2# /Users/acabrer/kube.py
################
POST
https://x.x.x.x/api/v1/namespaces/default/pods/nginx/exec
[('stdin', True), ('command', 'ls'), ('stderr', True), ('stdout', True)]
{'Content-Type': 'application/json', 'Accept': '*/*', 'User-Agent': 'Swagger-Codegen/1.0.0-alpha/python'}
[]
None
################

谢谢,

-Abe。

4 个答案:

答案 0 :(得分:1)

要在这里回答我自己的问题,这是在这里引用的实际python绑定中的一个错误:https://github.com/kubernetes-incubator/client-python/issues/58

答案 1 :(得分:0)

我遇到了同样的问题。解决方案是使用kubernetes.stream。您需要导入软件包并仅更改一行代码,如下所示:

html,body

答案 2 :(得分:0)

Python kuberenetes客户端现在有一个完整的示例如何exec

https://github.com/kubernetes-client/python/blob/master/examples/exec.py

答案 3 :(得分:-1)

如果您使用的是系统Python 2.7,Kubernetes 1.4+ REST API很可能无法在MacOS X上与Python客户端一起使用。这是因为我认为它需要TLS 1.2用于安全连接,而系统Python 2.7使用MacOS X上的OpenSSL,仅支持TLS 1.1。

您必须使用HomeBrew中的Python或Python 3.6的PSF版本。如果使用PSF Python 3.6,请确保执行额外的安装后步骤以安装证书包。