Course contentsShow
← Cloud and Infrastructure
Lesson 1265 of 2,98925. Kubernetes Configuration and SecretsFree lesson

Consuming Secrets as Environment Variables

Inject Secret data into container environment variables for application configuration.

Consuming Secrets as Environment Variables

What you'll learn: How to inject Secret data directly into your container's environment variables so applications can access sensitive configuration at runtime.

The Core Idea

Once you've created a Secret in Kubernetes, your application needs a way to actually use that secret data. One of the simplest methods is injecting Secret values as environment variables inside your container. When the container starts, it sees these secrets as regular environment variables—just like HOME or PATH—but the values come from the Secret object instead of being hardcoded in your manifest.

Think of it like receiving a sealed envelope (the Secret) and having someone automatically open it and pin the contents to your bulletin board (environment variables) before you arrive at your desk. You don't handle the envelope yourself; you just read what's already there.

How It Works

You reference the Secret in your Pod or Deployment spec using env or envFrom fields:

apiVersion: v1
kind: Pod
metadata:
  name: secret-env-pod
spec:
  containers:
  - name: myapp
    image: myapp:1.0
    env:
    - name: DB_PASSWORD
      valueFrom:
        secretKeyRef:
          name: db-credentials
          key: password

Here, the password key from the db-credentials Secret becomes the DB_PASSWORD environment variable inside the container. Your application code reads DB_PASSWORD normally—it doesn't know or care that Kubernetes injected it from a Secret.

You can also inject all keys from a Secret at once using envFrom, though individual env entries give you more control over naming.

Key Takeaway: Consuming Secrets as environment variables lets your applications access sensitive data without hardcoding credentials, using standard environment variable access patterns your code already understands.