DevOps
Kubernetes
Env-From-Secret

Uses Secret to set environment variables in a Deployment

Create a Secret

Create a Secret with the kubectl create secret command. The env key in the Pod spec references the Secret.

kubectl create secret generic test-secret --from-literal=username='my-app' --from-literal=password='39528$vdg7Jb'

Use the Secret in a Pod

The following Pod spec uses the Secret

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test
spec:
  replicas: 3
  selector:
    matchLabels:
      app: test
    spec:
      containers:
      - name: test
        image: ghcr.io/my-org/my-repo:latest
 
        envFrom:
        - secretRef:
            name: test-secret