Kahibaro
Discord Login Register

Environment variables

Role of Environment Variables in OpenShift

In OpenShift, environment variables are a primary mechanism to inject configuration into running containers without rebuilding images. They are evaluated inside the container process, just like environment variables in a regular Linux process, but are defined and managed through Kubernetes/OpenShift resources.

Environment variables are especially useful for:

They complement (and often reference) ConfigMap and Secret objects, but this chapter focuses specifically on how environment variables are defined, prioritized, and used.

Ways to Define Environment Variables in OpenShift

Environment variables can be set at several resource levels and through several mechanisms.

In Deployment/Pod specifications

The most direct way is to define variables under env in pod specs (for example in a Deployment, DeploymentConfig, Job, or CronJob):

apiVersion: apps/v1
kind: Deployment
metadata:
  name: example-app
spec:
  template:
    spec:
      containers:
        - name: app
          image: quay.io/org/app:latest
          env:
            - name: APP_MODE
              value: "production"
            - name: LOG_LEVEL
              value: "info"

This sets static, literal values in the pod. Changes require updating the workload resource and usually a rollout.

From ConfigMaps

To keep configuration separate from workload definitions and make it easier to update, environment variables are often populated from ConfigMap keys:

env:
  - name: APP_CONFIG
    valueFrom:
      configMapKeyRef:
        name: app-config
        key: app-config.yaml

Or bulk-import an entire ConfigMap as environment variables:

envFrom:
  - configMapRef:
      name: app-config

In the bulk approach, each key in the ConfigMap becomes an environment variable with the same name.

From Secrets

Environment variables can also be populated from Secret objects (appropriate for sensitive values, discussed more in the Secrets chapter):

env:
  - name: DB_PASSWORD
    valueFrom:
      secretKeyRef:
        name: db-credentials
        key: password

Or all keys from a Secret:

envFrom:
  - secretRef:
      name: db-credentials

Note that once a secret is in an environment variable, it is visible to processes and can appear in logs or crash dumps; treat such pods carefully.

Using `oc` CLI shortcuts

The oc CLI provides helpers for adding environment variables to existing resources without manually editing YAML:

  oc set env deployment/example-app APP_MODE=staging LOG_LEVEL=debug
  oc set env deployment/example-app --from=configmap/app-config
  oc set env deployment/example-app --from=secret/db-credentials
  oc set env deployment/example-app APP_MODE- LOG_LEVEL-

These commands update the resource spec and typically trigger a rollout for controllers like Deployment.

Types and Sources of Values

Literal values

Values given directly in the spec using value. These are static strings:

env:
  - name: FEATURE_FLAG_NEW_UI
    value: "true"

Environment variables are always strings from the container’s perspective; any typing or conversion is handled by the application.

Values from fields (`fieldRef`)

You can dynamically set environment variables from pod or container fields, such as pod name, namespace, or node name:

env:
  - name: POD_NAME
    valueFrom:
      fieldRef:
        fieldPath: metadata.name
  - name: POD_NAMESPACE
    valueFrom:
      fieldRef:
        fieldPath: metadata.namespace
  - name: NODE_NAME
    valueFrom:
      fieldRef:
        fieldPath: spec.nodeName

This is useful for logging, metrics, and behavior that depends on runtime placement.

Values from resource fields (`resourceFieldRef`)

You can expose pod resource limits/requests to the container, often for tuning runtimes (e.g., JVM):

env:
  - name: JVM_MAX_HEAP
    valueFrom:
      resourceFieldRef:
        containerName: app
        resource: limits.memory
        divisor: 1Mi

This lets the container adapt its behavior based on configured resources.

Precedence, Overrides, and Merging

When environment variables are defined from multiple sources, certain rules apply:

Example:

  envFrom:
    - configMapRef:
        name: defaults-config      # defines LOG_LEVEL=info
  env:
    - name: LOG_LEVEL
      value: "debug"               # overrides defaults-config

Environment Variables versus Files and Volumes

Configuration can be injected via:

Environment variables are best when:

Mounted files are better when:

You can combine both:

Pattern: Environment-Specific Configuration

A common OpenShift pattern is to differentiate configuration across environments (dev, test, prod) via environment variables, while keeping the image the same.

Examples:

Typical setup:

This supports the “build once, deploy many times” model central to cloud-native and OpenShift workflows.

Environment Variables in OpenShift Templates and Pipelines

Environment variables are frequently used with OpenShift templates and CI/CD logic:

Using environment variables this way keeps deployments flexible and automatable without manual edits to application code.

Observability and Debugging of Environment Variables

To troubleshoot issues related to configuration:

  oc describe deployment/example-app

Look under Environment: in the output.

    oc rsh pod/example-app-abc123
    env | sort

When debugging:

For sensitive variables, be cautious: commands like oc describe and shell env output will show the values in plain text.

Best Practices Specific to Environment Variables

Environment variables are a central, flexible mechanism in OpenShift for connecting applications to their runtime environment. Used properly, they enable consistent deployments, safe configuration separation, and straightforward operational control across clusters and environments.

Views: 11

Comments

Please login to add a comment.

Don't have an account? Register now!