Is it possible to disable service discovery with environment variables in kubernetes?

Viewed 2547

As we know, kubernetes supports 2 primary modes of finding a Service - environment variables and DNS, could we disable the first way and only choose the DNS way?

2 Answers

As shown in this PR, this feature will land with Kubernetes v1.13. From the PR (as Docs are not available yet) I expect it to be the field enableServiceLinks in the pod spec with true as default.

Edit: It has been a while and the PR finally landed. The enableServiceLinks was added as an optional Boolean to the Kubernetes PodSpec.

For the record: using DNS to discover service endpoints is the recommended approach. The docker link behavior, from where the environment variables originate, has long been deprecated.

Per kubernetes v1.8 source, it's impossible to disable services discovery with environment variables.

Only service meet either condition is exposed by envVars.

  • service in the same namespace as the pod;
  • kubernetes service in the default namespace;

Even though, these environment variables can be overwritten by env and envFrom defined in pod template.

I'm wondering what's your scenario, maybe we can figure out some workaround.

Related