【问题标题】:Authenticate with Kubernetes cluster with token使用令牌对 Kubernetes 集群进行身份验证
【发布时间】:2019-03-10 06:30:56
【问题描述】:

我的环境设置方式 (AWS EKS) 是在我的~/.kube/config 中,用户有一个执行配置来调用aws-iam-authenticator

这样当kubectl 运行时,它会向 Kubernetes 集群请求令牌以进行身份​​验证。

我目前正在编写一个将与 Kubernetes API 交互的客户端应用程序。这是用 Python 编写的,使用 official Python client

在执行任何示例时,我都会收到 system:anonymous 不允许执行特定操作(例如列出 pod)的错误。我认为问题的根源在于我需要将令牌从aws-iam-authenticator 传递给我的客户端请求。

不幸的是,我似乎无法弄清楚如何使用 Kubernetes 的 Python 客户端传递此令牌。我看到了this snippet,但我收到一个错误,指出api_key 属性不是configuration 模块的一部分(果然,它不是)。

我应该如何将令牌注入到来自 Kubernetes 的 Python 客户端的请求中?

提前致谢!

【问题讨论】:

    标签: python kubernetes


    【解决方案1】:

    我相信您需要通过以下方式配置“Authorization: Bearer”标头:configuration.api_key_prefix['authorization'] = 'Bearer'。所以基本上:

    from __future__ import print_function
    import time
    import kubernetes.client
    from kubernetes.client.rest import ApiException
    from pprint import pprint
    
    # Configure API key authorization: BearerToken
    configuration = kubernetes.client.Configuration()
    configuration.api_key['authorization'] = 'YOUR_API_KEY'
    # Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
    configuration.api_key_prefix['authorization'] = 'Bearer' ## <== This one
    
    # create an instance of the API class
    api_instance = kubernetes.client.ApiregistrationV1Api(kubernetes.client.ApiClient(configuration))
    body = kubernetes.client.V1APIService() # V1APIService | 
    pretty = 'pretty_example' # str | If 'true', then the output is pretty printed. (optional)
    
    try: 
        api_response = api_instance.create_api_service(body, pretty=pretty)
        pprint(api_response)
    except ApiException as e:
        print("Exception when calling ApiregistrationV1Api->create_api_service: %s\n" % e)
    

    基本上是描述here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-21
      • 2019-01-21
      • 2014-09-26
      • 1970-01-01
      • 2016-12-17
      相关资源
      最近更新 更多