I want to fetch the cluster name, region, and zone of the cluster. How can I do that in python

Tushar Sharma 46 Reputation points
2023-01-09T07:43:52.383+00:00

I want to fetch the cluster name, region, and zone of the cluster. How can I do that in python by using Kubernetes API or tell me if we have any other method to fetch data, I didn't find any documents related to that.

Azure Kubernetes Service
Azure Kubernetes Service
An Azure service that provides serverless Kubernetes, an integrated continuous integration and continuous delivery experience, and enterprise-grade security and governance.
2,458 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andrei Barbu 2,596 Reputation points Microsoft Employee
    2023-01-09T07:58:03.15+00:00

    Hello @Tushar Sharma !

    Thank you for raising this question!

    The recommended way to fetch this data is via azure cli. You can use "az aks show" command together with "--query parameter" and output as tsv. To be handy for you, I prepared the commands for your needs (name, region and availability zone):

    az aks show -g resourceGroupName -n clusterName --query "name" -o tsv

    az aks show -g resourceGroupName -n clusterName --query "location" -o tsv

    az aks show -g resourceGroupName -n clusterName --query "agentPoolProfiles[].availabilityZones" -o tsv

    I hope this answers your question.

    If the ANSWER is helpful, please click "Accept Answer" and upvote it.

    Thank you!

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Akram Kathimi 1,281 Reputation points Microsoft Employee
    2023-01-09T08:47:00.107+00:00

    Hi @Tushar Sharma ,

    Following to Andrei's answer, using python you can have something like:

    from azure.mgmt.containerservice import ContainerServiceClient  
    from azure.identity import AzureCliCredential  
      
      
    credential = AzureCliCredential()  
      
    # Replace the value of resource_group with the name of your resource group  
    subscription_id='your-subid'  
    resource_group = 'rg-name'  
    aks_name= 'cluster-name'  
      
    client=ContainerServiceClient(credential,subscription_id)  
      
    aks=client.managed_clusters.get(resource_group_name=resource_group,resource_name=aks_name)  
    cluster_name = aks.name  
    region = aks.location  
    zone = aks.agent_pool_profiles[0].availability_zones  
      
    # Print the AKS cluster information  
    print(f'Cluster name: {cluster_name}')  
    print(f'Region: {region}')  
    print(f'Zone: {zone}')  
    

    You can refer to https://github.com/Azure-Samples/azure-samples-python-management/tree/main/samples/containerservice for examples.
    Also, keep in mind that the zone depends on the node pool, so you might want to loop over the agent pools (in my example I just printed the first one).

    Hope this helps !
    Please "Accept as Answer" if it helped, so that it can help others in the community looking for help on similar topics.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.