How to set secret files to kubernetes secrets by yaml?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In Kubernetes, secrets are used to manage sensitive information such as passwords, OAuth tokens, and SSH keys. They help to keep your application secure by avoiding the exposure of sensitive data in configuration files or code. In this article, we'll delve into how to set secrets in Kubernetes using YAML.
Understanding Kubernetes Secrets
Kubernetes secrets are objects designed to hold sensitive data, such as a token, password, or key. The platform provides this mechanism to ensure that such sensitive information does not appear in your application's pod specs in plain text.
Why Use Kubernetes Secrets?
- Security: Secrets are stored in a more secure manner as compared to ConfigMaps. The data in secrets can be encrypted at rest.
- Separation of Concerns: Secrets allow you to separate sensitive data from application code.
- Flexibility: Kubernetes secrets can dynamically inject data into containers, allowing for greater flexibility.
- Access Control: Access to secrets can be fine-tuned using RBAC (Role-Based Access Control).
Secrets in YAML
Kubernetes secrets can be created in two ways:
- Imperatively using
kubectlcommands. - Declaratively using YAML manifest files.
In this guide, we focus on the latter approach.
Basic YAML Structure of a Secret
Here's a basic template for defining a secret in YAML:
Example: Creating a Secret YAML File
Suppose you want to store a database username and password as secrets. First, you must encode these values in Base64 format. Kubernetes secrets expect Base64-encoded data.
Encode Data in Base64
If your username is admin and password is P@ssw0rd, you can encode them using Base64 encoding:
Create the Secret YAML File
Using the Base64 encoded values, you can create the YAML manifest:
Applying the Secret to the Cluster
To create a secret in your Kubernetes cluster from your YAML file, use the kubectl command:
Using Secrets in Pods
Once the secret is created, you can use it in your pods. Mounting secrets as environment variables is a common practice.
Example Pod Spec
Here’s an example of how to mount a secret as an environment variable inside a pod:
In this scenario, the DB_USERNAME and DB_PASSWORD environment variables are automatically populated with the data from the db-secret secret.
Additional Considerations
Secret Management Strategy
- Version Control: Avoid including secrets in your source code repository. Use a dedicated secret management solution, or access control mechanisms.
- Encryption at Rest: Ensure that secrets stored in etcd are encrypted to provide an additional layer of security.
- RBAC and Access Control: Implement fine-grained access controls to ensure that only authorized entities can access secret data.
Limitations and Tips
- Size Limit: Each secret can have a maximum size limit of 1MB.
- Base64 Encoding: Always remember to encode your secret data using Base64 before inserting it into your YAML definitions. Failing to do so could result in errors.
Conclusion
Kubernetes secrets provide a secure, flexible, and organized method for storing and managing sensitive data. With proper management practices such as encryption, RBAC, and not storing sensitive information in version control, Kubernetes secrets can significantly enhance the security posture of your containerized applications.
Summary Table
| Feature | Details |
| Storage Type | Opaque, TLS, DockerConfig, etc. |
| Access Control | Uses Kubernetes RBAC |
| Data Format | Base64 encoded values |
| Mount Options | Environment Variables Volume Mounts |
| Size Limit | 1MB per secret |
| Security Feature | Can be encrypted at rest with etcd |
Related reading
- How to set the workdir of a container launched by Kubernetes
- How to set user name in container of kubernetes pod?
- how to setup basic rabbitmq on kubernetes
- How to setup Kubernetes NLB Load Balancer with target group IP based AWS?
- How to set the time zone in Amazon EC2?
- How to set up Spring Boot and log4j2 properly?
- How to share a file from initContainer to base container in Kubernetes
- How to share an EFS volume across multiple namespaces

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.