Get count of all containers across all our AKS clusters?

feelsLikeHome2020 21 Reputation points


Management is asking for how many containers we have across all our AKS clusters (170).

All the clusters are 'Managed Clusters', so Metrics>Containers reports the number of User Pods and System Pods in each of the clusters, but does not contain a grand total or way to export the results to CSV.

Also, Pods may have more than one container, so even if these numbers could be totaled, they won't be the total number of containers.

If there is a straightforward way to retrieve the total count of containers (or total count of pods) we have across all our AKS clusters, that will be really helpful.

Thank you,

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

1 answer

Sort by: Most helpful
  1. shiva patpi 13,156 Reputation points Microsoft Employee

    Hello @feelsLikeHome2020 ,
    I was thinking of writing a custom powershell script to get that data. Can you try out the below script. It will output the AKS Clustername,NumberOfPods on the screen.

    $AllAKSClusters = Get-AzAks #Get All AKS Clusters List
    $AksclusterAndPods=@{} #Hashtable to store the list
    foreach($akscluster in $AllAKSClusters) #loop threugh each AKS cluster
    $aksclustername = $akscluster.Name
    $id = $akscluster.Id
    $id = $id.Substring($id.IndexOf('resourcegroups')+15)
    $aksresourcegroup = $id.Substring(0,$id.IndexOf('/')) #extract resource group
    az aks get-credentials -g $aksresourcegroup -n $aksclustername #Set the context
    $pods = (kubectl get pods -A) #get pods
    $AksClusterAndPods.Add($aksclustername,$pods.Length) #add AKS clustername and number of pods


    Write-Host "AKS Cluster Names and number of Pods"
    foreach($key in $AksclusterAndPods.Keys)
    $output = '{0},{1}' -f $key , $AksclusterAndPods[$key]
    Write-Host $output

    0 comments No comments