> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Configuring Environment Variables in Applications

> Explains how to set and manage environment variables for Kubernetes containers using env, envFrom, ConfigMaps and Secrets with examples and security guidance

In this lesson you'll learn how to set environment variables for containers running in Kubernetes and how that maps from the typical Docker `-e` pattern.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/gJFNfF5mIbzp1GWe/images/Certified-Kubernetes-Administrator-CKA/Application-Lifecycle-Management/Configuring-Environment-Variables-in-Applications/environment-variables-slide-code-icon.jpg?fit=max&auto=format&n=gJFNfF5mIbzp1GWe&q=85&s=e1db72bec3966c8d1284724fb016098b" alt="A presentation slide titled &#x22;Environment Variables&#x22; with a code/HTML icon on a dark blue curved panel to the right. The slide has a clean white background and a small &#x22;Copyright KodeKloud&#x22; note in the corner." width="1920" height="1080" data-path="images/Certified-Kubernetes-Administrator-CKA/Application-Lifecycle-Management/Configuring-Environment-Variables-in-Applications/environment-variables-slide-code-icon.jpg" />
</Frame>

## Quick comparison: Docker vs Kubernetes

When running a container locally with Docker you can pass environment variables using `-e`:

```bash theme={null}
# Pass APP_COLOR to a container with Docker
docker run -e APP_COLOR=pink simple-webapp-color
```

In Kubernetes, you configure environment variables inside the container spec of a Pod, Deployment, or other workload manifest using the `env` (or `envFrom`) fields.

<Callout icon="lightbulb" color="#1CB2FE">
  Use `env` for individual variables (static or referenced), and `envFrom` to import all keys from a ConfigMap or Secret. The same `env` configuration works inside any workload that has a `containers` spec (Pod, Deployment, ReplicaSet, etc.).
</Callout>

## Basic example — key/value in a Pod manifest

Add an `env` array to the container spec. Each item uses `name` and `value`. This example shows a direct, static environment variable:

```yaml theme={null}
apiVersion: v1
kind: Pod
metadata:
  name: simple-webapp-color
spec:
  containers:
  - name: simple-webapp-color
    image: simple-webapp-color
    ports:
    - containerPort: 8080
    env:
    - name: APP_COLOR
      value: "pink"
```

Notes:

* `env` is an array, so each environment variable is prefixed with `-`.
* The same `env` entries can be put into a `containers` entry inside a Deployment manifest.

## Referencing ConfigMaps and Secrets

Instead of hardcoding values, reference keys from a ConfigMap or Secret using `valueFrom`. This is the preferred pattern for separating configuration and secrets from container images.

Example referencing a key from a ConfigMap:

```yaml theme={null}
env:
- name: APP_COLOR
  valueFrom:
    configMapKeyRef:
      name: color-config
      key: APP_COLOR
```

Example referencing a key from a Secret:

```yaml theme={null}
env:
- name: APP_COLOR
  valueFrom:
    secretKeyRef:
      name: color-secret
      key: APP_COLOR
```

## Importing all keys with envFrom

If you want to expose every key from a ConfigMap or Secret as environment variables, use `envFrom`. This will create one environment variable per key in the referenced object.

```yaml theme={null}
envFrom:
- configMapRef:
    name: color-config
# or
- secretRef:
    name: color-secret
```

## When to use each approach

| Method | Use case | Example |
| - | - | - |
| Static `env` with `value` | Simple, unchanging values or demos | See Pod manifest above |
| `valueFrom.configMapKeyRef` | Single config key from a ConfigMap | `configMapKeyRef: name: color-config key: APP_COLOR` |
| `valueFrom.secretKeyRef` | Single secret key (sensitive) | `secretKeyRef: name: color-secret key: APP_COLOR` |
| `envFrom` | Import all keys from a ConfigMap or Secret (convenient but broad) | `envFrom: - configMapRef: name: color-config` |

## Security reminder

<Callout icon="warning" color="#FF6B6B">
  Secrets in Kubernetes are base64-encoded, not encrypted by default (see Kubernetes documentation: [https://kubernetes.io/docs/concepts/configuration/secret/](https://kubernetes.io/docs/concepts/configuration/secret/)). Treat them as sensitive and consider using appropriate secrets management (encryption at rest, RBAC controls, or external secret stores) for production workloads.
</Callout>

## Links and references

* Kubernetes ConfigMap: [https://kubernetes.io/docs/concepts/configuration/configmap/](https://kubernetes.io/docs/concepts/configuration/configmap/)
* Kubernetes Secret: [https://kubernetes.io/docs/concepts/configuration/secret/](https://kubernetes.io/docs/concepts/configuration/secret/)
* Kubernetes Pods and Containers: [https://kubernetes.io/docs/concepts/workloads/pods/](https://kubernetes.io/docs/concepts/workloads/pods/)

That's it — you've seen the common patterns to inject environment variables into containers in Kubernetes and when to use static values, ConfigMaps, Secrets, or `envFrom`.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/cka-certification-course-certified-kubernetes-administrator/module/2ddcf79b-abb0-4aeb-ad0c-3d54c7b4fc64/lesson/48a369f6-4e52-4199-83e2-c7d522f30251" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.