How To Create Config-Map Using K8s Python Client?

close up photo of programming of codes
Reading Time: 3 minutes

Hello Folks… I hope you all are doing well. In this blog we will see how to create config map using k8s python client? As we all know that generally we use kubectl commands for creating, listing, updating, deleting the kubernetes resources.

But In this blog we will see how we can use python for doing these things with resources. So stay tuned with me.

What Is Config-map ?

Config map is kubernetes object which allows you to separate your configuration from your pods and components as a result it keeps your containers portable and makes the configuration easier to change and manage and prevents the hard coding configuration data into pod spec.

So config map stores the configuration data as a key value pair for example if you passing any configuration in files then the name of the file is going to be key and content of that file is going to be the value.

You can also pass key value page straight from the command line or as a environment variable. So the help of config maps in kubernetes you can manage configuration of containers.

Installing Python Kubernetes Client :

Before we start creating config maps using kubernetes python client. We need to follow some prerequisites like:

$ pip install kubernetes

The kubernetes python client is successfully installed now.

So; now let’s see how we can create a config maps via kubernetes python client.

Creating Config Maps :

First I will create a directory and inside this I will make a file named

cd Desktop  
mkdir k8s 
cd k8s  

My file contains the following code for creating a Config Maps using Kubernetes Python Client.

Now that we have the kubernetes package installed, we can import it as:

from kubernetes import client, config

Code for creating Config Maps via python client :

from kubernetes import client, config

def create_configmap(data , string_data , client_api):
    secret = client.V1Secret(
        data=data , 

    api = client_api.create_namespaced_config_map(namespace="default", body=secret)
    return api

api_server_endpoint = "Your_Token"
bearer_token = "Your_Token"

configuration = client.Configuration() = api_server_endpoint
configuration.verify_ssl = False
configuration.api_key = {"authorization": "Bearer " + bearer_token}
client_api = client.CoreV1Api()

    "name": "mayuri"
create_configmap({} , cm , client_api)

Now, It’s time to create the secrets. So, I will now run the python code.

$ python3

Now; if i check my configmap is created or not so i will simply run the command :

kubectl get configmap

As you can see here my config map named –> my-config-map is successfully created.

So, We are successfully done now. This is how we can play with the Kubernetes python client.

Conclusion :

So; in this blog we have seen how easily we created config-maps using kubernetes python client. I hope this blog will help you. Thank you for sticking to the end.

Happy Learning..!!!

Reference :

Written by 

Mayuri Dhote is a Software Consultant at Knoldus Software. She has completed her MCA from VIT University. She has very dedicated towards her work. She has always ready to learn new things. Her practice area is Devops. When not working, you will find her writing poems and poetry.