python - 如何在Python SDK中没有配置文件的情况下连接到kubernetes集群?

标签 python kubernetes amazon-eks

我正在制作一个应用程序,它将使用 K8S (EKS) 来编排集群上的部署。目前,我正在使用 Minikube 进行开发,因此它按预期进行,但它使用 $HOME/.kube/config 文件来加载配置。连接 EKS 的其他方式有哪些?

我用于工作证明的示例代码:

from kubernetes import client, config

# Configs can be set in Configuration class directly or using helper utility
config.load_kube_config()

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))

最佳答案

根据“load_authentication”文档:

This function goes through various authentication methods in user section of kube-config and stops if it finds a valid authentication method. The order of authentication methods is:

  • auth-provider (gcp, azure, oidc)
  • token field (point to a token file)
  • exec provided plugin
  • username/password

_load_authentication_ - 从 kube-config 用户部分读取身份验证(如果存在)。 以下是如何将 client.Configuration()barer token to authenticate 一起使用的示例.

另请查看 Authenticate with the Azure Management Libraries for Python

###更新###

对于 AWS 特定环境,您可以使用 AWS SDK for Python (Boto 3)Create a kubeconfig for Amazon EKS

有一个社区示例 - 如何将 aws 的凭证设置到 kubeconfig 或构建配置 using boto3

希望这有帮助。

关于python - 如何在Python SDK中没有配置文件的情况下连接到kubernetes集群?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56768947/

相关文章:

python - Tensorflow:将 `tf.data.Dataset` 迭代器转换为张量

kubernetes - 如何手动强制Kubernetes扩展节点?

kubernetes - 将 minikube 上的服务映射到主机 IP

kubernetes - 为什么 EKS 中默认有两个 ENI?

python - 是否可以插入excel公式值而不是公式

python - 在Python子进程中,使用Popen()和check_output()有什么区别?

python - numpy "lock"跨进程占用什么资源?

kubernetes - 如何在GCE上的容器内使用gsutil从存储桶中复制文件

amazon-web-services - 如何在Cloudformation模板中引用现有的ELB DNSName

.net-core - 使用角色 ARN 从 EKS 连接到 SQS