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

  1. Environment Variables: Key-value pairs that are used to configure applications.
  2. ConfigMaps: Kubernetes objects that store configuration data in key-value pairs.
  3. 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.

  1. Create a ConfigMap:
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
data:
  MY_ENV_VAR: "my-value"
  1. Use ConfigMap in Pod:
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: my-image
    envFrom:
    - configMapRef:
        name: my-config

Using Secrets

Secrets are similar to ConfigMaps but are intended to hold sensitive information.

  1. Create a Secret:
apiVersion: v1
kind: Secret
metadata:
  name: my-secret
type: Opaque
data:
  MY_SECRET_VAR: bXktc2VjcmV0LXZhbHVl  # Base64 encoded value
  1. Use Secret in Pod:
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: my-image
    envFrom:
    - secretRef:
        name: my-secret

Practical 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

  1. Create ConfigMap:
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  APP_ENV: "production"
  APP_DEBUG: "false"
  1. 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-config

Example 3: Using Secrets

  1. Create Secret:
apiVersion: v1
kind: Secret
metadata:
  name: app-secret
type: Opaque
data:
  DB_PASSWORD: cGFzc3dvcmQ=  # Base64 encoded value
  1. 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-secret

Exercises

Exercise 1: Create a Pod with Environment Variables

  1. Create a Pod definition that sets an environment variable GREETING to "Hello, Kubernetes!".
  2. Use the busybox image and print the value of GREETING using the echo command.

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

  1. Create a ConfigMap named app-config with the following data:
    • APP_MODE: "development"
    • APP_PORT: "8080"
  2. Create a Pod that uses this ConfigMap to set environment variables and prints them.

Solution:

  1. Create ConfigMap:
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  APP_MODE: "development"
  APP_PORT: "8080"
  1. 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-config

Exercise 3: Use Secret to Set Environment Variables

  1. Create a Secret named db-secret with the following data:
    • DB_USER: "admin" (Base64 encoded)
    • DB_PASS: "password" (Base64 encoded)
  2. Create a Pod that uses this Secret to set environment variables and prints them.

Solution:

  1. 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
  1. 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-secret

Common Mistakes and Tips

  • Base64 Encoding: Ensure that the values in Secrets are Base64 encoded. Use echo -n 'value' | base64 to 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

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

Module 8: Security in Kubernetes

Module 9: Scaling and Performance

Module 10: Kubernetes Ecosystem and Tools

Module 11: Case Studies and Real-World Applications

Module 12: Preparing for Kubernetes Certification

© Copyright 2024. All rights reserved