kube-forward: A Rust Supervisor for kubectl port-forward That Reconnects After Pod RestartsNote · digest for November 28
Engineering model of kube-forward continuous port tunneling with a resilient bridge over a broken network link
Note3 sections≈ 4 min

kube-forward: A Rust Supervisor for kubectl port-forward That Reconnects After Pod Restarts

You have four terminal tabs running kubectl port-forward: one each for PostgreSQL, Grafana, ArgoCD, and Jaeger. Everything runs smoothly until a teammate ships a deploy or a pod hits an OOMKill. The database goes silent. You check the console—the tunnel is dead or stuck on the old pod, and restarting it throws port already in use.

Why standard forwarding drops and how the supervisor works

kubectl port-forward forwards a local port to a container inside a cluster. Over a long session, two issues show up: the forward is pinned to a single pod and won't switch after a redeploy, and the built-in client only supports TCP—you can't forward UDP services like cluster DNS.

kube-forward fixes this routine—a Rust binary under the MIT license (built on kube-rs and tokio). Release v0.6.2 ships prebuilt binaries for darwin-arm64, linux-amd64, linux-arm64, and windows-amd64.exe, plus scripts/install.sh (Intel macOS builds via Cargo).

Instead of a pile of terminal tabs, a single supervisor process manages everything. It checks that the target pod is still Running and that the channel actually opens. After three consecutive failures for TCP (or five for UDP), it reconnects using exponential backoff with jitter: retry delays grow gradually, and random spread prevents all tunnels from hammering the API server at once.

The biggest bug in DIY reconnect loops is a stuck local socket. Here, the old listener closes before rebinding, and rebinding gets up to three attempts with a one-second pause. For UDP, it opens a local tokio::net::UdpSocket with a reusable session per client address and an idle connection_timeout (1h by default, without dropping active traffic).

One config.yaml for every service: TCP, UDP, validate, and add

All forwards live in a single YAML file. Here is an example for Jaeger UI, PostgreSQL (TCP), and CoreDNS (UDP):

# Jaeger UI with explicit check intervals
- name: "jaeger-ui"
  target: "simple-query.observability"
  ports:
    local: 16686
    remote: 16686
  options:
    retry_interval: 1s
    health_check_interval: 30s
  pod_selector:
    label: "app.kubernetes.io/instance=simple"

# PostgreSQL over TCP (tcp is the default protocol)
- name: "postgres"
  target: "postgres.tr"
  ports:
    local: 5434
    remote: 5432

# CoreDNS over UDP on local port 5454
- name: "coredns"
  target: "kube-dns.kube-system"
  ports:
    protocol: udp
    local: 5454
    remote: 53
  pod_selector:
    label: "k8s-app=kube-dns"

pod_selector finds a live pod by Kubernetes label when the service name differs from the pod name. The options block is optional: defaults are retry_interval: 5s, health_check_interval: 10s, max_retries: 3, and persistent_connection: true. Note one detail: while persistent_connection: true, max_retries is ignored and the supervisor retries indefinitely. If you want it to stop after three tries, set it to false.

To avoid hunting down ports and labels manually, two helper commands are built in:

# Validate config offline without a cluster
kube-forward validate -c config.yaml

# Interactively add a service from the cluster
kube-forward add -c config.yaml

# Start tunnels and test CoreDNS UDP forwarding
kube-forward -c config.yaml
dog google.com @127.0.0.1:5454

validate catches duplicate names, port conflicts, and broken selectors offline (the same checks run on startup). Meanwhile, add reads your kubeconfig (~/.kube/config) in read-only mode, suggests a free port, pulls pod_selector, and re-validates the file before saving. On startup, the console prints a clear summary like ✓ 3/4 established ✗ 1 failed with a reason such as no ready pods found matching selector.

Metrics, logs, and where it fits

Log verbosity is controlled via RUST_LOG (default info), and --expose-metrics (-e, port -m defaulting to 9292) opens a Prometheus endpoint:

# Run with debug logs and metrics on port 9292
RUST_LOG=debug kube-forward -c config.yaml --expose-metrics
curl -s localhost:9292/metrics | grep port_forward

The endpoint exposes four metrics labeled with service="kube-forward" and forward="<name>": counters port_forward_connection_attempts_total, port_forward_connection_successes_total, port_forward_connection_failures_total, and gauge port_forward_connected (1 when up, 0 when down).

The project is young and niche (18 stars, 196 commits, by kainlite) and strictly meant for local debugging. Production needs Ingress, Gateway API, a Service Mesh, or a VPN. Compared to kubefwd (which needs root and edits /etc/hosts) or Telepresence and mirrord, it wins on simplicity: local ports, one YAML file, and zero changes to your OS or cluster.

Want a deep diveVotes help us choose what to break down in detail. This material has 0 votes.
Sources · 3