Telepresence For Kubernetes

Telepresence For Kubernetes

Env file

In this guide, we’ll show you how to capture and reuse environment variables from a remote Kubernetes container when intercepting a service with Telepresence. By exporting these variables into a local .env file, you ensure your local process runs with the exact same configuration as in the cluster.

1. Intercept the Remote Service

Suppose you have a products service deployed in your Kubernetes cluster. To forward traffic from that service to your local machine, run:

telepresence intercept products-depl --port 8000:3000

This command maps remote port 3000 to your local port 8000, but it doesn’t pull in any environment variables by default.

2. Inspect the Deployment’s Environment Variables

Your Kubernetes deployment might define variables like this:

spec:
  containers:
    - name: products
      image: sanjeevkt720/telepresence-products
      ports:
        - containerPort: 3000
          name: web
      env:
        - name: API_URL
          value: http://inventory-service:3000/
        - name: LOG_LEVEL
          value: debug

When you run your service locally, it needs access to API_URL, LOG_LEVEL, and any other container-specific settings.

3. Generate a Local .env File

Telepresence can automatically dump all container environment variables into a file. Simply add the --env-file flag to your intercept command:

telepresence intercept products-depl \
  --port 8000:3000 \
  --env-file .env

After the command runs, you’ll find a .env file in your current directory containing:

API_URL=http://inventory-service:3000/
LOG_LEVEL=debug
# ...other variables

Note

You can choose any filename for the environment file (e.g., dev.env or products.env). Just update your intercept command accordingly.

4. Load the .env File in Your Local Process

Most development tools support dotenv files out of the box. For example, with a Node.js application you can:

npm install dotenv
// index.js
require('dotenv').config();
console.log('API URL:', process.env.API_URL);

Or, if you prefer a one-liner in Bash:

export $(grep -v '^#' .env | xargs) && npm start

Warning

Avoid committing your .env file to version control if it contains sensitive data. Add it to your .gitignore instead.

5. Summary of Key Flags

FlagDescriptionExample
--portForward remote port to local--port 8000:3000
--env-fileDump container environment variables to file--env-file .env

Watch Video

Watch video content

Previous
Demo Telepresence Intercept