我需要连接到Azure Kubernetes以使用我自己的CLI工具

ergxz8rk  于 2024-01-06  发布在  Kubernetes
关注(0)|答案(1)|浏览(141)

以下场景
我用python写了自己的CLI工具,想建立一个到AKS的连接。因为我发现只有REST API才有可能(如果有其他方法,请告诉我)有人有这方面的经验吗?我该怎么做?
有一些命令,比如删除运行中的pod。非常基本的东西到这里。
在未来,通过令牌进行身份验证以涵盖安全方面也很重要。
我会很高兴的解决方案和答案。
我对AKS只有基本的经验,这就是为什么我问这个问题。而且大多数用户只是使用kubectl或其他工具,所以在谷歌上搜索它之后没有太多的方向

xdyibdwo

xdyibdwo1#

要使用Python CLI连接到AKS,可以使用Azure管理API(如Azure SDK for Python(Azure管理容器))来使用AKS API
确保已安装Azure SDK for Python,并且已配置最新版本的Azure CLI。

  1. pip install azure-mgmt-containerservice

字符串


的数据

  1. from azure.identity import DefaultAzureCredential
  2. from azure.mgmt.containerservice import ContainerServiceClient
  3. from azure.mgmt.containerservice.models import ManagedCluster
  4. #Authenticate
  5. credential = DefaultAzureCredential()
  6. #Specify your Azure subscription ID and resource group
  7. subscription_id = "your_subscription_id"
  8. resource_group = "your_resource_group"
  9. cluster_name = "your_cluster_name"
  10. #Create a ContainerServiceClient
  11. client = ContainerServiceClient(credential, subscription_id)
  12. #Get information about the AKS cluster
  13. cluster = client.managed_clusters.get(resource_group, cluster_name)
  14. print(f"AKS Cluster Name: {cluster.name}")
  15. print(f"AKS Cluster Location: {cluster.location}")
  16. print(f"AKS Cluster Kubernetes Version: {cluster.kubernetes_version}")


输出:

另一种方法是使用kubeconfig文件或bearer token,如matt_j的here所述

展开查看全部

相关问题