Kubernetes and Cloud Native Associate (KCNA)Kubernetes FundamentalsEasy

A developer is writing a YAML manifest for a Pod and wants to ensure that a specific container within that Pod always starts with a particular environment variable, `APP_ENV`, set to `production`. Which section of the Pod's manifest should be used to define this environment variable for the container?

  1. A`spec.restartPolicy`
  2. B`spec.volumes`
  3. C`spec.containers.env`
  4. D`spec.containers.image`
Show answer & explanation

Correct answer: C. `spec.containers.env`

Environment variables for containers within a Pod are defined under the `env` field within the container's specification in the Pod manifest.

Why the other options are wrong

  • A. `spec.restartPolicy` defines how the Pod's containers should be restarted, not environment variables.
  • B. `spec.volumes` defines volumes that can be mounted into containers, not environment variables.
  • D. `spec.containers.image` specifies the container image to use, not environment variables.

Kubernetes Pod Environment Variables

Environment variables can be passed to containers within a Kubernetes Pod via the `env` field in the container specification. This allows for dynamic configuration of applications.

  • Defined as key-value pairs.
  • Can be sourced directly or from ConfigMaps/Secrets.
  • Used for application configuration without rebuilding the container image.

Memory trick: Environment Enriches Execution

More Kubernetes Fundamentals questions