【问题标题】:Amazon EKS: generate/update kubeconfig via python scriptAmazon EKS:通过 python 脚本生成/更新 kubeconfig
【发布时间】:2020-10-01 23:59:25
【问题描述】:

在使用 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)?

【问题讨论】:

    标签: kubernetes boto3 aws-cli amazon-eks


    【解决方案1】:

    没有方法函数可以做到这一点,但你可以像这样自己构建配置文件:

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

    【讨论】:

      【解决方案2】:

      这在https://docs.aws.amazon.com/eks/latest/userguide/create-kubeconfig.html手动创建 kubeconfig 部分进行了解释,实际上是从 boto3 EKS 文档中引用的。那里的手动方法与@jaxxstorm 的答案非常相似,只是它没有显示您需要的python 代码,但它也没有假定heptio anthenticator(它显示令牌和IAM 身份验证器方法)。

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题,决定将它实现为 Python 包 它可以通过安装

        pip install eks-token
        

        然后简单地做

        from eks_token import get_token
        response = get_token(cluster_name='<value>')
        

        更多细节和例子here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-23
          • 2021-02-24
          相关资源
          最近更新 更多