how to pass environment variable in kubectl deployment?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Understanding Environment Variables in Kubernetes
Kubernetes, the popular container orchestration platform, provides a robust configuration management system that can take care of environment-specific dependencies needed by applications. One common requirement in deployment is the need to pass environment variables to your applications. Kubernetes allows you to configure environment variables for your containers using the kubectl command-line tool among other methods.
In this article, we will delve into different methods for passing environment variables to your Kubernetes Pods with kubectl, complete with explanations and examples to guide you.
Why Use Environment Variables?
Environment variables provide a dynamic way to configure applications without changing application code. This method:
- Simplifies moving applications between different environments (dev, staging, production).
- Keeps sensitive information (like API keys) out of your source code.
- Supports the Twelve-Factor App methodology for application deployment.
Methods for Passing Environment Variables
1. Using the env Field in YAML Configuration
The most common way to define environment variables is within the Pod or Deployment YAML file under the env field. Here's how you can structure it:
This configuration will pass two environment variables, KEY_ONE and KEY_TWO, with values value1 and value2, respectively, to the containers in the my-app deployment.
2. Using ConfigMaps
ConfigMaps allow you to decouple environment-specific configuration from your container images, making applications more portable.
Step 1: Create a ConfigMap
Step 2: Reference the ConfigMap in your Pod or Deployment YAML:
3. Using Secrets for Sensitive Data
Use Secrets when dealing with sensitive information like passwords, tokens, or keys.
Step 1: Create a Secret
Step 2: Reference the Secret in your Pod or Deployment:
4. Using Downward API
Kubernetes also allows you to expose Pod fields and metadata as environment variables using the Downward API. This is used for getting resource requests/limits, Pod name, and namespace, etc.
Example:
With this configuration, the POD_NAME and POD_NAMESPACE environment variables will be set to the Pod's name and namespace, respectively.
Key Points Summary
| Method | Use Case | Pros | Cons |
env Field | Direct and straightforward usage | Simple and readable | Less flexibility for sensitive data |
| ConfigMaps | Configs that may change frequently | Easy management, environment-agnostic | External management needed |
| Secrets | Sensitive data (e.g., passwords) | Encrypted storage, separation of concerns | Access must be tightly controlled |
| Downward API | Access Pod metadata | Real-time metadata access | Requires familiarity with Pod specs |
Conclusion
Passing environment variables in Kubernetes using kubectl can be achieved through various methods, each with its own advantages. Whether you are looking to manage configuration data with ConfigMaps, handle secrets securely, or utilize the Downward API for dynamic metadata access, Kubernetes provides flexible options tailored to different needs. Understanding these methods allows for robust and adaptable application configuration across multiple environments.
Using these techniques thoughtfully ensures that your applications are not only portable but also efficiently configured in a Kubernetes ecosystem. Each method has its specific use case and choosing the right one depends on your application requirements.

