Injecting Environment Variables into Pods

Tutorial 3 of 5

1. Introduction

In this tutorial, we aim to learn how to inject environment variables into Pods in Kubernetes. Environment variables are a universal mechanism for conveying configuration information to Unix programs. Kubernetes exposes Services through environment variables.

By the end of this tutorial, you will:
- Understand what environment variables are and why they're important in Kubernetes,
- Learn how to inject environment variables into Pods in Kubernetes.

Prerequisites:
- Basic understanding of Kubernetes
- Kubernetes installed on your machine

2. Step-by-Step Guide

Kubernetes allows you to define environment variables for your containers, which can be used to make your applications more configurable. You can define environment variables in a variety of ways, including by using Pod fields, ConfigMaps, and Secrets.

Defining Environment Variables for a Container

Here's an example of a Pod that sets the LOG_LEVEL and DB_URL environment variables using the env field:

apiVersion: v1
kind: Pod
metadata:
  name: envar-demo
spec:
  containers:
    - name: envar-demo-container
      image: docker/whalesay:latest
      env:
        - name: LOG_LEVEL
          value: "Debug"
        - name: DB_URL
          value: "postgresql://db.example.com:5432"

3. Code Examples

Example 1: Using Pod Fields to Define Environment Variables

Here's a Pod definition that defines the MY_POD_NAME environment variable using the metadata.name field:

apiVersion: v1
kind: Pod
metadata:
  name: field-demo
spec:
  containers:
    - name: field-demo-container
      image: docker/whalesay:latest
      env:
        - name: MY_POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name

In this example, the MY_POD_NAME environment variable is set to the name of the Pod.

Example 2: Using ConfigMaps to Define Environment Variables

Here's a ConfigMap that defines several environment variables:

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
data:
  LOG_LEVEL: "Debug"
  DB_URL: "postgresql://db.example.com:5432"

And here's a Pod that consumes the ConfigMap:

apiVersion: v1
kind: Pod
metadata:
  name: config-demo
spec:
  containers:
    - name: config-demo-container
      image: docker/whalesay:latest
      envFrom:
        - configMapRef:
            name: my-config

Here, all the data defined in my-config ConfigMap gets injected as environment variables in the Pod.

4. Summary

In this tutorial, we've learned:
- What environment variables are and how to inject them into Pods in Kubernetes,
- How to define environment variables using Pod fields, ConfigMaps, and Secrets.

Next steps for learning:
- Learn how to define environment variables using Secrets,
- Learn how to use environment variables in your applications.

Additional resources:
- Kubernetes documentation

5. Practice Exercises

  1. Create a Pod that sets the LOG_LEVEL environment variable to "Info".
  2. Create a ConfigMap that defines the DB_USER and DB_PASSWORD environment variables. Then, create a Pod that consumes this ConfigMap.

Solutions with explanations:
1. This Pod sets the LOG_LEVEL environment variable to "Info":

yaml apiVersion: v1 kind: Pod metadata: name: log-demo spec: containers: - name: log-demo-container image: docker/whalesay:latest env: - name: LOG_LEVEL value: "Info"

In this solution, we've defined the LOG_LEVEL environment variable directly in the Pod definition.

  1. This ConfigMap defines the DB_USER and DB_PASSWORD environment variables:

yaml apiVersion: v1 kind: ConfigMap metadata: name: db-config data: DB_USER: "myuser" DB_PASSWORD: "mypassword"

And this Pod consumes the ConfigMap:

yaml apiVersion: v1 kind: Pod metadata: name: db-demo spec: containers: - name: db-demo-container image: docker/whalesay:latest envFrom: - configMapRef: name: db-config

In this solution, we've first defined the DB_USER and DB_PASSWORD environment variables in a ConfigMap. Then, we've referenced this ConfigMap in a Pod to inject these environment variables.

Tips for further practice:
- Try defining environment variables using different fields of the Pod,
- Try defining environment variables using Secrets.