开发者社区> 问答> 正文

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

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

我检查了python版本== 2.7和pip冻结 - 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 - not not很有帮助。

尝试使用流这里建议 - 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,05 Jan 2019 08:01:22 GMT','Content-Length':'139','Content-Type': 'application / json'})HTTP响应正文:{“kind”:“状态”,“apiVersion”:“v1”,“元数据”:{},“状态”:“失败”,“消息”:“升级请求所需的”, “理由”: “错误请求”, “代码”:400}

展开
收起
k8s小能手 2019-01-09 13:41:15 8903 0
1 条回答
写回答
取消 提交回答
  • 整合最优质的专家资源和技术资料,问答解疑

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

    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

    2019-07-17 23:24:56
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
ACK 云原生弹性方案—云原生时代的加速器 立即下载
ACK集群类型选择最佳实践 立即下载
企业运维之云原生和Kubernetes 实战 立即下载

相关镜像