Create service environment variable Kubernetes

11/3/2021

I was wondering if this is possible. In a lot of my K8s deployments, I will have a Postgres database, sometimes the service is called Postgres, other times Postgresql (depending on which Docker image I use). Kubelet will automatically add {SVCNAME}_SERVICE_HOST and {SVCNAME}_SERVICE_PORT environment variables, and I was wondering if I could create another (consistent naming) EV from this variable? The reason I ask is because that is how I construct my connection string, so if this changes I need to rebuild the image. Thanks!

-- user1314147
kubernetes
postgresql
service-discovery

1 Answer

11/3/2021

Yes, you can use that unless you are not changing anything for postgres service

A list of all services that were running when a Container was created is available to that Container as environment variables.

Example :

HOSTNAME=something-fddf-123123123-gbh45
SHLVL=1
HOME=/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_SERVICE_PORT_HTTPS=80
KUBERNETES_SERVICE_HOST=x.x.x.x
PWD=/

Ref : https://kubernetes.io/docs/concepts/containers/container-environment/

-- Harsh Manvar
Source: StackOverflow