How do you avoid Kubernetes environment variable collisions

4/13/2018

Presuming you have a Kubernetes cluster with SVC_A that references another service(Postgres in this case) via an environment variables

: "${POSTGRES_HOST:="postgres"}"
: "${POSTGRES_PORT:="5432"}"

POSTGRES_PORT is there a way to avoid the collision of the auto-generated environment variables without changing the service name postgres as this would alter the DNS lookup within the cluster that other services are referring too.

I am aware I could manually set environment variables on the referring services Kubernetes deployment sections but I'd like to avoid this as this seems laborious and error-prone when there are multiple containers referring to the service(Postgres)

relevant service definition from Kubernetes YAML file

apiVersion: v1
kind: Service
metadata:
  name: postgres
spec:
  type: ClusterIP
  selector:
    app: airflow
    tier: db
  ports:
    - protocol: TCP
      port: 5432
      targetPort: postgrestest

Exported environment variables when the referring pod container

declare -x POSTGRES_PORT="tcp://10.100.0.208:5432"
declare -x POSTGRES_PORT_5432_TCP="tcp://10.100.0.208:5432"
declare -x POSTGRES_PORT_5432_TCP_ADDR="10.100.0.208"
declare -x POSTGRES_PORT_5432_TCP_PORT="5432"
declare -x POSTGRES_PORT_5432_TCP_PROTO="tcp"
declare -x POSTGRES_SERVICE_HOST="10.100.0.208"
declare -x POSTGRES_SERVICE_PORT="5432"
declare -x POSTGRES_SERVICE_PORT_POSTGRES="5432"
-- Cris Favero
dockerfile
environment-variables
kubernetes

0 Answers