我的环境设置方式 (AWS EKS) 是在我的 ~/.kube/config
中,用户有一个 exec 配置来调用 aws-iam-authenticator
.
这样,当 kubectl
运行时,它将请求 token 对 Kubernetes 集群进行身份验证。
我目前正在编写一个与 Kubernetes API 交互的客户端应用程序。这是用 Python 编写的,使用 official Python client .
在执行任何示例时,我收到错误,不允许 system:anonymous
执行特定操作(例如列出 Pod)。我认为问题的根源是我需要将 token 从 aws-iam-authenticator
传递给我的客户端请求。
不幸的是,我似乎无法弄清楚如何使用 Kubernetes 的 Python 客户端传递此 token 。我明白了this snippet但我收到一个错误,指出 api_key
属性不是 configuration
模块的一部分(果然,它不是)。
我该如何将 token 注入(inject)到来自 Kubernetes 的 Python 客户端的请求中?
提前致谢!
最佳答案
我相信您需要通过以下方式配置“Authorization:Bearer” header :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
关于python - 使用 token 向 Kubernetes 集群进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52653931/