Python examples for fetching pod details using Azure AKS SDK and kubernetes python

Rajesh Swarnkar 861 Reputation points
2023-01-17T13:29:56.5033333+00:00

Hi,

I am trying to fetch the the pods CPU and and Memory Limits details using Python SDK for an Azure Kubernetes Cluster (AKS Cluster). Any Example would be very helpful.

I already have setup Python packages: azure-identity, azure-common, azure-mgmt-hybridkubernetes, azure-mgmt-kubernetesconfiguration .

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.
2,146 questions
0 comments No comments
{count} votes

Accepted answer
  1. Akram Kathimi 1,046 Reputation points Microsoft Employee
    2023-01-24T09:48:50.6+00:00

    Hi @Rajesh Swarnkar ,

    The Azure Python SDK, is used to manage the cluster. For fetching the pods' info you would need to use the Kubernetes package.

    For example:

    
    from kubernetes import client, config
    
    config.load_kube_config()
    
    
    v1 = client.CoreV1Api()
    
    pods = v1.list_pod_for_all_namespaces().items
    # Get the CPU and memory limits for each pod
    for pod in pods:
        print(f'Pod name: {pod.metadata.name}')
        for container in pod.spec.containers:
            print(f'\tContainer name: {container.name}')
            if container.resources.limits:
                if 'cpu' in container.resources.limits:
                    print(f'\tCPU limit: {container.resources.limits["cpu"]}')
                else:
                    print("\tNo cpu resource limits defined")
                if 'memory' in container.resources.limits:
                    print(f'\tMemory limit: {container.resources.limits["memory"]}')
                else:
                    print("\tNo memory resource limits defined")
            else:
               print("\tNo resource limits defined") 
                
    
    

    I 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.

0 additional answers

Sort by: Most 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.