top of page
Writer's pictureShubham Nikam

All about Python Kubernetes Client

Updated: Nov 12


Introduction to Kubernetes Client

Kubernetes, also known as K8s, is an open-source system for automating deployment, scaling, and management of containerized applications.

Kubernetes is an open source container orchestration engine for automating deployment, scaling, and management of containerized applications. The open source project is hosted by the Cloud Native Computing Foundation (CNCF).So if you are a developer and not familiar with kubernetes CLI, then the kubernetes client will help you to interact with kubernetes.There are several language specific kubernetes clients are available e.g. Python, Java, C#, Javascript etc. Let’s deep dive into the Python Kubernetes Client and understand it using the following steps and examples.


Step 1: Install kubernetes


Installing Kubernetes


pip install kubernetes​

Step 2: Import client and config from kubernetes in your python code


from kubernetes import client, config

Step 3: Load Kubernetes cluster Configuration


Configuring Kubernetes Client

try:
    config.load_incluster_config()
except config.ConfigException:
    try:
        config.load_kube_config()
    except config.ConfigException:
        raise Exception("Could not configure kubernetes python client")

Step 4: Interact with Kubernetes resources


In step 3 we have loaded kubernetes config so we are ready to perform different operations, or to get different resources of kubernetes using this kubernetes python client api’s.


1. Get Nodes


Getting nodes

        kubectl is the CLI for kubernetes, for getting nodes of clusters you have run commands. A node may be a virtual or physical machine, depending on the cluster. 


Command: kubectl get nodes

Above command will return present nodes in kubernetes cluster


To Get the same data using a python client we have to use the class CoreV1Api which we can get from a client that we imported from kubernetes as follows.


Using Client:​
  v1 = client.CoreV1Api()        
  v1.list_node()

2. Get Namespaces

      

Namespace in kubernetes is something like group in general term, so if we want to bind our pod, deployment, PV, PVC etc under one label or group, will have create a namespace and while creating each kubernetes resources that we mentioned earlier add –namespace your_namespace_name flag at the end of command.



Working with namespaces

Command: kubectl get namespaces

   Using Client:
   v1 = client.CoreV1Api()
   v1.list_namespace()

3. Get Pods in all Namespaces


Getting pods in all namespaces

        Pods are the smallest deployable units of computing that you can create and manage in Kubernetes. Pod is a set of containers with shared namespaces and shared file system volumes.


Command: kubectl get pods 

   Using Client:
   v1 = client.CoreV1Api()
   v1.list_pod_for_all_namespaces()

4. Get Pods in Specific Namespace

        For finding pods deployed under specific namespace we use below command and similarly we can do using python kubernetes client.


Get pods in specific namespace

Command: kubectl get pods –n your_namespace_here
        
   Using Client:
   v1 = client.CoreV1Api()
   pod_list  = v1.list_namespaced_pod(pod_namespace)
   pods = [pod.metadata.name + " " + pod.status.phase for pod in pod_list.items]

Note: Kubernetes resources (such as pod, deployment etc.) created without namespace are included under default namespace.


5. Create Pod with namespace


Creating a pod

Command: kubectl apply -f your_pod_yaml_file.yaml

   Using Client:
   with open(podYamlFilePath) as f:
           dep = yaml.safe_load(f)
           k8s_apps_v1 = client.CoreV1Api()
           resp = k8s_apps_v1.create_namespaced_pod(body=dep, namespace=pod_namespace)
           print("Deployment created. status='%s'" % resp.metadata.name)

6.Get pod Status


Getting Pod Status

        A Pod's status field is a PodStatus object, which has a phase field. The phase of a Pod is a simple, high-level summary of where the Pod is in its lifecycle. Pod status has 4 possible values Pending, Running, Succeeded, Failed and Unknown.


Note: After deleting a pod it shows terminating status by some kubectl commands, but this is not a pod phase.


 Command: kubectl describe pod pod_name --namespace your_pod_namespace

  Using Client:
  v1 = client.CoreV1Api()
  pod = v1.read_namespaced_pod(name=pod_name, namespace=pod_namespace)
  print(pod.status.phase)#it will print pod status

7. Delete pod with namespace


Deleting pod with namespace

Command:
    kubectl delete pod your_pod_name —namespace your_pod_namespace

   Using Client:
   v1 = client.CoreV1Api()
         api_response = v1.delete_namespaced_pod(pod_name, pod_namespace)
         print(api_response) 

For more information you can see official documentation of Python-Kubernets-Clientand for more examples Visit official kubernetes example folder.


Conclusion:


Kubernetes using Python client

Kubernetes client helps to interact with kubernetes cluster to perform different tasks and operations programmatically, without running CLI commands as we saw in our article, we can perform much more advanced things using kubernetes client and one of the great things is it is available in different programming languages.

References: 

24 views0 comments

Comments


bottom of page