pythonkubernetescommand-lineazure-akscommand-line-tool

Need a connection to Azure Kubernetes for my own CLI Tool


The following scenario

I have written my own CLI tool with python and want to establish a connection to the AKS. As I found out it is only possible with REST API (if there are other ways please let me know) does anyone have experience with this? how do I do this?

There are commands like to delete running pods. very basic things up to here.

In the future it would also be important to get an authentication via token to cover the security aspect.

I would be very happy about solutions and answers.

I have just basic experiences with AKS, thats why Im asking. And most of the users just use kubectl or other tools so there no many directions after googling it


Solution

  • To connect to AKS using a Python CLI, you can use Azure Management API, such as Azure SDK for Python (Azure-Mgmt-Containers) to work with the AKS API

    Make sure you have installed Azure SDK for Python, and the latest version of Azure CLI is configured.

    pip install azure-mgmt-containerservice
    

    enter image description here

    from azure.identity import DefaultAzureCredential
    from azure.mgmt.containerservice import ContainerServiceClient
    from azure.mgmt.containerservice.models import ManagedCluster
    
    #Authenticate
    credential = DefaultAzureCredential()
    
    #Specify your Azure subscription ID and resource group
    subscription_id = "your_subscription_id"
    resource_group = "your_resource_group"
    cluster_name = "your_cluster_name"
    
    #Create a ContainerServiceClient
    client = ContainerServiceClient(credential, subscription_id)
    
    #Get information about the AKS cluster
    cluster = client.managed_clusters.get(resource_group, cluster_name)
    
    print(f"AKS Cluster Name: {cluster.name}")
    print(f"AKS Cluster Location: {cluster.location}")
    print(f"AKS Cluster Kubernetes Version: {cluster.kubernetes_version}")
    

    Output: enter image description here

    Another approach is using kubeconfig file or bearer token as explained here by matt_j