19 May 2022

Print environment variables of a running pod

  • k8s
  • kubernetes
  • howto

A quick way to print the environment variables of a running pod is to use the following command.

kubectl exec <pod-name> -- env

For example, take a look at the sample nginx pod with few additional (MY_POD and PORT) environment variables.

apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- image: nginx:1.14.2
name: nginx
ports:
- containerPort: 8080
env:
- name: POD_NAME
value: "my_pod"
- name: PORT
value: "8080"

Once the pod is in running state, you can use the following command to print the environment variables of the pod.

kubectl exec my-pod -- env

You can see the output as below listing the environment variables.

$ kubectl exec my-pod -- env

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=my-pod
POD_NAME=my_pod
PORT=8080
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP_PROTO=tcp
NGINX_VERSION=1.14.2-1~stretch
NJS_VERSION=1.14.2.0.2.6-1~stretch
HOME=/root