Environment Connection

Overview

Environment Connection allows workspaces to connect to environments and access their services directly by name. When a workspace connects to an environment, its network namespace switches to enable seamless access to all services running in that environment.

Network Namespace Switching

When you connect a workspace to an environment, the workspace's network namespace switches to access services from the environment by their names (e.g.,postgres,redis).

How It Works

1

Workspace Without Connection

By default, a workspace runs in its own isolated network namespace. It has access to external internet but cannot directly access environment services.

2

Connect to Environment

When you connect the workspace to an environment, the workspace's network configuration changes. The workspace can now resolve environment service names through DNS.

3

Service Access by Name

Services are now accessible using their service names as hostnames. For example, connect to PostgreSQL at postgres:5432 or Redis at redis:6379.

Connecting to Environment

Use the kl env connect command from within your workspace to connect to an environment. This switches your workspace's network namespace to access environment services.

Interactive Mode (Recommended)

Run the command without arguments to see a list of available environments and select one interactively using fuzzy-find.

kl env connect

This will display all available environments and let you select using arrow keys and search.

Connect to Specific Environment

If you know the environment name, you can connect directly:

kl env connect my-env
kl env connect team-env
kl e c test-env          # Using aliases

What Happens After Connection

  • Network Namespace Switch: Your workspace's network configuration changes to the environment's namespace
  • DNS Resolution: Service names in the environment become resolvable as hostnames
  • Immediate Access: You can now access services using their short names (e.g., postgres, redis)

Accessing Services by Name

After connecting to an environment, all services running in that environment become accessible using their service names as hostnames. You no longer need fully qualified domain names or IP addresses.

How It Works

When you connect to an environment, your workspace's DNS configuration is updated to resolve service names to their cluster IPs. This means:

  • Service postgres resolves to its cluster IP
  • Service redis resolves to its cluster IP
  • Service api-server resolves to its cluster IP

From Terminal

Access services directly from your workspace terminal using their service names:

# Connect to PostgreSQL

psql -h postgres -p 5432 -U myuser -d myapp

# Connect to Redis

redis-cli -h redis -p 6379

# Ping a service

ping postgres

# Check service connectivity

curl http://api-server:8080/health

In Application Code

Use service names directly in your application configuration and code:

# Environment Variables (.env)

DATABASE_URL=postgresql://user:pass@postgres:5432/myapp
REDIS_URL=redis://redis:6379
API_ENDPOINT=http://api-server:8080

# Node.js Example

const redis = require('redis');
const client = redis.createClient({
  host: 'redis',
  port: 6379
});

# Python Example

import psycopg2

conn = psycopg2.connect(
    host="postgres",
    port=5432,
    database="myapp"
)

Find Available Services

Use kl env status to see all services available in the connected environment:

kl env status

This command shows the environment name, namespace, and lists all available services with their ports.

Port Numbers

Always specify the port number when connecting to services (e.g., postgres:5432). The service name alone won't include the port.

Use Cases

Development Against Real Services

Develop your application code in the workspace while connecting to real databases, caches, and APIs running in the environment

Database Migrations

Run database migrations from your workspace directly against the environment database

Testing & Debugging

Test your code against production-like services and debug issues in a realistic environment setup

Switch Between Environments

Connect to different environments as needed to test against various service configurations and data sets

Seamless Development Experience

Environment connection eliminates the need to run services locally or manage complex port forwarding. Simply connect and start coding against real services.