Kubernetes secrets are Kubernetes objects designed to hold small pieces of sensitive data such as passwords, tokens, API keys, transport layer security (TLS) certificates and SSH keys. They are a core part of Kubernetes secrets management and help secure cloud-native workloads by separating confidential data from container images, configuration files and plain text manifests.
A secret is an object in a Kubernetes cluster that stores secret data as key-value pairs, typically representing credentials, tokens or certificates needed by workloads. Kubernetes objects are persistent resources that define the wanted state of your cluster.
Kubernetes secrets allow you to store sensitive information separately from pods and application code, enhancing security. The Kubernetes API treats secrets as first‑class resources, enabling this process to work. With this approach, pods, deployments and other controllers can reference them without embedding sensitive information directly in the pod spec or hardcoding it into applications.
Kubernetes secrets are distinct from
Kubernetes, often called k8s or kube, is an open source platform for orchestrating containerized applications across distributed environments. It automates core operational tasks such as deploying applications, managing their lifecycle and scaling workloads as demand changes. By handling responsibilities like container scheduling and service discovery, Kubernetes provides a consistent framework for running applications reliably at scale. This action allows teams to focus on building and improving software rather than managing the underlying infrastructure.
Stay up to date on the most important—and intriguing—industry trends on AI, automation, data and beyond with the Think newsletter. See the IBM Privacy Statement.
Kubernetes secrets are stored in
The Kubernetes API server exposes secret objects through the Kubernetes API and only authenticated and authorized clients can access them based on role-based access control (RBAC) policies. Kubelet, the node agent, retrieves secret objects referenced by pods scheduled onto that node and makes them available to the container runtime.
This action allows them to be mounted into pods or injected as environment variables. This process lets pods use confidential data without directly embedding it in the pod spec or container images.
A secret is typically defined in a YAML file. This file can then be applied with a
In this example, the data values are Base64 encoded representations of the plain text values. However, the underlying semantics are just key-value pairs representing secret data. Alternatively,
Kubernetes supports several built-in types of secrets, each optimized for different use cases. The “type” field in the secret manifest helps the API server and tools validate the data structure and enforce constraints for each type.
Kubernetes provides options to create secret objects directly from the command line with kubectl. A common approach is to use “kubectl create secret generic” to generate an opaque secret from literals or files.
The next example presents how to create a generic secret from literal key-value pairs:
This command stores the provided literals as Base64 encoded secret keys in the
Secrets can also be created from existing files, which is especially useful when you have certificates or keys already stored as files on disk. To do this action, apply the method in this example:
We can see here that each file becomes a key in the secret data, with its contents Base64 encoded as the corresponding value.
You can inspect secrets by using
List all secrets in a namespace
View details about a specific secret
Check which keys exist inside a secret
Because secret data is stored as Base64 values, you must decode them to read the actual content.
Read a specific key
Read all keys in a secret
Export a secret to YAML
(Values will still be Base64‑encoded.)
After creation, workloads such as pods, deployments and “StatefulSets” reference secrets through the pod spec. This method will allow for secure injection of confidential data. There are two primary ways to consume a secret: as environment variables or as mounted volumes inside the container file system.
To use secret values as environment variables, you can use env and valueFrom with secretKeyRef in the pod spec:
This pod uses environment variables to inject secret data into the application process, avoiding plain text values in the YAML or container image.
Another method you can apply is mounting a secret as a volume within the container file system:
In this instance, each key in the secret data is represented as a file under the
Kubernetes uses role-based access control (RBAC) to constrain which users and service accounts can read or modify secret objects within a namespace. RBAC roles and role bindings specify authorized operations such as get, list and update on secret resources. Following a least-privilege model is recommended to minimize exposure of sensitive data.
Secrets are namespaced resources, meaning each secret resides in a specific namespace and is accessible only to workloads and users with permissions in that namespace. Anyone authorized to create pods in a namespace can potentially configure a pod to reference any secret in that namespace. This access means that controlling who can create or update workloads is a critical part of Kubernetes secrets management.
Service accounts provide identities for pods and are often associated with specific permissions to access secrets. Service account tokens themselves can be represented as secrets and Kubernetes automatically mounts them into pods, which makes it essential to carefully manage service account permissions.
Kubernetes supports encrypting secret data at rest by enabling encryption providers configured on the API server, which ensures that secret data is encrypted before being written to
Storing database usernames and passwords for applications running in pods.
Managing API keys for external services or internal microservices.
Handling TLS certificates and private keys for Ingress controllers or backend services.
Providing SSH keys for maintenance operations or application-level SSH authentication.
Configuring credentials for Docker registries to pull private images.
Managing secrets in Kubernetes involves securing them throughout their entire lifecycle. By lifecycle this means creation, storage, access, rotation and deletion. Kubernetes provides several built‑in mechanisms to help you protect sensitive data and the official documentation offers detailed guidance on these practices. The next steps are the optimal method to manage Kubernetes secrets:
Optimize your cloud with unified lifecycle automation—secure, scalable hybrid infrastructure designed for resilience and AI.
Safeguard your hybrid-cloud and AI environments with intelligent, automated protection across data, identity, and threats.
Protect and manage user access with automated identity controls and risk-based governance across hybrid-cloud environments.
“Kubernetes.” IBM Think, 31 January 2024
“Secrets.” Kubernetes Documentation, 19 November 2024