Kubernetes
Environment Variables
Pod
DevOps
Configuration

Get environment variable from kubernetes pod?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Understanding Environment Variables in Kubernetes Pods

Environment variables are essential components that allow you to configure applications dynamically across different environments without changing the application code. In Kubernetes, environment variables are widely used to manage configuration information for applications running inside pods. This article dives into methods for getting and utilizing environment variables within Kubernetes pods, offering insight, examples, and a practical guide.

What are Kubernetes Pods?

A Kubernetes pod is the smallest deployable unit that can be created, scheduled, and managed in Kubernetes. Pods typically run one or more containers that require access to configuration information to function correctly. Environment variables in pods are a suitable way to externalize configuration and ensure that applications behave as expected.

Setting Environment Variables in a Pod

Kubernetes provides several methods to define environment variables for the containers within pods. The primary ways include:

  • Static Environment Variables: Directly defined in the pod specification YAML.
  • ConfigMaps/Secrets: Used for managing configurations at scale, especially for sensitive data.
  • FieldRef and ResourceFieldRef: Allow variables to be set based on pod fields or resource limits and requests.
yaml
1apiVersion: v1
2kind: Pod
3metadata:
4  name: example-pod
5spec:
6  containers:
7  - name: example-container
8    image: nginx
9    env:
10    - name: MY_VARIABLE
11      value: "Hello, Kubernetes!"
12    - name: POD_NAME
13      valueFrom:
14        fieldRef:
15          fieldPath: metadata.name

Accessing Environment Variables in a Pod

Accessing environment variables within a running container is straightforward, similar to accessing them in any typical operating environment.

From within the Container

If you have shell access to the container, you can use standard shell commands to list or get the variable:

bash
1# Log into the container
2kubectl exec -it example-pod -- /bin/sh
3
4# Print the environment variable
5echo $MY_VARIABLE
From within the Application Code

You can access environment variables directly through the supported libraries in your application code, depending on the programming language in use:

  • Python:
python
    import os
    my_variable = os.getenv('MY_VARIABLE')
  • Node.js:
javascript
    const myVariable = process.env.MY_VARIABLE;
  • Java:
java
    String myVariable = System.getenv("MY_VARIABLE");

Retrieval of Environment Variables Without Shell Access

In some scenarios, it might be necessary to inspect or grab the value of environment variables for troubleshooting or configuration validation without shell access. Kubernetes facilitates this through:

  • kubectl describe: Use this command to reveal information about the pod, including environment variables.
bash
    kubectl describe pod example-pod

This command provides an extensive output, listing all environment variables under the Containers section.

  • kubectl exec with Environment Print Command:
bash
    kubectl exec example-pod -- printenv

This command outputs all the environment variables set inside the container.

Using ConfigMaps and Secrets

ConfigMaps and Secrets allow you to store configuration data and manage sensitive information respectively. Integrating them as environment variables is common:

  • ConfigMap Example:
    Suppose you have a ConfigMap named my-config.
yaml
1    apiVersion: v1
2    kind: ConfigMap
3    metadata:
4      name: my-config
5    data:
6      DB_HOST: "database.example.com"

Use it in a pod:

yaml
    envFrom:
    - configMapRef:
        name: my-config
  • Secrets Example:
    A Secret named db-credentials.
yaml
1    apiVersion: v1
2    kind: Secret
3    metadata:
4      name: db-credentials
5    type: Opaque
6    data:
7      password: d2VsY29tZTEyMw==  # Base64 encoded string

Use it in a pod:

yaml
    envFrom:
    - secretRef:
        name: db-credentials

Summary Table

Configuration AspectMethodExample
Static VariableDefine directly in pod spec YAMLvalue: "Hello, Kubernetes!"
ConfigMapUtilize envFrom with configMapRefname: my-config
SecretsUtilize envFrom with secretRefname: db-credentials
Field ReferenceUse valueFrom with fieldRef for accessing pod fieldsfieldPath: metadata.name
List Variables (CLI)Use kubectl exec and a shell printenv commandkubectl exec example-pod -- printenv
Describe PodUse kubectl describe to inspect the podkubectl describe pod example-pod

By understanding and leveraging these methods, DevOps practices around application configuration, security, and deployment can be improved, paving the way for more flexible and robust deployments in production environments. Embrace these techniques to seamlessly and securely manage configurations using Kubernetes environment variables.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.