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.
Introduction
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.
Prerequisites
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 create.py.
cd Desktop
mkdir k8s
cd k8s
touch create.py

My create.py 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(
api_version="v1",
kind="Secret",
metadata=client.V1ObjectMeta(name="my-secret"),
data=data ,
string_data=string_data
)
api = client_api.create_namespaced_secret(namespace="default",
body=secret)
return api
api_server_endpoint = "Your_API"
bearer_token = "Your_Token"
configuration = client.Configuration()
configuration.host = api_server_endpoint
configuration.verify_ssl = False
configuration.api_key = {"authorization": "Bearer " + bearer_token}
client.Configuration.set_default(configuration)
client_api = client.CoreV1Api()
cm={
"name": "deekasha"
}
create_secret({} , cm , client_api)
Now, It’s time to create the secrets. So, I will now run the python code.
$ python3 create.py

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.
Conclusion
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.
HAPPY LEARNING!!!
Thank You!!!