0

I am working on automating certain tasks related to Azure Kubernetes.

For this, I want to connect to AKS to list pods and to get live logs which we get through kubectl.

However, when I import the azure module as follows

from azure.mgmt.containerservice import ContainerServiceClient

or

from azure.mgmt.kubernetesconfiguration import SourceControlConfigurationClient

It throws exception that

ModuleNotFoundError: No module named 'azure.mgmt'

I have properly installed this module in virtual env which gets listed on the pip3 list.

Is there any new way of working with AKS or container service?

Edit - Output of pip3 list is -

Package                            Version
---------------------------------- ---------
azure-common                       1.1.28
azure-core                         1.26.3
azure-identity                     1.12.0
azure-mgmt-core                    1.3.2
azure-mgmt-kubernetesconfiguration 2.0.0
Pallavi
  • 548
  • 2
  • 9
  • 18

3 Answers3

0

From the list i don't see the package,

you need to do

pip install azure-mgmt

You need to use the specific packages Starting with v5.0.0, in this case you need to install

pip install azure-mgmt-containerservice

here is the doc

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

I tried in my environment and got below results:

I installed package with azure-container-service with latest version by refering this document with my python version 3.10.4:

Command:

 pip install azure-mgmt-containerservice==21.1.0
 

After installed package in my environment and I tried the below code to get the list of pods it executed successfully.

Code:

from azure.identity import DefaultAzureCredential
from azure.mgmt.containerservice import ContainerServiceClient
import os
from kubernetes import client, config

credential = DefaultAzureCredential()
subscription_id = "<your subscription id>"
resource_group_name= 'your resource name'
cluster_name = "your cluster name" 
container_service_client = ContainerServiceClient(credential, subscription_id)

# getting kubeconfig in a decoded format from CredentialResult
kubeconfig = container_service_client.managed_clusters.list_cluster_user_credentials(resource_group_name, cluster_name).kubeconfigs[0].value.decode(encoding='UTF-8')
# writing generated kubeconfig in a file
f=open("kubeconfig","w")
f.write(kubeconfig)
f.close()
# loading the config file
config.load_kube_config('kubeconfig')
# deleting the kubeconfig file
os.remove('kubeconfig')
v1 = client.CoreV1Api()
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
    print("%s\t%s\t%s" % (i.status.pod_ip,i.metadata.namespace,i.metadata.name))

Output:enter image description here

ip address      namespace           name
10.244.x.x       default         azure-vote-back-7cd69cc96f-xdv79
10.244.x.x       default         azure-vote-front-7c95676c68-52582
10.224.x.x      kube-system     azure-ip-masq-agent-s6vlj
10.224.x.x      kube-system     cloud-node-manager-mccsv
10.244.x.x      kube-system     coredns-59b6bf8b4f-9nr5w

Reference: azure-samples-python-management/samples/containerservice at main · Azure-Samples/azure-samples-python-management · GitHub

Venkatesan
  • 3,748
  • 1
  • 3
  • 15
  • The problem was with Pycharm IDE. Import worked like charm in VS Code and my script is now working to get pods listed and getting live logs. Thanks! – Pallavi Feb 20 '23 at 09:05
0

The problem is solved for me for the time being i.e. I am no more seeing the error.

What I did? --> Used VS Code rather than Pycharm IDE where I was getting error.

Workaround or solution? --> This is workaround. i.e. I could manage to make it working for me and proceed with my implementation.

So problem seems to be with Pycharm IDE and not sure what's the solution for it.

Any suggestions to solve this Pycharm problem is most welcome. (I will mark that answer as accepted, in that case.)

Pallavi
  • 548
  • 2
  • 9
  • 18