Home > Mobile >  Not able to access env variables in kubernetes pod
Not able to access env variables in kubernetes pod

Time:05-02

In my pod I have some env variables. I have written a bash script to get inside the pod and connect to database using the env variables. But am not able to use the env variables.

kubectl exec $POD_ID  -- /bin/bash -c """
     printenv DATABASE_HOST_NAME
     echo $DATABASE_HOST_NAME
     psql -h $DATABASE_HOST_NAME -U DATABASE_USER
"""

Here printenv returns the correct env variable value. echo returns empty. And psql statement doesn't take the host. Anyone has any idea how i can use the env variables in the pod in my psql connection statement.

When i manually get in the container and run the psql command with env variable it works file.But when running the script it shows the error.

CodePudding user response:

You can try this as a workaround:

psql -h $(printenv DATABASE_HOST_NAME)

However, this can help: https://www.reddit.com/r/linuxquestions/comments/e2x2xv/differences_between_printenv_and_echo/&ved=2ahUKEwiCwZ3vkMD3AhX_jokEHSKLCAgQjjh6BAgHEAE&usg=AOvVaw2zSOnGM16E76x8u70_9vVT

CodePudding user response:

You can check this post I answered days ago: Can't access postgres via service from the postgres container itself

With this configuration I am able to access the env variable I have configured.

bguess

CodePudding user response:

As DATABASE_HOST_NAME is defined in k8s pod, you need single quote to avoid $DATABASE_HOST_NAME to be expanded on the host.

kubectl exec $POD_ID  -- /bin/bash -c '
     printenv DATABASE_HOST_NAME
     echo $DATABASE_HOST_NAME
     psql -h $DATABASE_HOST_NAME -U DATABASE_USER
'
  • Related