Kubernetes Secrets: A Complete Guide with Code Examples
Managing sensitive data in Kubernetes is critical. Whether it’s API keys, database credentials, or TLS certificates, Kubernetes Secrets provide a secure way to store and manage sensitive information.
By the end of this guide, you’ll understand:
✅ What Kubernetes Secrets are and why they matter
✅ How to create, manage, and secure Kubernetes Secrets
✅ Different types of Secrets and when to use them
✅ Best practices for protecting sensitive data in Kubernetes
Let’s get started! 🚀
1. What Are Kubernetes Secrets?
Kubernetes Secrets are objects designed to store sensitive information, such as:
- 🔑 API Keys
- 🗝 Passwords
- 🔒 TLS Certificates
- 🏦 Database Credentials
Secrets prevent hardcoding sensitive data in ConfigMaps, YAML files, or environment variables.
Why Use Secrets Instead of ConfigMaps?
Feature | ConfigMaps | Secrets |
---|---|---|
Data Type | Plaintext | Base64-encoded (not encrypted) |
Used For | App configs | Sensitive data |
Access Control | Standard | More restrictive |
Mounted As | Volumes or Env Vars | Volumes or Env Vars |
Security Level | Low | Medium (requires extra security measures) |
2. Creating and Managing Kubernetes Secrets
2.1 Creating a Secret from a File
First, create a plaintext file:
|
|
Now, create a Kubernetes Secret:
|
|
Verify:
|
|
2.2 Creating a Secret from Key-Value Pairs
|
|
Retrieve it:
|
|
Note: Data is base64-encoded, NOT encrypted!
Decode the password:
|
|
2.3 Creating a Secret Using a YAML File
Create secret.yaml
:
|
|
Apply it:
|
|
3. Using Kubernetes Secrets in Pods
3.1 Using Secrets as Environment Variables
Modify pod.yaml
:
|
|
Apply:
|
|
3.2 Mounting Secrets as Volumes
Modify pod-volume.yaml
:
|
|
Apply:
|
|
Access secrets inside the pod:
|
|
4. Securing Kubernetes Secrets
4.1 Enabling Role-Based Access Control (RBAC)
Create an RBAC policy to restrict Secret access:
|
|
Create a RoleBinding:
|
|
Apply:
|
|
4.2 Encrypting Secrets at Rest
By default, Secrets are stored unencrypted in etcd. Enable encryption at rest:
Modify encryption-config.yaml
:
|
|
Apply:
|
|
4.3 Using External Secret Management Systems
Use HashiCorp Vault or AWS Secrets Manager for added security.
Vault Integration Example
|
|
Retrieve secret:
|
|
5. Best Practices for Kubernetes Secrets
✅ Use RBAC to restrict access to Secrets
✅ Enable encryption at rest for Secrets
✅ Avoid storing Secrets in ConfigMaps or environment variables
✅ Use a Secret Management System (Vault, AWS Secrets Manager)
✅ Monitor access to Secrets using audit logs
✅ Regularly rotate Secrets and enforce expiration policies
Final Thoughts
Kubernetes Secrets help manage sensitive data securely, but they must be properly secured.
Key Takeaways
✅ Secrets prevent hardcoding sensitive data in Pods
✅ Use RBAC and encryption to protect Secrets
✅ External secret managers enhance security
✅ Monitor and audit Secret access in production
With proper management, Kubernetes Secrets enhance security while keeping configurations manageable. 🚀