我正在制作一个应用程序,它将使用 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/