When to use Secrets as opposed to ConfigMaps in Kubernetes?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Kubernetes is a powerful orchestration tool that supports containerization, offering various options for managing configuration data for applications. Two primary objects are available for this purpose: ConfigMaps and Secrets. Understanding when to use Secrets as opposed to ConfigMaps is crucial for managing sensitive data and configurations effectively within a Kubernetes cluster.
ConfigMaps vs. Secrets: An Overview
ConfigMaps
ConfigMaps are used to decouple configuration artifacts from the application code. They store data as key-value pairs which can be used to configure other Kubernetes resources like Pods, Deployments, and more. ConfigMaps are useful for non-sensitive configurations, such as environmental variables, command-line arguments, or configuration files.
Secrets
Secrets, on the other hand, provide a mechanism for storing sensitive information, like passwords, OAuth tokens, or SSH keys. Similar to ConfigMaps, Secrets store data as key-value pairs but with an additional level of base64 encoding and stricter access policies, enhancing data security at rest.
When to Use Secrets Instead of ConfigMaps
Storing Sensitive Data
The primary use case for Secrets is to store sensitive data. Passwords, API keys, TLS certificates, and other confidential data should be stored as Secrets to provide better security controls and options, such as encryption at rest.
Enhancing Security
Secrets in Kubernetes have more secure default configurations. They are designed with immutable storage in mind and can be encrypted using a key management service (KMS), providing an extra layer of data protection compared to ConfigMaps which are stored as plain text.
Fine-Grained Access Control
In Kubernetes, access to different resources can be controlled using Role-Based Access Control (RBAC). For Secrets, this becomes particularly important. You can assign distinct policies to Secrets, ensuring only certain groups or services can access the sensitive data.
Limiting Base64 Encoding
While ConfigMaps are stored as plain text, Secrets are base64 encoded by default. Although base64 encoding is not encryption, it helps prevent accidental exposure in logs and user interfaces.
When ConfigMaps Suffice
Non-Sensitive Data
For application configurations that do not contain sensitive information, ConfigMaps are sufficient. Examples include setting environmental variables such as configName or versionNumber, or providing other metadata settings.
Application Defaults
Default application config values that are subject to frequent changes should be stored in ConfigMaps for ease of updates without changing code.
Performance Optimization
Since ConfigMaps are treated as plain text, they are slightly faster to deploy and access compared to Secrets. This difference can be minute but may matter in high-performance scenarios involving large amounts of configuration data.
Key Differences
Below is a table summarizing the key differences between ConfigMaps and Secrets:
| Aspect | ConfigMap | Secret |
| Purpose | Non-sensitive configuration | Sensitive data storage |
| Data Format | Plain text | Base64 encoded |
| Access Control | Basic RBAC | Enhanced RBAC |
| Security | No encryption by default | Can be encrypted using KMS |
| Use Case | App config, Metadata | Passwords, API keys, OAuth tokens |
| Performance | Slightly faster deployment | Slower, due to additional encoding |
| Versioning | Yes (if created as immutable) | Yes (if created as immutable) |
| Data Volume | Config data, Properties | Secrets, TLS certificates |
| Risk | Accidental exposure in logs/UI | Base64 encoding reduces accidental exposure |
Conclusion
The choice between Secrets and ConfigMaps should primarily hinge on the sensitivity of the information being stored. While ConfigMaps provide a convenient mechanism for managing and deploying non-sensitive application configurations, Secrets are designed to manage and protect sensitive data, enhancing security through encryption and restricted access mechanisms. Understanding these differences and applying the appropriate resource ensures both security and efficiency within a Kubernetes architecture, creating a robust infrastructure for containerized workloads.
In practice, a well-balanced application often utilizes both ConfigMaps and Secrets, depending on the needs for security and configuration management.
Related reading
- Where are helm charts stored locally?
- Where are the Kubernetes kubelet logs located?
- Where can I get a list of Kubernetes API resources and subresources?
- Where helm stores its data on a cluster
- When to use the different log levels
- When to use writer.flush in Tensorboard
- When using Spring Security, what is the proper way to obtain current username (i.e. SecurityContext) information in a bean?
- When using Trusted_Connectiontrue and SQL Server authentication, will this affect performance?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.