> ## 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.

# Lab Solution Env Variables Optional

> A Kubernetes lab showing how to inspect and change a Pod environment variable, handle immutable fields by replacing the Pod, and use a ConfigMap with envFrom to supply APP_COLOR.

In this lab you will inspect a running Pod's environment, modify an environment variable, and then switch the Pod to consume its configuration from a ConfigMap. This walkthrough covers the common kubectl patterns used when pod fields are immutable and demonstrates how to use `envFrom` to import all keys from a ConfigMap.

## 1) Inspect the existing Pod and its environment variable

List pods in the current namespace:

```bash theme={null}
kubectl get pods
```

Example output:

```text theme={null}
NAME           READY   STATUS    RESTARTS   AGE
webapp-color   1/1     Running   0          16s
```

Describe the Pod to inspect the container spec and environment variables:

```bash theme={null}
kubectl describe pod webapp-color
```

Relevant excerpt (cleaned for readability):

```text theme={null}
Containers:
  webapp-color:
    Image:  kodekloud/webapp-color
    ...
    Environment:
      APP_COLOR:   pink
```

* The environment variable `APP_COLOR` is set to `pink`.
* Visiting the web application confirms the UI background is pink.

## 2) Update the Pod environment variable directly (immutable fields limitation)

Most Pod fields are immutable. Changing certain fields (including container environment values) via an in-place patch is not allowed by the API. The usual workflow is to fetch the Pod manifest, edit the desired fields locally, and then replace the Pod by deleting and recreating it from the edited manifest.

Fetch and edit the Pod manifest with kubectl:

```bash theme={null}
kubectl edit pod webapp-color
```

If you try to change `spec.containers[*].env[*].value` in-place, the API will reject the update with an error like:

```text theme={null}
# pods "webapp-color" was not valid:
# * spec: Forbidden: pod updates may not change fields other than `spec.containers[*].image`, ...
```

kubectl writes your edits to a temporary file (for example `/tmp/kubectl-edit-XXXX.yaml`). In that file, change the `APP_COLOR` value to `green`. Then replace the Pod by forcing a delete and recreate:

```bash theme={null}
kubectl replace --force -f /tmp/kubectl-edit-XXXX.yaml
```

Example output:

```text theme={null}
pod "webapp-color" deleted
pod/webapp-color replaced
```

Verify the Pod restarted and the UI now shows the new color (`green`).

<Callout icon="lightbulb" color="#1CB2FE">
  Pods are immutable for many fields. Using `kubectl replace --force -f <file>` deletes and recreates the pod from the provided manifest—this is a common technique to apply changes that cannot be done in-place.
</Callout>

## 3) Inspect ConfigMaps to find DB\_HOST

List ConfigMaps in the `default` namespace:

```bash theme={null}
kubectl get configmap
```

Example output:

```text theme={null}
NAME               DATA   AGE
kube-root-ca.crt   1      16m
db-config          3      22s
```

Describe the `db-config` ConfigMap to inspect its data:

```bash theme={null}
kubectl describe configmap db-config
```

Relevant output:

```text theme={null}
Data
====
DB_HOST:
------
SQL01.example.com
DB_NAME:
-----
SQL01
DB_PORT:
----
3306
```

* The database host is `SQL01.example.com` (case-insensitive for answers).

## 4) Create a ConfigMap to provide APP\_COLOR for the Pod

Create a ConfigMap named `webapp-config-map` with the key `APP_COLOR` and value `dark blue`:

```bash theme={null}
kubectl create configmap webapp-config-map --from-literal=APP_COLOR="dark blue"
```

Verify the ConfigMap contents:

```bash theme={null}
kubectl get configmap webapp-config-map -o yaml
```

Expected snippet:

```yaml theme={null}
apiVersion: v1
kind: ConfigMap
metadata:
  name: webapp-config-map
data:
  APP_COLOR: "dark blue"
```

## 5) Update the Pod to consume environment variables from the ConfigMap

Rather than setting each environment variable individually, you can import all keys from a ConfigMap using `envFrom` with `configMapRef`. Update the container spec to include:

```yaml theme={null}
envFrom:
  - configMapRef:
      name: webapp-config-map
```

Because Pod specs are immutable for many fields, open the Pod manifest for editing:

```bash theme={null}
kubectl edit pod webapp-color
```

Add the `envFrom` block under the container spec, save the temporary file created by kubectl (e.g. `/tmp/kubectl-edit-YYYY.yaml`), and then replace the Pod:

```bash theme={null}
kubectl replace --force -f /tmp/kubectl-edit-YYYY.yaml
```

Example output:

```text theme={null}
pod "webapp-color" deleted
pod/webapp-color replaced
```

Describe the recreated Pod to confirm it references the ConfigMap:

```bash theme={null}
kubectl describe pod webapp-color
```

Relevant output:

```text theme={null}
Containers:
  webapp-color:
    ...
    Environment Variables from:
      webapp-config-map  ConfigMap  Optional: false
    Environment:     <none>
```

* The Pod now loads `APP_COLOR` from the `webapp-config-map` ConfigMap and the UI should display `dark blue`.

## Quick reference — commands used in this lab

| Task | Command / Example |
| - | - |
| List pods | `kubectl get pods` |
| Describe pod | `kubectl describe pod webapp-color` |
| Edit pod manifest | `kubectl edit pod webapp-color` |
| Force replace from file | `kubectl replace --force -f /tmp/kubectl-edit-XXXX.yaml` |
| List ConfigMaps | `kubectl get configmap` |
| Describe ConfigMap | `kubectl describe configmap db-config` |
| Create ConfigMap from literal | `kubectl create configmap webapp-config-map --from-literal=APP_COLOR="dark blue"` |
| Show ConfigMap YAML | `kubectl get configmap webapp-config-map -o yaml` |

## Summary

* You inspected the Pod and found `APP_COLOR=pink`.
* Direct in-place edits to change certain Pod fields are blocked by immutability rules; editing and then using `kubectl replace --force -f <file>` is the recommended approach to apply such changes.
* You retrieved `DB_HOST` from the `db-config` ConfigMap (`SQL01.example.com`).
* You created a ConfigMap `webapp-config-map` with `APP_COLOR="dark blue"`.
* You updated the Pod to import environment variables from the ConfigMap using `envFrom: - configMapRef: name: webapp-config-map`, then recreated the Pod so it picks up `dark blue`.

## Links and References

* [Kubernetes Pods](https://kubernetes.io/docs/concepts/workloads/pods/)
* [ConfigMaps](https://kubernetes.io/docs/concepts/configuration/configmap/)
* [kubectl replace documentation](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#replace)

<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/f77d10fe-6407-4607-9462-fdd46cd16e61" />
</CardGroup>


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