Creating a Kubernetes Secret from an Env File

2021-10-11 18:25 (3 years ago) ytyng

When saving secret information locally in a file named .env and registering it with Kubernetes:

Execute the following script in the development environment during deployment.

# Set kubeconfig
export
KUBECONFIG=${HOME}/.kube/my-kube-config

# If a secret already exists, delete it (for the next registration)
kubectl -n mynamespace delete secret myapp-env
# Register the local ../.env as a secret
kubectl -n mynamespace create secret generic myapp-env --from-env-file=../.env

When using it

apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deployment
namespace: mynamespace
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: my-image:latest
imagePullPolicy: Always
ports:
- containerPort: 8008
env:
- name: DJANGO_SETTINGS_MODULE
value: myapp.settings.production
envFrom:
- secretRef:
name: myapp-env
Current rating: 4
The author runs the application development company Cyberneura.
We look forward to discussing your development needs.

Archive

2025
2024
2023
2022
2021
2020
2019
2018
2017
2016
2015
2014
2013
2012
2011