Kubernetes β’ Config & Secrets
How to Create a Kubernetes Generic Secret from an .env File
`kubectl create secret generic` reads key-value pairs from standard `.env` configuration files, encodes each value in base64, and creates a Kubernetes Secret resource ready to be mounted or injected via `envFrom` into Pods.
Create Secret from .env File Command
Safe β’ Read-Only / Non-Destructive
kubectl create secret generic app-secrets --from-env-file=.env -n defaultCustomize:
Namespace:
Resource Name:
Flags & Parameter Breakdown
generic <secret-name>Name of the Secret to generate--from-env-file=<path>Path to local .env file containing secrets--dry-run=client -o yamlGenerate YAML manifest output without applying to clusterStep-by-Step Execution Workflow
1Prepare .env file
Ensure secret values are defined in KEY=VALUE pairs:
2Create secret in cluster
Execute the creation command:
kubectl create secret generic app-secrets --from-env-file=.env -n default3Generate YAML manifest for GitOps
Use dry-run mode to export clean YAML for version control (remember to encrypt with SealedSecrets or SOPS!):
kubectl create secret generic app-secrets --from-env-file=.env --dry-run=client -o yaml > secret.yamlCommon Pitfalls & Troubleshooting Advice
- Never commit unencrypted secret YAML files to public git repositories.
- If updating an existing secret, delete the old secret first or pipe with `kubectl create secret ... --dry-run=client -o yaml | kubectl apply -f -`.
Prerequisites & Cluster Access
- Valid local .env file with KEY=VALUE syntax.
- Permission to create secrets in the target namespace.
Frequently Asked Questions About Create Secret from .env File
Frequently Asked Questions
Frequently Asked Questions
Everything you need to know regarding specifications, syntax, and security best practices.
Add `envFrom: [ { secretRef: { name: "app-secrets" } } ]` under the container spec.
Related Kubernetes & DevOps Recipes
View All RecipesNetworking & Access
How to Port-Forward a Kubernetes Pod or Service to Localhost
port-forwardGuide β
Debug & Inspection
How to Exec into a Kubernetes Pod with an Interactive Bash Shell
execGuide β
Troubleshooting
How to Debug and Fix CrashLoopBackOff Pods in Kubernetes
logsGuide β
Troubleshooting
How to Force Delete a Kubernetes Pod Stuck in Terminating State
deleteGuide β
Deployments & Workloads
How to Restart a Kubernetes Deployment with Zero Downtime
rolloutGuide β
Monitoring & Resources
How to Check CPU and Memory Resource Usage of Pods and Nodes
topGuide β