How to create a Kubernetes Secret from a file
Introduction
A Kubernetes Secret is used to store sensitive data (like passwords, tokens, or keys) securely. Creating a Secret from a file is useful when you already have the secret value saved in a file (example: db_password.txt) and you want Kubernetes to manage it and provide it to Pods when needed.
Prerequisites
Before running the commands, ensure you have:
- A running Kubernetes cluster (Minikube / Kind / EKS / AKS / GKE)
kubectlinstalled and connected to the cluster- The file
db_password.txtavailable in your current directory - Access/permission to create Secrets in the target namespace
Steps (Create Secret from a File)
- Verify the file exists
ls -l db_password.txt
- Create the secret from the file
kubectl create secret generic db-secret --from-file=db_password.txt
- Confirm the secret is created
kubectl get secret db-secret
- View secret details (value is not shown)
kubectl describe secret db-secret
Conclusion
By creating a Secret from a file, you keep sensitive information out of your application code and configuration files. Kubernetes stores the file content securely and you can mount it into Pods when required, making deployments safer and easier to manage.
