Is it possible to source a .env file to create Kubernetes secrets?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Yes, a .env file can be the source for a Kubernetes Secret, but you usually do not want to source it first. kubectl can already read env-style key-value pairs directly, which avoids unnecessary shell expansion and keeps the workflow simpler.
That distinction matters because source .env changes your current shell state. In automation and CI pipelines, that can leak variables into later steps in ways that are easy to miss.
Use --from-env-file First
The most direct solution is kubectl create secret generic with --from-env-file.
If .env contains content such as this:
then Kubernetes creates a secret with one entry per line. This is usually the best answer to the question because it skips shell quoting issues and avoids leaking values into your interactive environment.
Generate YAML Instead of Creating Immediately
Many teams prefer declarative manifests instead of one-off imperative commands. You can still use the env file as the source and ask kubectl to emit YAML.
That command prints a Secret manifest to standard output. If you want to apply it directly, pipe it into kubectl apply.
This keeps the source of truth in the .env file while still fitting into a manifest-driven workflow.
When source Makes Sense
You can source the file first, but it is usually a fallback rather than the best default. One case is when you need to transform or combine values before creating the secret.
This works, but it has drawbacks. The variables now exist in your shell environment, shell syntax inside the file matters, and accidental expansion can create hard-to-debug mistakes. For a plain key-value file, --from-env-file is cleaner.
Remember What a Kubernetes Secret Is
Kubernetes stores secret values as base64-encoded data, not as magically encrypted content. Whether that is sufficient depends on your cluster configuration, access controls, and whether you have encryption at rest enabled.
Using a .env file is therefore only one part of the story. You still need to treat the original file, the generated manifest, and any logs that mention these values as sensitive material.
Keep the Env File Format Simple
--from-env-file works best with plain KEY=value lines. Comments and blank lines are generally fine, but shell-heavy constructs, command substitution, and complex quoting rules are better avoided. If the file behaves like a shell script, use a real template step instead of assuming kubectl will interpret it the same way.
Reference the Secret in a Pod
After creating the secret, mount it into a workload or expose selected keys as environment variables.
This is the point of the conversion: keep sensitive configuration in Kubernetes and let workloads consume it through normal cluster primitives.
Common Pitfalls
- Assuming a
.envfile is encrypted. It is not; it is only a text format. - Committing
.envfiles or generated secret YAML into version control. - Expecting every shell-oriented
.envfeature to work with--from-env-file. Keep the file in simpleKEY=valueform. - Forgetting that changing the
.envfile does nothing until you recreate or reapply the Kubernetes secret.
Summary
- Yes, you can use a
.envfile to create a Kubernetes secret. - In most cases, prefer
kubectl create secret generic --from-env-file=.envoversource .env. - Use
--dry-run=client -o yamlwhen you want a manifest-based workflow. - Only
sourcethe file when you truly need shell-side transformation or composition. - Treat both the
.envfile and the generated secret data as sensitive material.
Related reading
- Is it possible to stop nodes in AWS ElastiCache cluster
- Is it possible to use a bash script to do the liveness test in pod?
- Is it possible to use Istio without kubernetes or docker?
- Is it recommended to run clustered database with Kubernetes in production environment?
- Is it possible to start a shell session in a running container without ssh
- Is it possible to use AWS as a web host?
- Is it recommended to use kustomize after helm?
- Is kubectl top the current memory / CPU value?

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.