【问题标题】:how to get kubectl configuration from azure aks with python?如何使用 python 从 azure aks 获取 kubectl 配置?
【发布时间】:2019-05-01 07:54:28
【问题描述】:

我用python创建了一个k8s部署脚本,为了从kubectl获取配置,我使用python命令:

from kubernetes import client, config

config.load_kube_config()

要获得 azure aks 配置,我使用以下 az 命令:

az login
az aks get-credentials --resource-group [resource group name] --name [aks name]

有没有什么方法可以只从 python 获取 azure aks 凭据,而不需要 az 命令?

谢谢!

【问题讨论】:

  • 是的,使用 python sdk 并直接调用相同的 api(或者不使用 sdk,只需对 api 进行 http 请求)。但是,只是做一些subprocess.check_call('az aks get-credentials --resource-group [resource group name] --name [aks name]', shell=True) 会容易得多
  • 我不想使用subprocess 解决方案,因为我不想在az 命令中登录Azure(我不想依赖az 包,仅在 python 上)。
  • 好吧,无论哪种方式,您都必须进行身份验证,这取决于您。 python SDKgithub.com/Azure/azure-sdk-for-python。这可能是你感兴趣的电话吧:github.com/Azure/azure-sdk-for-python/blob/master/…
  • 非常感谢!我使用list_cluster_admin_credential 来获取我的配置

标签: python azure kubernetes azure-aks


【解决方案1】:

【讨论】:

【解决方案2】:

是的,这可以通过 Azure Python SDK 完成。

from azure.identity import DefaultAzureCredential
from azure.mgmt.containerservice import ContainerServiceClient

credential = DefaultAzureCredential(exclude_cli_credential=True)
subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
container_service_client = ContainerServiceClient(credential, subscription_id)

kubeconfig = container_service_client.managed_clusters.list_cluster_user_credentials("resourcegroup-name", "cluster-name").kubeconfigs[0]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-26
    • 2021-09-29
    • 2020-05-07
    • 2023-04-09
    • 2021-08-28
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多