Telepresence

repository·release/v2·Indexed 11 days ago

https://github.com/telepresenceio/telepresence

A tool for local Kubernetes development that bridges a developer's workstation and a remote cluster, allowing local processes to act as if they are part of the cluster network. It includes server-side components manageable via Helm for configuring traffic managers, agent injectors, and networking options like QUIC tunnels.

Tokens
203K
Snippets
541
Records
845
Agent score
91%

What's inside Telepresence

  1. What is Telepresence

    release/v2
    Telepresence is a CNCF project designed for fast, local development for Kubernetes. It connects your workstation to a Kubernetes cluster, allowing you to code and debug services locally using your own IDE, debugger, and hot reload capabilities. It enables your local process to receive real traffic from the cluster, bypassing the traditional container build/push/deploy cycle.
  2. Use Telepresence Docker Plugins for isolated development

    release/v2

    When starting a Telepresence client with telepresence connect --docker, the client runs inside a Docker container. This isolation ensures that the network and volume mounts created by Telepresence do not interfere with your workstation's local network or filesystem.

    To enable this, Telepresence uses two specialized Docker plugins:

    1. Teleroute Network Plugin: Allows other Docker containers to reach cluster networks via a dedicated Docker network.
    2. Telemount Volume Plugin: Allows remote directories (provided via SFTP by traffic agents) to be mounted as standard Docker volumes.

    These plugins are installed automatically on demand when you run telepresence connect --docker.

    telepresence connect --docker
  3. Understand Connections and Networking

    release/v2

    Connection

    A connection is the link between your workstation and a cluster, established via telepresence connect. It makes the cluster's network reachable locally and is a prerequisite for creating any attachment. You can have multiple named connections active simultaneously.

    Virtual network interface (VIF)

    The network device created by the Root daemon upon connection. It routes cluster subnets so that local tools can reach cluster services.

  4. Understand Telepresence session credentials

    release/v2

    Telepresence uses session credentials to secure communication between clients and agents. These credentials are minted by the traffic-manager and expire every 24 hours. There are two forms of credentials used depending on the transport layer:

    1. Client Certificate: Used for TLS-based transports, specifically the QUIC tunnel's mutual-TLS handshake. The CommonName is set to the session ID.
    2. Signed Bearer Token: Used for transports that cannot carry a certificate, such as:
      • FTP: Used as the FTP password.
      • gRPC Metadata: Used for tunnel and dial-watcher calls on the agent's gRPC surface.

    Credentials are regenerated whenever the traffic-manager restarts, which revokes all outstanding credentials.

  5. Understand Telepresence file-sharing authentication

    release/v2

    Telepresence uses two forms of credentials rooted in a single Certificate Authority (quictunnel.CA) to secure file-sharing and gRPC communication. The authentication enforcement level is tied to the manager's security.authentication.mode:

    • disabled / permissive: Credentials are verified and failures are logged, but connections without credentials are still allowed (maintains backward compatibility).
    • enforcing: No valid session credential is permitted for any file operation or connection.

    Credential Types

    1. X.509 session client certificate: Used for TLS-based transports like the QUIC tunnel. The Common Name (CN) is set to the sessionID.
    2. Signed session token: A compact bearer string used for transports that cannot carry certificates (e.g., FTP PASS command or gRPC metadata on plaintext port-forwarded channels). The format is v1.<sessionID>.<expiry>.<base64url ECDSA-sig by the CA key>.
  6. Choose between the sidecar and the node-agent

    release/v2

    Telepresence offers two modes for attaching to workloads:

    1. Sidecar Mode (Default): Uses a mutating webhook to inject an agent container into your pods. This requires a pod restart and modifies the workload's pod template.
    2. Node-agent Mode: Runs as a node-pinned Kubernetes Job that enters the existing target pod's Linux namespaces from the outside. This requires no workload mutation and no pod restart.

    When to use node-agent

    • When mutating or restarting the workload is undesirable or disallowed (e.g., expensive startup times).
    • When workloads are managed by an operator that conflicts with the sidecar webhook.
    • In clusters where the agent-injector is disabled (agentInjector.enabled=false).

    Trade-offs and Requirements

    • Privilege: Node-agents require high privileges (hostPID: true, SYS_ADMIN, SYS_PTRACE, NET_ADMIN, and NET_RAW).
    • Security Standards: The traffic-manager namespace must permit the privileged Pod Security Standard.
    • Incompatibility: Clusters that reject hostPID (like GKE Autopilot) cannot run node-agents; you must use the sidecar mode there.
    # To use node-agent for a specific attachment, use the --node-agent flag
    telepresence intercept <service> --node-agent
    
    # To use node-agent for wiretap
    telepresence wiretap <service> --node-agent
    
    # To use node-agent for ingest
    telepresence ingest <service> --node-agent
  7. Choose a Telepresence installation method

    release/v2

    Telepresence offers two primary installation methods depending on your needs for privilege management:

    1. Standalone binaries: Manual installation where the root daemon runs on-demand with elevated privileges. Best for users who want manual control.
    2. Platform installers: Bundles Telepresence with a system service for the root daemon. This eliminates the need for repeated privilege elevation by running the daemon as a persistent background service.

    Installers are available for Windows, macOS, and Linux.

  8. Implement certificate-scoping for QUIC authorization

    release/v2

    To achieve RBAC-equivalent security on the QUIC path, authorization must be embedded within the client certificate, as the agent (which terminates the connection) is the only component that can enforce it.

    Two implementation strategies were evaluated:

    Option A: Session-wide certificate with namespace list

    • Mechanism: The manager embeds an allowed set of namespaces in a single session certificate using Authorizer.CanPortForward. The agent verifies this list during the TLS handshake.
    • Pros: Single certificate for the session.
    • Cons: The scope is frozen for the certificate's lifetime (e.g., 24h via clientCertValidity). Revoking RBAC does not take effect until the certificate expires.
    • Mechanism: The manager provides a new RPC GetQuicAgentClientCert(session, podUID). This mints a short-lived certificate specifically for one agent, naming that agent's identity. The agent verifies that the presented certificate matches its own SNI.
    • Pros:
      • Authorizes against the actual target.
      • Supports per-pod-name RBAC via CanPortForward's fallback.
      • Naturally short-lived (minutes instead of hours).
      • Composes well with session binding requirements.
    • Cons: Requires one extra unary RPC per agent per session.
  9. How the fixture engine works

    release/v2

    The framework uses rt.Fixture[T] to manage resources via declarative, memoized specs.

    Key Concepts

    • Memoization: rt.Get(t, f) returns a resource. If the resource exists and matches the spec, it is reused. If not, it is provisioned. A provisioning failure for a fixture will cause all subsequent tests requiring that fixture to be skipped.
    • Mutation: rt.Mutate(t, f) is used when a test needs to change a shared resource (like a manager spec or a workload). It performs a Get followed by invalidation at the end of the test, ensuring the next test re-provisions the new state.
    • Lazy Provisioning: Suites do not provision in SetupSuite. Resources are only provisioned when an accessor (like s.Manager()) is called. This allows filtered test runs to be extremely fast.
    • Mutual Exclusivity: Manager specs are mutually exclusive. Provisioning a new spec invalidates all previous specs and connections, as the rollout replaces the pods used for port-forwarding.
    • Workload Lifecycle: Workloads are always destroyed at the end of a run (even in dev mode) to prevent node resource exhaustion. If a workload manifest changes, it is deleted and recreated.
    • Adoption: In dev mode (without RTEST_TEARDOWN), the framework attempts to 'adopt' existing namespaces, releases, and connections from previous runs to speed up subsequent tests.
  10. How the Telemount Volume Plugin works

    release/v2

    The Telemount plugin allows remote directories from Kubernetes traffic agents to be mounted as Docker volumes in your local containers. This is used during attachment commands like telepresence ingest, intercept, replace, or wiretap.

    Workflow:

    1. The daemon starts a bridge mounter that proxies the traffic agent's SFTP server to the daemon's localhost.
    2. When attaching a container (using --docker-run, --docker-build, or --docker-debug), Telepresence merges volume flags from the command line (-v, --volume, --mount) with volumes propagated from the remote pod's traffic agent. Command-line flags take priority.
    3. For each volume, Telepresence executes docker create volume --driver=telemount, passing the SFTP proxy port.
    4. The container is started with the resulting volumes attached.
    5. Upon detachment, volumes are unmounted and removed, and the SFTP proxy is closed.

    Configuration:

    • The plugin registry, name, and tag can be configured via docker.telemount in config.yml.
  11. How DNS resolution works in Telepresence

    release/v2

    Telepresence uses a dynamic DNS resolver to map cluster service names to IPs.

    Namespace Resolution Rules:

    • Current Namespace: Processes running locally can reach services in the currently managed namespace using just the service-name.
    • Other Namespaces: To reach services in other managed namespaces, use the format <service-name>.<namespace>.

    Configuration & Customization:

    • You can expand the list of names Telepresence resolves by using the includeSuffixes or mappings options in the cluster DNS configuration.
    • Query Types Supported: A, AAAA, CNAME, MX, NS, PTR, SRV, and TXT.
    • Caching: Telepresence uses short TTLs (Time To Live) for records to ensure quick eviction when configurations change. It also maintains an internal cache to minimize the load on the cluster resolver.
  12. Configure the Traffic Manager agent mode (Sidecar vs Node-agent)

    release/v2

    When a developer attaches to a workload, the Traffic Manager places a traffic-agent next to the application. You can choose between two modes:

    1. Sidecar (Default): The agent-injector webhook adds the agent as a sidecar container. This requires restarting the workload's pods once.
    2. Node-agent: A node-hosted agent that attaches to existing pods without modifying or restarting them. This requires running with privileged permissions.

    To enable the node-agent during installation:

    telepresence helm install --set nodeAgent.enabled=true