cancel
Showing results for 
Search instead for 
Did you mean: 

Quick Intro

In this article, we continue our exploration of Kubernetes API but this time we're going to use Python along with Wireshark.

I strongly advise you to go through Exploring Kubernetes API using Wireshark part 1: Creating, Listing and Deleting Pods and Exploring Kubernetes API using Wireshark part 2: Namespaces first.

We're not creating any app here. I'll just show you how you can explore Kubernetes API using Python's client API and Wireshark output retrieved from kubectl get pods command:

0151T000003kTv8QAE.png

In case you want to follow along, I've set up gcloud tool as I'm using Google Cloud and GOOGLE_APPLICATION_CREDENTIALS environment variable on my Mac so I had no problem authenticating to Kubernetes API.

I hope you understand that when I say Kubernetes API, I'm talking about the API on Kubernetes Master node on my Google Cloud's Kubernetes cluster.

What I'm going to do here

In this article, we're going to do the following (using Python):

  • Authenticate to kube API
  • Connect to /api/v1 and then move to /api/v1/namespaces/default/pods
  • Add pod information to a variable named pods
  • from variable pods, create a list of:
  • pod names
  • pod status
  • At the end, we display the above info 

Authenticating/Authorising your code to contact Kube API

Here we're loading auth and cluster info from kube-config file and storing into into our client API config:

0151T000003kTvDQAU.png

Connecting to /api/v1

We now store into v1 variable the API object. For the sake of simplicity, think of it as we were pointing to /api/v1/ folder and we haven't decided where to go yet:

0151T000003kTvIQAU.png

Connecting to /api/v1/namespaces/default/pods

We now move to /api/v1/namespaces/default/pods which where we find information about pods that belong to default namespace (watch=False just means we will not be monitoring for changes, we're just retrieving it as one-off thing):

0151T000003kTvNQAU.png

Now, what we've stored in the above variable (ret) is the equivalent of moving to the root directory in our JSON tree (Object) in the output below:

0151T000003kTvSQAU.png

The output above was from kubectl get pods command which lists all pods from default namespace and that's equivalent to what we're doing here using Python's Kubernetes Client API.

Python shows us similar options about where to go when I type ret. and hit tab:

0151T000003kTvXQAU.png

We've got kindapiVersionmetadata and items as options to move to but items is what we're looking for because it's supposed to store pod's information.

0151T000003kTvcQAE.png

So let's move to items and store it in another variable called pods to make it easier to remember that it actually holds all pods' information from default namespace:

0151T000003kTvhQAE.png

Listing pods' names

We're now in a comfortable place and ready to play!

Let's keep pods variable as our placeholder!

On Wireshark, pod's name is located in metadata.name:

0151T000003kTvmQAE.png\

So let's create another variable called pods_names to store all pods' names:

0151T000003kTvrQAE.png

Listing Pods' status (phase)

What we're looking for is in status.phase:

0151T000003kTv9QAE.png

Let's store it in status variable in Python:

0151T000003kTvdQAE.png

Displaying the output

We can now display the output using built-in zip() method:

0151T000003kTvnQAE.png

If you want something pretty you can use prettytable:

0151T000003kTvTQAU.png

Version history
Last update:
‎08-Jul-2019 01:31
Updated by:
Contributors