krp is a lightweight Kubernetes reverse proxy designed to provide on-demand port forwarding and seamless forwarding to internal Kubernetes resources. The tool facilitates automatic port forwards and provides dynamic routing via loopback addresses.
- On-Demand Port Forwarding β Automatically runs
kubectl port-forwardwhen you need it. - Context Aware β Adapts to changes in your current cluster context.
- Automatic Cleanup β All active port forwards is cleaned up on application exit.
- Dynamic Traffic Routing β Routes through
localhostusing hosts file or WinDivert - Zero Configuration β Once running, the tool requires no further setup or user intervention.
- DnsClient β DNS lookups for HTTP endpoints.
- kubectl port-forward β Forwards pod ports to local ports.
- kubernetes-client/csharp β Tracks context changes and discovers endpoints.
- Spectre.Console β Enables an interactive and visually rich terminal user interface.
- windivert β Intercepts DNS requests for Kubernetes hostnames and redirects traffic to
krp. - YARP β Provides dynamic HTTP(S) routing.
-
Endpoint registration:
Uses static configuration or dynamic discovery for endpoints. -
Routing configuration:
Each endpoint will get a unique loopback address (eg.127.0.0.x myapi.namespace). -
Reverse proxying:
Listens on the local machine and proxies requests to endpoint targets. -
Port forwarding endpoints:
Runskubectl port-forwardand forwards traffic to Kubernetes pods to local ports. Re-uses existing process if already exists, and if the pod dies the process also dies in which case a new one will spawn on-demand. -
HTTP proxy endpoints:
Routes to local port if up, otherwise routes to original IP.
.UseEndpoint(0, 80, "namespace", "service/myapi") // 0 for dynamic local port selection
- Assume your cluster has a service exposed at
myapi.namespace:80. - The DNS will be modified to resolve
myapi.namespaceto127.0.0.x(using hosts file or WinDivert). - Traffic will be proxied to
krp. krpwill find corresponding target endpoint based on loopback address and runkubectl port-forwardto forward traffic to local port.- You can then make requests as if the service was hosted locally:
curl myapi.namespace
.UseHttpEndpoint(5001, "http", "domain.com", "api/service/v2")
- Assume your API gateway is using
domain.com/api/service/v2. - The DNS will be modified to resolve
domain.comto127.0.0.x(using hosts file or WinDivert). - Traffic will be proxied to
krp. krpwill find corresponding service based on loopback address and forwards traffic to local port 5001 if up.- You can then make requests to the URL which will be proxied locally:
curl domain.com/api/service/v2
kubectlmust be installed and authenticated against your Kubernetes cluster.hostsfile modifications requires administrator privileges.windivertfor Windows Packet Filter driver requires administrator privileges.
# Using local code
git clone https://github.com/eddietisma/krp.git
cd krp
dotnet run# Using dotnet tool
dotnet tool install --global dotnet-krp
krp# Using docker
docker compose -f https://raw.githubusercontent.com/eddietisma/krp/main/docker-compose.yml up# Setup HTTPS
dotnet dev-certs https -ep "%USERPROFILE%\.krp\krp.pfx" -p your-cert-password --trustYou can configure port-forwarding and routing behavior as follows:
# krp --help
Usage: krp [options]
Options:
-v|--version Show version information.
-n|--nameserver <NAMESERVERS> DNS server, used for HTTP proxy endpoints
Default value is: 8.8.8.8.
--no-certificate-validation Disable certificate validation
--no-discovery Disable automatic Kubernetes endpoint discovery
--no-ui Disable terminal UI
-f|--forwarder <FORWARDER> Forwarding method
Allowed values are: tcp, http, hybrid.
Default value is: hybrid.
-r|--routing <ROUTING> Routing method
Allowed values are: hosts, windivert.
Default value is: windivert.
-?|-h|--help Show help information.
Environment variables:
KRP_HOSTS Override path to hosts file- Modifies system hosts file (eg.
C:\Windows\System32\drivers\etc\hostson Windows). - Each endpoint gets a unique loopback IPs (e.g.,
127.0.0.x). - Network traffic destined for these loopback IPs is intercepted and redirected to
krp.
Requirements
- Requires administrator privileges to modify the hosts file.
- Requires administrator privileges when running in docker.
- Requires a mounted path and env variable
KRP_HOSTSwhen running in docker.
WinDivert is a Windows packet capture and manipulation tool used by krp to implement a transparent UDP proxy for the DNS protocol. It redirect DNS traffic for endpoint hostnames. When enabled, WinDivert allows krp to dynamically reroute traffic to loopback addresses.
- Captures outgoing DNS requests (UDP/53) matching endpoints hostnames.
- Crafts DNS responses to resolve these hostnames to unique loopback IPs (e.g.,
127.0.0.x). - Network traffic destined for these loopback IPs is intercepted and redirected to
krp.
Requirements
- Only supported on Windows.
- Running WinDivert requires administrator privileges to capture and inject network packets.
Note
WinDiverts installs as an ephemeral windows driver service at runtime. Once stopped, it's automatically removed.
- Supports HTTP requests (only).
- Supports domain based routing (using HTTP headers).
- Multiplexing HTTP/1.1 and HTTP/2 over cleartext using same port without TLS is not supported .
- Uses SSL termination.
- For HTTPS either disable certificate validation on client or setup certificate for each domain.
- Supports low-level TCP requests.
- Supports domain based routing (using domain-based IP per hostname in hosts file).
- Supports low-level TCP requests.
- Supports domain based routing (using domain-based IP per hostname in hosts file)
- Forwards HTTP/x request to
HttpForwarderusing packet inspection.- Inspects TCP traffic and routes HTTP requests to different server ports based on protocol (81 for HTTP/1.1 and 82 for HTTP/2) without TLS requirement.
Note
When running Docker on Windows: No support for domain based routing for low-level TCP due to docker networking limitations. Windows do not yet have full support for host network driver, which results in NAT issues when routing (all loopback IPs will originate from Docker gateway). Limiting routing to HTTP requests only for Windows hosts.
For HTTPS we could use SNI to detect hostnames and use for routing but ran into issues with reacting to network changes due to already established TCP tunnels (need some more work to break existing TCP connections when needed).
To run krp in a Docker container, follow these steps:
-
Start Docker Desktop as an administrator (required for hosts file modification).
-
Build and run the Docker container:
docker buildx bake docker compose up -d -
Authenticate kubectl with cluster:
Most providers will encrypt the auth config for the specific machine. Hence mounting the config folder won't work inside the container.# For AKS docker exec -it $(docker ps --filter "name=krp" --format "{{.ID}}") az login --use-device-code docker exec -it $(docker ps --filter "name=krp" --format "{{.ID}}") kubelogin convert-kubeconfig -l azurecli # For GKE todo.. # For EKS todo...
services:
krp:
build:
context: .
image: eddietisma/krp:latest
container_name: krp
restart: unless-stopped
ports:
- "80:80"
# - "443:443"
environment:
# ASPNETCORE_Kestrel__Certificates__Default__Password: your-cert-password
# ASPNETCORE_Kestrel__Certificates__Default__Path: /root/.krp/krp.pfx
AZURE_CONFIG_DIR: /root/.krp/.azure
KRP_ENDPOINT_EXPLORER: false
KRP_HOSTS: /mnt/hosts
volumes:
- ~/.kube:/root/.kube
- ~/.krp:/root/.krp
- /c/Windows/System32/drivers/etc/:/host_etc/ # win
# - /etc/hosts:/mtn/hosts/ # Linux/macOS- Add integration tests.
- Auto-discovery of Kubernetes services.
- Support for low-level TCP traffic.
- Support for low-level UDP traffic.
- Support for translating internal Kubernetes IPs.
- Eliminate hosts file dependency using WinDivert/PF/iptables (or mitmproxy) for more flexible routing.
- Cross-platform support (Linux/macOS).
- User interface.
- Add GIF recordings of terminal use cases in README.