Is a feature that kubernetes is sharing all services as env variables?

1/23/2019

I was setting up a laravel and socketcluster install on kubernetes and when try to add redis to laravel got an error about an env variable that i doesn't have defined, so when i print all the env variables in any container i get all the variables from others services like this:

SCC_STATE_PORT_7777_TCP_PORT=7777
KUBERNETES_SERVICE_PORT=443
PHP_PORT_9000_TCP_ADDR=10.35.246.141
SOCKETCLUSTER_SERVICE_PORT=8000
RDB_SERVICE_PORT_DB=28015
REDIS_SERVICE_PORT=6379
SCC_BROKER_PORT_8888_TCP_PROTO=tcp
MARIADB_PORT_3306_TCP=tcp://10.35.247.244:3306
KUBERNETES_PORT_443_TCP_PORT=443
RDB_SERVICE_PORT_WEB=8080
RDB_PORT=tcp://10.35.250.91:28015
RDB_PORT_28015_TCP=tcp://10.35.250.91:28015
KUBERNETES_SERVICE_HOST=10.35.240.1
NGINX_PORT_80_TCP_PORT=80
PHP_SERVICE_PORT=9000
RDB_SERVICE_PORT=28015
RDB_PORT_8080_TCP_ADDR=10.35.250.91
SCC_STATE_PORT_7777_TCP_ADDR=10.35.254.120
SOCKETCLUSTER_PORT=tcp://10.35.244.112:8000
RDB_PORT_28015_TCP_ADDR=10.35.250.91
PHP_PORT=tcp://10.35.246.141:9000
PHP_PORT_9000_TCP=tcp://10.35.246.141:9000
RDB_PORT_28015_TCP_PROTO=tcp
REDIS_PORT_6379_TCP_ADDR=10.35.254.59
MARIADB_PORT_3306_TCP_PORT=3306
SCC_STATE_PORT_7777_TCP_PROTO=tcp
MARIADB_SERVICE_PORT=3306
PHP_SERVICE_HOST=10.35.246.141
PHP_PORT_9000_TCP_PROTO=tcp
RDB_PORT_8080_TCP=tcp://10.35.250.91:8080
RDB_PORT_8080_TCP_PROTO=tcp
REDIS_PORT_6379_TCP_PROTO=tcp
MARIADB_PORT_3306_TCP_ADDR=10.35.247.244
KUBERNETES_PORT_443_TCP_ADDR=10.35.240.1
NGINX_PORT_80_TCP_ADDR=10.35.247.125
REDIS_SERVICE_HOST=10.35.254.59
SCC_BROKER_SERVICE_HOST=10.35.243.129
SCC_STATE_PORT_7777_TCP=tcp://10.35.254.120:7777
NGINX_PORT=tcp://10.35.247.125:80
SOCKETCLUSTER_PORT_8000_TCP_PROTO=tcp
SCC_STATE_SERVICE_PORT=7777
SCC_STATE_PORT=tcp://10.35.254.120:7777
NGINX_PORT_80_TCP_PROTO=tcp
SOCKETCLUSTER_PORT_8000_TCP=tcp://10.35.244.112:8000
RDB_SERVICE_HOST=10.35.250.91
NGINX_SERVICE_PORT_DB=80
MARIADB_PORT_3306_TCP_PROTO=tcp
PHP_PORT_9000_TCP_PORT=9000
SOCKETCLUSTER_PORT_8000_TCP_PORT=8000
SOCKETCLUSTER_PORT_8000_TCP_ADDR=10.35.244.112
REDIS_PORT_6379_TCP=tcp://10.35.254.59:6379
NGINX_PORT_80_TCP=tcp://10.35.247.125:80
SCC_BROKER_PORT_8888_TCP=tcp://10.35.243.129:8888
KUBERNETES_PORT=tcp://10.35.240.1:443
NGINX_SERVICE_PORT=80
RDB_PORT_28015_TCP_PORT=28015
RDB_PORT_8080_TCP_PORT=8080
SCC_BROKER_SERVICE_PORT=8888
SCC_STATE_SERVICE_HOST=10.35.254.120
MARIADB_SERVICE_HOST=10.35.247.244
KUBERNETES_SERVICE_PORT_HTTPS=443
REDIS_PORT=tcp://10.35.254.59:6379
REDIS_PORT_6379_TCP_PORT=6379
SCC_BROKER_PORT=tcp://10.35.243.129:8888
NGINX_SERVICE_HOST=10.35.247.125
SCC_BROKER_PORT_8888_TCP_PORT=8888
MARIADB_PORT=tcp://10.35.247.244:3306
KUBERNETES_PORT_443_TCP_PROTO=tcp
SOCKETCLUSTER_SERVICE_HOST=10.35.244.112
SCC_BROKER_PORT_8888_TCP_ADDR=10.35.243.129
KUBERNETES_PORT_443_TCP=tcp://10.35.240.1:443

when i have this deployments: gke

Any idea if this is a feature, a miss config or what? UPDATE: or if i could disable it?

-- madwyatt
containers
kubernetes

1 Answer

1/23/2019

It's a feature. For every Service in the same namespace, you get OTHERSVC_SERVICE_HOST and OTHERSVC_SERVICE_PORT environment variables, plus some others that come from the legacy Docker links feature. I don't know of any way to turn these off.

Actually using these is problematic in practice, because it depends on the consuming pod starting after the producing service is up, which is hard to guarantee; DNS lookups othersvc.default.svc.cluster.local may not resolve at runtime but won't have a missing environment variable. Conversely, if you might configure your pod with an environment variable named something like MICRO_SERVICE_HOST or DATABASE_PORT, those names are liable to be "stepped on" by the generated environment variables.

-- David Maze
Source: StackOverflow