Skip to main content
 首页 » 编程设计

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

2024年12月31日11txw1958

我用 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 完成。

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]