gpt4 book ai didi

kubernetes - 亚马逊EKS : generate/update kubeconfig via python script

转载 作者:行者123 更新时间:2023-12-03 21:10:06 24 4
gpt4 key购买 nike

使用Amazon的K8s产品 EKS 服务时,有时需要将Kubernetes API和配置连接到AWS内建立的基础架构。特别是,我们需要一个具有适当凭据和URL的kubeconfig才能连接到EKS提供的k8s控制平面。

Amazon命令行工具aws提供了此任务的例程

aws eks update-kubeconfig --kubeconfig /path/to/kubecfg.yaml --name <EKS-cluster-name>

问题:通过Python/boto3进行相同操作

当查看 Boto API documentation时,我似乎无法发现上述 aws例程的等效项。也许我在找错地方了。
  • boto中是否有现成的函数可以实现此目的?
  • 否则,您将如何直接在python中处理此问题(除了在子进程中调用aws之外)?
  • 最佳答案

    没有方法函数可以执行此操作,但是您可以自己构建配置文件,如下所示:

    # Set up the client
    s = boto3.Session(region_name=region)
    eks = s.client("eks")

    # get cluster details
    cluster = eks.describe_cluster(name=cluster_name)
    cluster_cert = cluster["cluster"]["certificateAuthority"]["data"]
    cluster_ep = cluster["cluster"]["endpoint"]

    # build the cluster config hash
    cluster_config = {
    "apiVersion": "v1",
    "kind": "Config",
    "clusters": [
    {
    "cluster": {
    "server": str(cluster_ep),
    "certificate-authority-data": str(cluster_cert)
    },
    "name": "kubernetes"
    }
    ],
    "contexts": [
    {
    "context": {
    "cluster": "kubernetes",
    "user": "aws"
    },
    "name": "aws"
    }
    ],
    "current-context": "aws",
    "preferences": {},
    "users": [
    {
    "name": "aws",
    "user": {
    "exec": {
    "apiVersion": "client.authentication.k8s.io/v1alpha1",
    "command": "heptio-authenticator-aws",
    "args": [
    "token", "-i", cluster_name
    ]
    }
    }
    }
    ]
    }

    # Write in YAML.
    config_text=yaml.dump(cluster_config, default_flow_style=False)
    open(config_file, "w").write(config_text)

    关于kubernetes - 亚马逊EKS : generate/update kubeconfig via python script,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54953190/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com