.env · generator · Kubernetes

Kubernetes .env Generator

Kubernetes separates config into two primitives: ConfigMaps (non-sensitive) and Secrets (sensitive). This generator produces a .env you can feed directly into kubectl create secret generic --from-env-file or split into a ConfigMap.

.env · Kubernetes · 7 keys
Customize →
APP_NAMESPACE=default
APP_REPLICAS=2
DATABASE_URL=postgres://...svc.cluster.local/app
REDIS_URL=redis://redis.default.svc.cluster.local:6379
JWT_SECRET=jYj1UxXHMLejqsEt8qbgJJOksdUPSV9Me0vLYyqPbnNOkW3T_y8WBxsz3CNnaLGu
LOG_LEVEL=info
METRICS_PORT=9090

Secrets regenerate on every page load. Want to combine stacks? Use the full generator.

How to use this .env in Kubernetes

  1. 1.Save the output as app.env.
  2. 2.Secrets: kubectl create secret generic app-secrets --from-env-file=app.env -n default.
  3. 3.Non-secret config goes into a ConfigMap: kubectl create configmap app-config --from-env-file=app.env.
  4. 4.For declarative setups, use the envFrom: field in your Deployment spec.

Gotchas

  • ⚠Split secrets from config at creation time — everything in a Secret is base64-encoded (not encrypted by default).
  • ⚠Use External Secrets Operator or sealed-secrets for production — raw Secrets in git are a leak risk.
  • ⚠envFrom: inlines every key; use env: with valueFrom: for selective mounting.

Common keys explained

APP_NAMESPACE

Kubernetes namespace for the deployment.

APP_REPLICAS

Replica count for the Deployment.

DATABASE_URL

Internal cluster DNS for the DB service.

REDIS_URL

Internal cluster DNS for Redis.

JWT_SECRET

Goes in the Secret, not the ConfigMap.

METRICS_PORT

Port the Prometheus sidecar scrapes.

Loading and validating the .env in Kubernetes

deployment.yaml
# kubectl create secret generic app-env --from-env-file=.env
# kubectl create configmap app-config --from-literal=LOG_LEVEL=info
apiVersion: apps/v1
kind: Deployment
metadata: { name: app }
spec:
  template:
    spec:
      containers:
        - name: app
          image: ghcr.io/acme/app:1.4.2
          envFrom:
            - configMapRef: { name: app-config }   # non-secret settings
            - secretRef: { name: app-env }         # every key in the Secret
          env:
            - name: POD_NAME
              valueFrom: { fieldRef: { fieldPath: metadata.name } }

Kubernetes: env vars in development, staging and production

Kubernetes splits configuration into ConfigMaps (plain settings) and Secrets (credentials). A Secret is only base64-encoded, not encrypted. Enable encryption at rest for etcd, limit get/list on Secrets with RBAC, and keep Secret manifests out of git (use Sealed Secrets, SOPS or External Secrets Operator).

Environment variables are read when the container starts. Updating a Secret or ConfigMap does not change the env of running pods. Restart with kubectl rollout restart deployment/app, or mount the Secret as a volume if the app can reload files.

Kubernetes: .env errors and fixes

CreateContainerConfigError: secret "app-env" not found

The Secret lives in another namespace or was never created. Secrets are namespaced: create it with -n <namespace>, matching the Deployment.

Pod still has the old value after kubectl apply

Env is fixed at container start. Run kubectl rollout restart, or add a checksum annotation of the Secret to the pod template so changes trigger a rollout.

Invalid value: "DB-HOST": a valid environment variable name must consist of alphabetic characters, digits, '_', '-', or '.'

Use UPPER_SNAKE_CASE keys in .env before running --from-env-file. Many apps cannot read names that contain dashes anyway.

Kubernetes .env FAQ

How do I turn a .env file into a Kubernetes Secret?

kubectl create secret generic app-env --from-env-file=.env --dry-run=client -o yaml gives you the manifest. Use the envFrom.secretRef shown above to load all keys at once.

Are Kubernetes Secrets encrypted?

Not by default. Values are base64-encoded in etcd. Turn on EncryptionConfiguration (managed clusters such as GKE, EKS and AKS offer KMS encryption) and restrict access with RBAC.

envFrom or env with valueFrom?

envFrom imports every key of a ConfigMap or Secret. env with valueFrom picks individual keys and can rename them. Use envFrom for app config and valueFrom when you only need one or two keys.

Related tools

Other stacks