Kubernetes Python客户端:身份验证问题

时间:2019-04-01 09:23:17

标签: kubernetes kubernetes-python-client

我正在尝试将python API用于kubernetes,但似乎无法执行请求。我认为管道不清楚。

我正在执行以下步骤:Kubernetes python client: authentication issue

在远程服务器上:

  • 我设置了我的服务帐户并按照链接中的描述生成了机密
  • 我将令牌添加到了我的代码中

我被拒绝连接。

  • 我应该将任何信息从群集导入本地客户端吗?
  • 港口还好吗?
from kubernetes import client, config
def main():
    configuration = client.Configuration() 

    configuration.host = "http://my_ip:8080"
    configuration.api_key_prefix['authorization'] = "Bearer"
    configuration.api_key['authorization'] = "my_token"
    v1 = client.CoreV1Api()
    print("Listing pods with their IPs:")
    ret = v1.list_pod_for_all_namespaces(watch=False)
    for i in ret.items:
        print("%s\t%s\t%s" %
            (i.status.pod_ip, i.metadata.namespace, i.metadata.name))

if __name__ == '__main__':
    main()

输出:

   raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='xx.xx.xx.xx', port=8080): Max retries exceeded with url: /api/v1/pods?watch=False (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x1118e5668>: Failed to establish a new connection: [Errno 61] Connection refused'

当前本地客户端kubectl配置视图:

apiVersion: v1
clusters: []
contexts: []
current-context: ""
kind: Config
preferences: {}
users: []

2 个答案:

答案 0 :(得分:1)

首先,检查kubectl的配置是否正确,如下面docs here的摘录所示。

  

配置kubectl

     

为了让kubectl查找和访问Kubernetes集群,需要一个kubeconfig文件,该文件在您使用kube-up.sh创建集群或成功部署Minikube集群时会自动创建。 [...]默认情况下,kubectl配置位于~/.kube/config

     

检查kubectl配置

     

通过获取集群状态检查kubectl是否已正确配置:

     

kubectl cluster-info

     

如果看到URL响应,则表明kubectl已正确配置为访问您的集群。   如果您看到类似以下的消息,则说明kubectl配置不正确或无法连接到Kubernetes集群。

     

The connection to the server <server-name:port> was refused - did you specify the right host or port?


因为看起来好像它没有连接到集群。如果没有返回URL,则可以从远程服务器(从config目录中)获取$HOME/.kube文件。

您可以将该文件放置在本地计算机上,命名为$HOME/.kube/config

然后,您可以使用以下命令在Python脚本中加载该配置文件:

def main():
    config.load_kube_config()

可以找到示例here

答案 1 :(得分:0)

您忽略了在配置中指定API服务器证书。您包含的链接具有以下内容:

configuration.ssl_ca_cert = '<path_to_cluster_ca_certificate>'
相关问题