Configuring Pods: Declaring Environment Variables

One of the ways to configure a pod is to declare environment variables for its containers. Environment variables are key-value pairs that can be used to pass configuration information or secrets to the containerized applications. In this blog post, we will learn how to declare environment variables for pods in Kubernetes and how to use them in our applications.

There are two main ways to declare environment variables for pods: using the env field in the pod spec or using a ConfigMap or a Secret as a source of environment variables. Let’s see how each of these methods works.

Using the env field

The simplest way to declare environment variables for a pod is to use the env field in the container spec. The env field is an array of objects, each with a name and a value field. For example, the following pod spec declares two environment variables for the container: NAME and VERSION.

apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:

  • name: my-app image: my-app:1.0 env:
    • name: NAME
      value: my-app
    • name: VERSION
      value: 1.0

To use these environment variables in our application, we can simply refer to them by their names. For example, in a Python application, we can use os.environ[‘NAME’] and os.environ[‘VERSION’] to access the values of these variables.

Using a ConfigMap or a Secret

Another way to declare environment variables for a pod is to use a ConfigMap or a Secret as a source of environment variables. A ConfigMap is an object that stores configuration data as key-value pairs, while a Secret is an object that stores sensitive data such as passwords or tokens as key-value pairs. Both ConfigMap and Secret can be used to populate environment variables for pods.

To use a ConfigMap or a Secret as a source of environment variables, we need to create the ConfigMap or Secret object first and then reference it in the pod spec using the envFrom field. The envFrom field is an array of objects, each with a configMapRef or a secretRef field. For example, the following pod spec uses a ConfigMap named my-config and a Secret named my-secret as sources of environment variables for the container.

apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:

  • name: my-app image: my-app:1.0 envFrom:
    • configMapRef:
      name: my-config
    • secretRef:
      name: my-secret

To create the ConfigMap and the Secret objects, we can use the kubectl create command with the –from-literal flag. For example, the following commands create a ConfigMap named my-config with two key-value pairs and a Secret named my-secret with one key-value pair.

kubectl create configmap my-config –from-literal=NAME=my-app –from-literal=VERSION=1.0
kubectl create secret generic my-secret –from-literal=PASSWORD=secret123

To use these environment variables in our application, we can refer to them by their keys. For example, in a Python application, we can use os.environ[‘NAME’], os.environ[‘VERSION’], and os.environ[‘PASSWORD’] to access the values of these variables.

Conclusion

In this blog post, we learned how to declare environment variables for pods in Kubernetes and how to use them in our applications. We saw two main ways to declare environment variables: using the env field in the pod spec or using a ConfigMap or a Secret as a source of environment variables. Environment variables are useful for passing configuration information or secrets to our containerized applications without hard-coding them in our code or images.