首页 > 解决方案 > 尝试将 connect_get_namespaced_pod_exec 与 kubernetes python 客户端一起使用时出现 503 错误

问题描述

我正在尝试使用 kubernetes 客户端在 python 中执行 pod exec。

我想我错过了一些明显的东西,因为这不起作用。

问题可能出在哪里?

我的代码如下:

    TEST_FQDNS = ['kubernetes.default.svc.cluster.local']

    def k8Connect(kubeconfig,cluster,arn):
    """kubernetes connection"""
    try:
        config.load_kube_config_from_dict(config_dict=kubeconfig,context=arn)
        kube = client.CoreV1Api()
        if not kube:
            return None
        return kube
    except Exception as e:
        print('k8Connect: ' + str(e))
        return None

    kubeconfig = buildKubeConfig(cluster_info)
    k8conn = k8Connect(kubeconfig,cluster,cluster_info['arn'])

    whois_pods = [{"name": "whois-57b44f8756-mc8zz", "status": "Running"}, {"name": "whois-57b44f8756-qhnwc", "status": "Running"}]

    for tstfqdn in TEST_FQDNS:
          pod = whois_pods[0]['name']
          whois_command = ['sh','-c', 'nslookup', tstfqdn]
          res = stream(k8conn.connect_get_namespaced_pod_exec,
          name=pod, namespace='kube-system',
          command=whois_command, container='whois',
          stderr=True, stdin=False,
          stdout=True, tty=True)
          while res.is_open():
            if res.peek_stdout():
              print("STDOUT: %s" % res.read_stdout())
            if resp.peek_stderr():
              print("STDERR: %s" % resp.read_stderr())

我收到以下错误:

Traceback (most recent call last):
  File "v2.eksreport.py", line 587, in <module>
    main()
  File "v2.eksreport.py", line 571, in main
    stdout=True, tty=True)
  File "/usr/local/lib/python3.6/site-packages/kubernetes/stream/stream.py", line 35, in _websocket_request
    return api_method(*args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/kubernetes/client/api/core_v1_api.py", line 994, in connect_get_namespaced_pod_exec
    return self.connect_get_namespaced_pod_exec_with_http_info(name, namespace, **kwargs)  # noqa: E501
  File "/usr/local/lib/python3.6/site-packages/kubernetes/client/api/core_v1_api.py", line 1115, in connect_get_namespaced_pod_exec_with_http_info
    collection_formats=collection_formats)
  File "/usr/local/lib/python3.6/site-packages/kubernetes/client/api_client.py", line 353, in call_api
    _preload_content, _request_timeout, _host)
  File "/usr/local/lib/python3.6/site-packages/kubernetes/client/api_client.py", line 184, in __call_api
    _request_timeout=_request_timeout)
  File "/usr/local/lib/python3.6/site-packages/kubernetes/stream/ws_client.py", line 474, in websocket_call
    raise ApiException(status=0, reason=str(e))
kubernetes.client.exceptions.ApiException: (0)
Reason: failed CONNECT via proxy status: 503

标签: pythonkubernetes

解决方案


推荐阅读