Kubernetes
environment variables
reference
cloud-native
DevOps

Kubernetes How to refer to one environment variable from another?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Kubernetes has become an essential tool for managing containerized applications, offering robust features for deployment, scaling, and management. One of the key aspects of Kubernetes configuration is the use of environment variables. Kubernetes allows you to configure your container's environment dynamically at runtime, simplifying the process of managing different environment settings for different workloads. A common requirement is to reference one environment variable from another within a Kubernetes Pod. This article explains how to effectively achieve this.

Basics of Environment Variables in Kubernetes

In Kubernetes, environment variables can be set using a Pod specification. This specification allows you to define environment variables for containers within the Pod. Environment variables can be declared directly or populated from different sources such as ConfigMaps, Secrets, field values, etc.

Here's a basic YAML configuration that demonstrates setting an environment variable:

yaml
1apiVersion: v1
2kind: Pod
3metadata:
4  name: example-pod
5spec:
6  containers:
7  - name: example-container
8    image: nginx
9    env:
10    - name: EXAMPLE_VAR
11      value: "example_value"

Referencing Environment Variables

In some cases, you may want to reference the value of one environment variable from another. Kubernetes supports this through variable expansion within the value of another environment variable.

Using String Interpolation

Kubernetes supports the use of downward API values, allowing a variable to be referenced in another variable using the format $(VAR_NAME). This is the process known as string interpolation. However, nested variable expansion (e.g., using another environment variable's value within a value) is not supported directly in Kubernetes.

Example Configuration

Consider the following configuration, which demonstrates how you can use this feature:

yaml
1apiVersion: v1
2kind: Pod
3metadata:
4  name: interpolation-demo
5spec:
6  containers:
7  - name: demo-container
8    image: busybox
9    command: ["sh", "-c", "echo 'FULL_MESSAGE is: $FULL_MESSAGE'"]
10    env:
11    - name: PART1
12      value: "Hello"
13    - name: PART2
14      value: "$(PART1), World!"  # Notice the usage of $(VAR_NAME)
15    - name: FULL_MESSAGE
16      value: "$(PART2) - Greetings from Kubernetes."

In this configuration:

  • PART1 is set to "Hello".
  • PART2 attempts to use the value of PART1, but $(PART1) does not expand into its value within the environment variables.
  • FULL_MESSAGE attempts to further expand PART2, creating a message that does not concatenate as one might expect in standard shell script behavior.

Actual Value Resolution

Kubernetes does not support recursive environment variable expansion directly within its configurations. Attempting to use nested variables will not yield expected results as no substitution occurs.

To achieve nested variable resolution, you could handle this logic externally, within the application's startup script or ideally, manage configurations separately via a ConfigMap or by using scripts in your container.

Script Example:

Below is an example of how you might handle this in a script inside your container:

bash
1# startup.sh script
2#!/bin/sh
3
4export PART1="Hello"
5# Manually doing the equivalent of recursive expansion
6export PART2="${PART1}, World!"
7export FULL_MESSAGE="${PART2} - Greetings from Kubernetes."
8
9echo "FULL_MESSAGE is: ${FULL_MESSAGE}"

Table Summary

The table below summarizes key information about environment variable handling in Kubernetes:

Key PointDescription
Variable DeclarationEnvironment variables are declared within the env: section of a Pod or container.
Direct ReferenceUse $NAME or ${NAME} in shell scripts for direct value access, not in YAML.
Expansion LimitationKubernetes does not perform nested expansion within environment variable values.
WorkaroundHandle complex logic within container scripts or external tools like ConfigMaps.
Benefits of ConfigurationDynamic and flexible management of application environment settings at deployment.

Conclusion

In Kubernetes, while it's straightforward to set environment variables, referencing one from another requires additional handling. By understanding Kubernetes' limitations with variable expansion and employing scripts or configuration management tools, you can effectively manage complex environment requirements in your deployments.

Additional Recommendations:

  • Use ConfigMaps for managing configuration independently of container images.
  • Secure sensitive data using Kubernetes Secrets.
  • Consider application-level configuration management libraries for complex use cases.

Kubernetes' environment variable management is a powerful feature that, despite its limitations, can be extended with understanding and planning. By incorporating these best practices and solutions, you can leverage Kubernetes' capabilities to enhance the deployment and operation of your containerized applications.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.