Kubernetes Chronicles:(K8s#07)|K8s Series | PODs | Environment Variables.
Kubernetes environment variables are dynamic key-value pairs that provide configuration settings to containers within the pods. These variables later can be used by the application during runtime.
Environment variables can be defined at both the Pod and individual container levels. Variables defined at the pod level are shared among all containers within a pod.
We can define the Kubernetes Environment variables using env
or envFrom
fields in the manifests file. It overrides any environment variables defined in the container image. We can also set dependent environment variables using ${VAR_NAME} under the value section.
env
: allows to set environment variables by specifying key-value pairs directly for each variable.
envFrom
: allows to set environment variables by referencing either a ConfigMap or Secret.
Using env
keyword:
This is straightforward forward and we can easily define any environment variables in the deployment manifest using env
keyword under spec.containers
section.
apiVersion: v1
kind: Pod
metadata:
name: envar-demo
labels:
purpose: demonstrate-envars
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"
- configMapKeyRef: To define individual key-value pairs as environment variables from configmap.
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: registry.k8s.io/busybox
command: [ "/bin/echo", "$(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ]
env:
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: SPECIAL_LEVEL
- name: SPECIAL_TYPE_KEY
valueFrom:
configMapKeyRef:
name: special-config
key: SPECIAL_TYPE
restartPolicy: Never
- secretKeyRef: To define individual secret key-value pairs as environment variables from Kubernetes secret object.
---
apiVersion: apps/v1
kind: Deployment
(...)
containers:
- name: example-app-prod
image: [yourimage]
env:
# Inject variables from a Kuberentes secret
- name: secret_variables
valueFrom:
secretKeyRef:
name: secret_data
key: password
Using envFrom keyword.
- ConfigMapRef: To define all Configmap’s data as the environment variable.
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: registry.k8s.io/busybox
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: my-env-config
restartPolicy: Never
- secretRef: To define all secrets i.e... sensitive configuration data as the environment variable. In this way, we only need to specify the Kubernetes secret name, which in turn load all data as environment variables automatically.
---
apiVersion: apps/v1
kind: Deployment
(...)
containers:
- name: example-app-prod
image: [yourimage]
env:
# Inject variables from a Kuberentes secret
- name: secret_variables
valueFrom:
secretRef:
name: secret_data
Summary:
In this article, we have learned how to define environment variables in multiple ways depending on application-specific use cases. We can use env
and envFrom
by leveraging the ConfigMap and Secret objects to load the environment variables into application runtime dynamically.
In the next upcoming article, we will learn about Pod ConfigMaps. Stay Tuned.
If you find this article helpful, please show your appreciation and support using claps and follow me for more such articles. Your Support keeps this series running and fuels more content creation.