How To Create Secrets Using Kubernetes Python Client

photo of turned on laptop computer
Reading Time: 3 minutes

Hello Readers !!! Hope you all are doing well . Today in this Blog we’ll see how to Create Secrets Using Kubernetes 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. 


A secret is a small piece of confidential data, such as a password, token, or key. Alternatively, such information might be include in a Pod specification or a container image. You don’t have to incorporate confidential data in your application code if you use a Secret.

There is less risk of the Secret (and its data) being expose during the workflow of generating, viewing, and updating Pods because Secrets can be establish independently of the Pods that use them. Secrets can also be handle differently by Kubernetes and the applications that run in your cluster, such as avoiding writing secret data to nonvolatile storage.

Secrets are similar to Config Maps, except they’re designe to retain sensitive information.


Before we move forward with creating secrets using K8s python client we have some prerequisites that we need to follow.

Kubernetes library provides us modules such as client and config which we will use here. So, let’s install Kubernetes Python Client:

$ pip install kubernetes

Now, we have the python-kubernetes package-installed.

So, let’s get start!!!

Creating the Secrets

Let’s How To Create Secrets Using Kubernetes Python Client. 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 secrets using Kubernetes Python Client. Now that we have the python-kubernetes package installed, we can import it as:

from kubernetes import client, config

Here is my Code for creating Secrets Using Kubernetes Python Client

def create_secret(data , string_data , client_api):

    secret = client.V1Secret(




        data=data ,


    api = client_api.create_namespaced_secret(namespace="default", 

    return api

api_server_endpoint = "Your_API"

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": "deekasha"


create_secret({} , cm , client_api)

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

$ python3

As you can see here my secrets named –> my-secret is successfully created.

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


Thank you for sticking to the end. In this blog we have seen how easily we can create secret using kubernetes python client. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs.


Thank You!!!

Written by 

Deeksha Tripathi is a Software Consultant at Knoldus Inc Software. She has a keen interest toward learning new technologies. Her practice area is DevOps. When not working, she will be busy in listening music , and spending time with her family .