如何使用 python 从 azure aks 获取 kubectl 配置?

how to get kubectl configuration from azure aks with python?

我使用 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 命令?

谢谢!

在这种情况下,解决方案是使用 Azure Python SDK 绕过 Az Cli 直接从 Azure API 检索集群凭据。

https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/containerservice/azure-mgmt-containerservice/azure/mgmt/containerservice/v2019_11_01/operations/_managed_clusters_operations.py#L320

是的,这可以通过 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]