products deployment with Telepresence, exporting its Kubernetes environment variables into a .env file, and running the service locally so it can call the inventory service exactly as it does inside the cluster.
1. Reproducing the Error
First, start an intercept on theproducts deployment:
API_URL is undefined in your local environment.
2. Application Code (index.js)
The service reads API_URL from the environment and queries the inventory service:
3. Kubernetes Deployment Spec
Normally, the Pod spec injectsAPI_URL:
4. Pulling Environment Variables with Telepresence
-
Stop and remove the current intercept
-
Restart with
.envdump and mountThis generatesmy-envs.env:
The
--mount=true flag ensures your local process can read my-envs.env as if it were on your filesystem.5. Loading the .env File in Node.js
Install and configure dotenv:
index.js, override existing env vars:
process.env.API_URL matches the cluster’s value.