0

I wrote golang program which fetches values from environment variable set in my system using export var name = somevalue.

cloudType = os.Getenv("CLOUD_TYPE")
clusterRegion = os.Getenv("CLUSTER_REGION")
clusterType = os.Getenv("CLUSTER_TYPE")
clusterName = os.Getenv("CLUSTER_NAME")
clusterID = os.Getenv("CLUSTER_ID")

As mentioned above my program tries to fetch values from env var set in system using getenv func.The program is working good if run it and fetching values from env variables. But When I tried building a image and running it inside a pod it was able to fetch values from the env var. It is giving empty values. Is there any way to access the local env var from the pod?

Sathya
  • 69
  • 2
  • 8
  • Environment variables are not "set in my system". Rather, they are set in a process and from there passed to child processes started from there. This has nothing to do with Go, btw, it's how the OS works. – Ulrich Eckhardt Nov 26 '21 at 07:03
  • Can you exec into the pod, list the environment variables and check if you can find the variables that you have set? – gohm'c Nov 26 '21 at 07:12
  • I haven't set the env var in the pod. I set it locally in my system. – Sathya Nov 26 '21 at 07:27

3 Answers3

1

Make a yaml file like this to define a config map

apiVersion: v1
data:
  CLOUD_TYPE: "$CLOUD_TYPE"
  CLUSTER_REGION: "$CLUSTER_REGION"
  CLUSTER_TYPE: "$CLUSTER_TYPE"
  CLUSTER_NAME: "$CLUSTER_NAME"
  CLUSTER_ID: "$CLUSTER_ID"
kind: ConfigMap
metadata:
  creationTimestamp: null
  name: foo

Ensure your config vars are set then apply it to your cluster, with env substitution first

envsubst < foo.yaml | kubectl apply -f

Then in the pod definition use the config map

spec:
  containers:
  - name: mypod
    envFrom:
    - configMapRef:
        name: foo
Vorsprung
  • 32,923
  • 5
  • 39
  • 63
0

...haven't set the env var in the pod. I set it locally in my system

Environment variables set on your host are not automatically pass on to the Pod. You can set the env in your spec and access by your container. A common approach to substitute environment variables in the spec with variables on the host is using envsubst < draft-spec.yaml > final-spec.yaml. Example if you have spec:

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: busybox
  name: busybox
spec:
  restartPolicy: Never
  containers:
  - name: busybox
    image: busybox
    imagePullPolicy: IfNotPresent
    command: ["ash","-c","echo ${CONTAINER_MESSAGE}"]
    env:
    - name: CONTAINER_MESSAGE
      value: $HOST_MESSAGE

You can run HOST_MESSAGE='hello, world!' envsubst '{$HOST_MESSAGE}' < busybox.yaml | kubectl apply -f -. This will substitute $HOST_MESSAGE with "hello, world!" but will not touch ${CONTAINER_MESSAGE}. This approach does not depends on ConfigMap and it allows you to use kubectl set env to update the variable after deployed.

gohm'c
  • 13,492
  • 1
  • 9
  • 16
0

It seems you set the env var not in the image.

First, you need ensure that you set up env in your image or pods. In image, you need to use ENV in your Dockerfile. doc. In Kubernetes pod, doc.

Second, you mentioned you want to get runtime env vars from your pod, you can run below command.

kubectl exec -it ${POD_NAME} -- printenv
T.R
  • 126
  • 7