How to Create an Environment Variable in a Kubernetes Container: A Guide

How to Create an Environment Variable in a Kubernetes Container: A Guide
Kubernetes, the open-source platform for automating deployment, scaling, and management of containerized applications, is a crucial tool for data scientists. In this blog post, we’ll delve into the specifics of creating an environment variable in a Kubernetes container. This is a fundamental skill for managing configurations across different environments in a Kubernetes cluster.
What is an Environment Variable?
Before we dive into the how-to, let’s briefly discuss what an environment variable is. An environment variable is a dynamic-named value that can affect the way running processes will behave on a computer. They are part of the environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process.
Why Use Environment Variables in Kubernetes?
Environment variables are a universal mechanism for conveying configuration information to Unix programs. Kubernetes lets you create, modify, and delete environment variables for your containers. These variables provide valuable information to the containerized applications and influence their behavior in different environments.
Step-by-Step Guide to Creating an Environment Variable in a Kubernetes Container
Now, let’s get into the step-by-step process of creating an environment variable in a Kubernetes container.
Step 1: Define the Environment Variable in the Pod Specification
The first step is to define the environment variable in the pod specification. Here’s an example of how to do this:
apiVersion: v1
kind: Pod
metadata:
name: envar-demo
spec:
containers:
- name: envar-demo-container
image: gcr.io/google-samples/node-hello:1.0
env:
- name: DEMO_GREETING
value: "Hello from the environment"
- name: DEMO_FAREWELL
value: "Such a sweet sorrow"
In this example, two environment variables (DEMO_GREETING
and DEMO_FAREWELL
) are defined in the env
field of the envar-demo-container
container.
Step 2: Verify the Environment Variable
After defining the environment variable, the next step is to verify it. You can do this by running the following command:
kubectl exec envar-demo -- /bin/sh -c 'echo $DEMO_GREETING'
This command will return the value of the DEMO_GREETING
environment variable, which in this case is “Hello from the environment”.
Using ConfigMaps and Secrets as Environment Variables
In addition to defining environment variables directly in the pod specification, Kubernetes also allows you to use ConfigMaps and Secrets as environment variables.
ConfigMaps
ConfigMaps allow you to decouple configuration artifacts from image content to keep containerized applications portable. Here’s an example of how to use a ConfigMap as an environment variable:
apiVersion: v1
kind: Pod
metadata:
name: configmap-demo-pod
spec:
containers:
- name: demo
image: alpine
envFrom:
- configMapRef:
name: example-configmap
In this example, all key-value pairs in the example-configmap
ConfigMap are injected as environment variables into the demo
container.
Secrets
Secrets let you store and manage sensitive information, such as passwords, OAuth tokens, and ssh keys. Here’s an example of how to use a Secret as an environment variable:
apiVersion: v1
kind: Pod
metadata:
name: secret-demo-pod
spec:
containers:
- name: demo
image: alpine
envFrom:
- secretRef:
name: example-secret
In this example, all key-value pairs in the example-secret
Secret are injected as environment variables into the demo
container.
Conclusion
Creating environment variables in a Kubernetes container is a straightforward process that can greatly enhance the flexibility and portability of your containerized applications. By leveraging ConfigMaps and Secrets, you can further decouple configuration from your application code, making your applications more secure and easier to manage.
Remember, Kubernetes is a powerful tool, but with great power comes great responsibility. Always ensure that your environment variables are managed securely and efficiently to get the most out of your Kubernetes deployments.
About Saturn Cloud
Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.