kubectl
environment variables
kubernetes deployment
devops
tutorial

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:

yaml
1apiVersion: apps/v1
2kind: Deployment
3metadata:
4  name: my-app
5spec:
6  replicas: 3
7  selector:
8    matchLabels:
9      app: my-app
10  template:
11    metadata:
12      labels:
13        app: my-app
14    spec:
15      containers:
16      - name: my-container
17        image: my-image:latest
18        env:
19        - name: KEY_ONE
20          value: "value1"
21        - name: KEY_TWO
22          value: "value2"

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

bash
kubectl create configmap my-config --from-literal=KEY_ONE=value1 --from-literal=KEY_TWO=value2

Step 2: Reference the ConfigMap in your Pod or Deployment YAML:

yaml
envFrom:
  - configMapRef:
      name: my-config

3. Using Secrets for Sensitive Data

Use Secrets when dealing with sensitive information like passwords, tokens, or keys.

Step 1: Create a Secret

bash
kubectl create secret generic my-secret --from-literal=SECRET_KEY=mysecretvalue

Step 2: Reference the Secret in your Pod or Deployment:

yaml
envFrom:
  - secretRef:
      name: my-secret

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:

yaml
1env:
2- name: POD_NAME
3  valueFrom:
4    fieldRef:
5      fieldPath: metadata.name
6- name: POD_NAMESPACE
7  valueFrom:
8    fieldRef:
9      fieldPath: metadata.namespace

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

MethodUse CaseProsCons
env FieldDirect and straightforward usageSimple and readableLess flexibility for sensitive data
ConfigMapsConfigs that may change frequentlyEasy management, environment-agnosticExternal management needed
SecretsSensitive data (e.g., passwords)Encrypted storage, separation of concernsAccess must be tightly controlled
Downward APIAccess Pod metadataReal-time metadata accessRequires 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.


Course illustration
Course illustration

All Rights Reserved.