Environment variables are a fundamental concept in Kubernetes, allowing you to configure your applications dynamically without changing the code. This section will cover how to use environment variables in Kubernetes, including practical examples and exercises to reinforce your understanding.
Key Concepts
- Environment Variables: Key-value pairs that are used to configure applications.
- ConfigMaps: Kubernetes objects that store configuration data in key-value pairs.
- Secrets: Kubernetes objects that store sensitive data, such as passwords and API keys, in key-value pairs.
Setting Environment Variables in Kubernetes
Using env in Pod Definitions
You can define environment variables directly in the Pod specification using the env field.
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: my-image
env:
- name: MY_ENV_VAR
value: "my-value"Using ConfigMaps
ConfigMaps allow you to decouple configuration artifacts from image content to keep containerized applications portable.
- Create a ConfigMap:
- Use ConfigMap in Pod:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: my-image
envFrom:
- configMapRef:
name: my-configUsing Secrets
Secrets are similar to ConfigMaps but are intended to hold sensitive information.
- Create a Secret:
apiVersion: v1 kind: Secret metadata: name: my-secret type: Opaque data: MY_SECRET_VAR: bXktc2VjcmV0LXZhbHVl # Base64 encoded value
- Use Secret in Pod:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: my-image
envFrom:
- secretRef:
name: my-secretPractical Examples
Example 1: Using Environment Variables Directly
apiVersion: v1
kind: Pod
metadata:
name: env-pod
spec:
containers:
- name: env-container
image: busybox
command: ["sh", "-c", "echo Hello $MY_ENV_VAR"]
env:
- name: MY_ENV_VAR
value: "World"Example 2: Using ConfigMaps
- Create ConfigMap:
apiVersion: v1 kind: ConfigMap metadata: name: app-config data: APP_ENV: "production" APP_DEBUG: "false"
- Use ConfigMap in Pod:
apiVersion: v1
kind: Pod
metadata:
name: configmap-pod
spec:
containers:
- name: configmap-container
image: busybox
command: ["sh", "-c", "echo $APP_ENV $APP_DEBUG"]
envFrom:
- configMapRef:
name: app-configExample 3: Using Secrets
- Create Secret:
apiVersion: v1 kind: Secret metadata: name: app-secret type: Opaque data: DB_PASSWORD: cGFzc3dvcmQ= # Base64 encoded value
- Use Secret in Pod:
apiVersion: v1
kind: Pod
metadata:
name: secret-pod
spec:
containers:
- name: secret-container
image: busybox
command: ["sh", "-c", "echo $DB_PASSWORD"]
envFrom:
- secretRef:
name: app-secretExercises
Exercise 1: Create a Pod with Environment Variables
- Create a Pod definition that sets an environment variable
GREETINGto "Hello, Kubernetes!". - Use the
busyboximage and print the value ofGREETINGusing theechocommand.
Solution:
apiVersion: v1
kind: Pod
metadata:
name: greeting-pod
spec:
containers:
- name: greeting-container
image: busybox
command: ["sh", "-c", "echo $GREETING"]
env:
- name: GREETING
value: "Hello, Kubernetes!"Exercise 2: Use ConfigMap to Set Environment Variables
- Create a ConfigMap named
app-configwith the following data:APP_MODE: "development"APP_PORT: "8080"
- Create a Pod that uses this ConfigMap to set environment variables and prints them.
Solution:
- Create ConfigMap:
apiVersion: v1 kind: ConfigMap metadata: name: app-config data: APP_MODE: "development" APP_PORT: "8080"
- Create Pod:
apiVersion: v1
kind: Pod
metadata:
name: configmap-pod
spec:
containers:
- name: configmap-container
image: busybox
command: ["sh", "-c", "echo $APP_MODE $APP_PORT"]
envFrom:
- configMapRef:
name: app-configExercise 3: Use Secret to Set Environment Variables
- Create a Secret named
db-secretwith the following data:DB_USER: "admin" (Base64 encoded)DB_PASS: "password" (Base64 encoded)
- Create a Pod that uses this Secret to set environment variables and prints them.
Solution:
- Create Secret:
apiVersion: v1 kind: Secret metadata: name: db-secret type: Opaque data: DB_USER: YWRtaW4= # Base64 encoded value DB_PASS: cGFzc3dvcmQ= # Base64 encoded value
- Create Pod:
apiVersion: v1
kind: Pod
metadata:
name: secret-pod
spec:
containers:
- name: secret-container
image: busybox
command: ["sh", "-c", "echo $DB_USER $DB_PASS"]
envFrom:
- secretRef:
name: db-secretCommon Mistakes and Tips
- Base64 Encoding: Ensure that the values in Secrets are Base64 encoded. Use
echo -n 'value' | base64to encode. - Referencing ConfigMaps and Secrets: Double-check the names of ConfigMaps and Secrets when referencing them in Pod specifications.
- Environment Variable Names: Use clear and descriptive names for environment variables to avoid confusion.
Conclusion
In this section, you learned how to use environment variables in Kubernetes, including direct definitions, ConfigMaps, and Secrets. You also practiced creating and using these configurations through practical examples and exercises. Understanding how to manage environment variables is crucial for configuring and securing your applications in Kubernetes. In the next section, we will explore resource quotas and limits to manage resource allocation in your Kubernetes cluster.
Kubernetes Course
Module 1: Introduction to Kubernetes
- What is Kubernetes?
- Kubernetes Architecture
- Key Concepts and Terminology
- Setting Up a Kubernetes Cluster
- Kubernetes CLI (kubectl)
Module 2: Core Kubernetes Components
Module 3: Configuration and Secrets Management
Module 4: Networking in Kubernetes
Module 5: Storage in Kubernetes
Module 6: Advanced Kubernetes Concepts
Module 7: Monitoring and Logging
- Monitoring with Prometheus
- Logging with Elasticsearch, Fluentd, and Kibana (EFK)
- Health Checks and Probes
- Metrics Server
Module 8: Security in Kubernetes
Module 9: Scaling and Performance
Module 10: Kubernetes Ecosystem and Tools
Module 11: Case Studies and Real-World Applications
- Deploying a Web Application
- CI/CD with Kubernetes
- Running Stateful Applications
- Multi-Cluster Management
