使用 kubernetes 客户端 corev1api 运行 `connect_get_namespaced_pod_exec` 给出错误请求

Ult*_*ndz 6 python api kubernetes

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)
Run Code Online (Sandbox Code Playgroud)

得到这个错误:

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)
Run Code Online (Sandbox Code Playgroud)

得到这个错误:

ApiException: (400) 原因:错误请求 HTTP 响应标头:HTTPHeaderDict({'Date': 'Sat, 05 Jan 2019 08:01:22 GMT', 'Content-Length': '139', 'Content-Type': 'application/json'}) HTTP 响应正文:{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"升级请求required","re​​ason":"BadRequest","code":400}

Pra*_*dha 5

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

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)
Run Code Online (Sandbox Code Playgroud)

它对我来说非常好。

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

from kubernetes.client import configuration 
config.load_kube_config()                                                 
configuration.assert_hostname = False
Run Code Online (Sandbox Code Playgroud)

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