- DevOpsCube Newsletter
- Posts
- Kubernetes Pod enableServiceLinks
Kubernetes Pod enableServiceLinks
By default, Kubernetes automatically injects environment variables for all services in the same namespace into your pods.
For example,

The environment variables prefixed with KUBERNETES_ and HOSTNAME are automatically injected by Kubernetes and provide information about the Kubernetes environment, such as the API server details
Everything starting with NGINX_
is the service information. As you can see, there are around 7 environment variables for a service.
Here is how it works.
When a pod starts, Kubernetes automatically adds environment variables based on the services in the same namespace. These variables follow a specific format:
<SERVICE_NAME>_SERVICE_HOST
→ The service's cluster IP<SERVICE_NAME>_SERVICE_PORT
→ The service’s port.
If there are 20 Services in a Namespace, and each Service injects 7 environment variables into every Pod, the total number of environment variables per Pod will be, 20×7 =140.
So, each Pod will have 140 extra environment variables just from the Services alone.
This can clutter the environment variables of each Pod.
And some applications may not need these extra environment variables.
How to Disable This Behavior?
You can disable automatic service links by setting,
enableServiceLinks: false
Note: Disabling enableServiceLinks: false
only stops environment variables for user-defined Services in the namespace, but it does not remove the Kubernetes API Service environment variables.
For example,

Here is the output of the environment variables of a pod with this flag enabled. You won't see any service-based environment variables except for Kubernetes service details.

key Takeaway
In most cases, Kubernetes injecting environment variables for Services is not a major issue.
However, there are cases where too many environment service variables cause Pods to go into CrashLoopBackOff
. Read this story for an example.
Reply