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.
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.
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:
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:
- Node.js:
- Java:
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.
This command provides an extensive output, listing all environment variables under the Containers section.
- kubectl exec with Environment Print Command:
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.
Use it in a pod:
- Secrets Example:A Secret named
db-credentials.
Use it in a pod:
Summary Table
| Configuration Aspect | Method | Example |
| Static Variable | Define directly in pod spec YAML | value: "Hello, Kubernetes!" |
| ConfigMap | Utilize envFrom with configMapRef | name: my-config |
| Secrets | Utilize envFrom with secretRef | name: db-credentials |
| Field Reference | Use valueFrom with fieldRef for accessing pod fields | fieldPath: metadata.name |
| List Variables (CLI) | Use kubectl exec and a shell printenv command | kubectl exec example-pod -- printenv |
| Describe Pod | Use kubectl describe to inspect the pod | kubectl 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
- Get error unknown field serviceName in io.k8s.api.networking.v1.IngressBackend when switch from v1beta1 to v1 in Kubernetes Ingress
- Get pods on nodes with certain label
- Get Ready status using kubectl -ojsonpath
- Get replica set of the deployment
- Get exception description and stack trace which caused an exception, all as a string
- Get log4net log file in C
- Get Service selectors with K8s Python client
- Get the image and SHA image ID of images in pod on Kubernetes deployment

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.