使用令牌向Kubernetes集群进行身份验证

2024-05-20 09:38:44 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这样,当kubectl运行时,它将请求令牌对Kubernetes集群进行身份验证。在

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

在做任何一个例子时,我得到的错误是system:anonymous不允许执行某个操作(例如listpods)。我认为问题的根源是我需要从aws-iam-authenticator向我的客户机请求传递一个令牌。在

不幸的是,我似乎不知道如何将这个令牌传递给Kubernetes的Python客户机。我看到了this snippet,但是我得到了一个错误,api_key属性不是configuration模块的一部分(果然,它不是)。在

我应该如何将令牌注入来自Python客户机for Kubernetes的请求?在

提前谢谢!在


Tags: 用户authenticatorawsconfig客户机环境错误方式
1条回答
网友
1楼 · 发布于 2024-05-20 09:38:44

我相信您需要通过以下方式配置“Authorization:bearner”头: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

相关问题 更多 >