4

客观的

我想从正在运行的 pod 内连接并调用 Kubernetes REST API,所讨论的 Kubernetes 是使用 IAM 身份验证的 AWS EKS 集群。所有这些都使用 Kubernetes Python 库。

我试过的

从我的内部python file

from kubernetes import client, config

config.load_incluster_config()
v1 = client.CoreV1Api()
ret = v1.list_pod_for_all_namespaces(watch=False)

上面的命令会抛出一个403错误,我相信这是由于 AWS EKS 使用了不同的身份验证机制。

我已经知道的工作

ApiToken = 'eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.xxx.yyy'
    configuration = client.Configuration()
    configuration.host = 'https://abc.sk1.us-east-1.eks.amazonaws.com'
    configuration.verify_ssl = False
    configuration.debug = True
    configuration.api_key = {"authorization": "Bearer " + ApiToken}
    client.Configuration.set_default(configuration)

虽然上述方法有效,但我必须对我通过 kubectl 在本地生成的令牌进行硬编码,并将其签入存在安全风险的代码中。

有没有更合适的方法来使用 AWS EKS 对 Kubernetes python 库进行身份验证?

4

1 回答 1

2

您可以使用以下方法获取令牌。这假设您已在 pod/server/laptop 上成功安装和配置aws-iam-authenticator 。

def get_token(cluster_name):
    args = ("/usr/local/bin/aws-iam-authenticator", "token", "-i", cluster_name, "--token-only")
    popen = subprocess.Popen(args, stdout=subprocess.PIPE)
    popen.wait()
    return popen.stdout.read().rstrip()

api_token = get_token("<cluster_name>")
configuration = client.Configuration()
configuration.host = '<api_endpoint>'
configuration.verify_ssl = False
configuration.debug = True
configuration.api_key['authorization'] = "Bearer " + api_token
configuration.assert_hostname = True
configuration.verify_ssl = False
client.Configuration.set_default(configuration)

v1 = client.CoreV1Api()
ret = v1.list_pod_for_all_namespaces(watch=False)
print ret

kubernetes-client/python-base 有一个 PR,增加了对 exec 插件的支持,尝试在 kubeconfig 中实现 exec-plugins 支持

于 2018-10-02T03:04:39.257 回答