Updated on 2024-11-01 GMT+08:00

Environment Variables

Environment variables are set in the container running environment.

They provide great flexibility for applications. You can use environment variables in applications, assign values to environment variables when creating containers, and read the values of environment variables when containers are running, realizing flexible configuration. With environment variables, you do not need to rewrite application files to create images.

You can also use ConfigMap and secret as environment variables. For details, see ConfigMap and Secret.

The following shows how to use an environment variable. You only need to configure the spec.containers.env field.

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
    containers:
    - image: nginx:1
      name: container-0
      resources:
        limits:
          cpu: 500m
          memory: 1024Mi
        requests:
          cpu: 500m
          memory: 1024Mi
        env:                          # Environment variable
      - name: env_key
        value: env_value
      - name: pod_name
         valueFrom:                   # Name of the referenced pod
          fieldRef:
            fieldPath: metadata.name
      - name: pod_ip
        valueFrom:                    # IP address of the referenced pod
          fieldRef:
            fieldPath: status.podIP
    imagePullSecrets:
    - name: imagepull-secret