Using create_namespaced_secret API in Kubernetes Python client
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Creating a Kubernetes Secret from Python is straightforward once you understand the shape of the API object and the difference between data and string_data. The create_namespaced_secret call writes a Secret into one namespace, so you need the correct client configuration, namespace, and RBAC permissions. The safest approach is to build a V1Secret explicitly and let the Kubernetes API validate the object.
Load Configuration and Create the API Client
The Kubernetes Python client can run either from your workstation or from inside a cluster.
Local config example:
In-cluster example:
Use load_kube_config() for scripts and local tooling, and load_incluster_config() when your Python code runs inside Kubernetes itself.
Build a Secret Object Correctly
The cleanest way to create a Secret is to use client.V1Secret.
You can provide raw strings with string_data and let the API server encode them.
This is easier and safer than manually base64-encoding the values yourself.
Create the Secret in a Namespace
Once the object is prepared, call create_namespaced_secret.
This creates the secret only in app-dev. Secrets are namespace-scoped, so a pod in another namespace cannot use it unless you create a separate copy there.
data Versus string_data
This distinction causes many first-time mistakes.
Use string_data when:
- you have normal Python strings
- you want Kubernetes to handle base64 encoding
Use data when:
- you already have base64-encoded values
- you are reproducing an exact manifest format
Manual data example:
If you use data, the values must already be base64-encoded strings.
Handle Existing Secrets Safely
create_namespaced_secret fails if the secret already exists. If your workflow is idempotent, catch that case and patch or replace instead.
For update flows, use replace_namespaced_secret or patch the secret instead of calling create repeatedly.
Verify the Secret
After creation, read it back to confirm metadata and keys.
Remember that stored.data contains base64-encoded values, because that is how the API represents secret data when reading it back.
RBAC and Operational Concerns
Secret creation is permission-sensitive. Your service account or user must have the right verbs on the secrets resource in the target namespace.
Minimal RBAC concept:
- '
get' - '
create' - optionally
updateorpatch
Also remember:
- secrets are only base64-encoded, not encrypted by default in the client response
- avoid printing secret values in logs
- prefer external secret managers when the environment requires stronger secret lifecycle controls
The API call is simple, but secret handling policy is not.
Common Pitfalls
One common mistake is manually base64-encoding values and then putting them into string_data. That causes double-encoding semantics and incorrect stored values.
Another mistake is creating the secret in the wrong namespace and then wondering why the pod cannot mount or reference it.
Developers also log the full secret object while debugging, which can leak credentials into build logs or monitoring systems.
Finally, repeated create calls without conflict handling make automation brittle. If the script may run twice, decide whether the correct behavior is fail, patch, or replace.
Summary
- Use
create_namespaced_secretwith aV1Secretobject for explicit, readable secret creation. - Prefer
string_datawhen you have normal strings and want Kubernetes to encode them. - Secrets are namespace-scoped, so choose the target namespace carefully.
- Handle
409 Conflictif your workflow may attempt to create an existing secret. - Treat secret creation as a security-sensitive operation, not just another CRUD call.

