【问题标题】:How to connect to kubernetes cluster without config file in Python SDK?如何在没有 Python SDK 中的配置文件的情况下连接到 kubernetes 集群?
【发布时间】:2019-11-08 04:07:18
【问题描述】:

我正在制作一个使用 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))

【问题讨论】:

  • 您是否尝试从另一个 kubernetes 集群访问?还是一个位置的多个 Kubernetes 集群?

标签: python kubernetes amazon-eks


【解决方案1】:

根据“load_authentication”的文档:

这个函数会在 kube-config 的 user 部分遍历各种认证方法,如果找到有效的认证方法就停止。认证方式顺序为:

  • auth-provider(gcp、azure、oidc)
  • 令牌字段(指向令牌文件)
  • exec 提供的插件
  • 用户名/密码

_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

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2021-03-15
    • 2020-11-11
    • 2017-09-04
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    相关资源
    最近更新 更多