[Kubernetes (K8S)] Use kubefwd to forward Kubernetes (K8S) ports for Local Development

kubefwd

kubefwd is a command line utility built to port forward multiple services within one or more namespaces on one or more Kubernetes clusters. kubefwd uses the same port exposed by the service and forwards it from a loopback IP address on your local workstation. kubefwd temporally adds domain entries to your /etc/hosts file with the service names it forwards.

kubefwd helps to enable a seamless and efficient way to develop applications and services on a local workstation. Locally develop applications that intend to interact with other services in a Kubernetes cluster. kubefwd allows applications with connection strings like http://elasticsearch:9200/ or tcp://db:3306 to communicate into the remote cluster. kubefwd can be used to reduce or eliminate the need for local environment specific connection configurations.

Architecture

Prerequisites

  • Kubernetes (K8S)
    Kubernetes (K8s) is an open-source system for automating deployment, scaling, and management of containerized applications.

  • Homebrew
    Homebrew is the Missing Package Manager for macOS (or Linux).

    For more information about installing and using Homebrew, see the Homebrew - https://brew.sh/.

Installation

If you are running MacOS and use homebrew you can install kubefwd directly from the txn2 tap:

1
2
3
4
# macOS
$ brew install txn2/tap/kubefwd

$ brew upgrade kubefwd

Usage

Forward all services for the namespace the-project. Kubefwd finds the first Pod associated with each Kubernetes service found in the Namespace and port forwards it based on the Service spec to a local IP address and port. A domain name is added to your /etc/hosts file pointing to the local IP.

1
$ sudo kubefwd services -n the-project

Execute a curl call to the service sin your Kubernetes cluster:

1
2
3
$ curl -s the-project:80

$ curl -s the-project:443

Docker

Forward all services from the namespace the-project to a Docker container named the-project

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ docker run -it --rm --privileged --name the-project \
-v $HOME/.kube/config:/root/.kube/config \
txn2/kubefwd services -n the-project
INFO[02:10:46] _ _ __ _
INFO[02:10:46] | | ___ _| |__ ___ / _|_ ____| |
INFO[02:10:46] | |/ / | | | '_ \ / _ \ |_\ \ /\ / / _ |
INFO[02:10:46] | <| |_| | |_) | __/ _|\ V V / (_| |
INFO[02:10:46] |_|\_\\__,_|_.__/ \___|_| \_/\_/ \__,_|
INFO[02:10:46]
INFO[02:10:46] Version 1.18.1
INFO[02:10:46] https://github.com/txn2/kubefwd
INFO[02:10:46]
INFO[02:10:46] Press [Ctrl-C] to stop forwarding.
INFO[02:10:46] 'cat /etc/hosts' to see all host entries.
INFO[02:10:46] Loaded hosts file /etc/hosts
INFO[02:10:46] HostFile management: Backing up your original hosts file /etc/hosts to /root/hosts.original
INFO[02:10:46] Successfully connected context: kubernetes-admin@kubernetes
INFO[02:10:46] Port-Forward: 127.1.27.1 the-project:80 to pod the-project-c44fd599c-shbdg:80
INFO[02:10:46] Port-Forward: 127.1.27.1 the-project:443 to pod the-project-c44fd599c-shbdg:443

Execute a curl call to the service sin your Kubernetes cluster:

1
2
3
$ docker exec the-project curl -s the-project:80

$ docker exec the-project curl -s the-project:443

Help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
$ kubefwd svc --help

INFO[20:48:38] _ _ __ _
INFO[20:48:38] | | ___ _| |__ ___ / _|_ ____| |
INFO[20:48:38] | |/ / | | | '_ \ / _ \ |_\ \ /\ / / _ |
INFO[20:48:38] | <| |_| | |_) | __/ _|\ V V / (_| |
INFO[20:48:38] |_|\_\\__,_|_.__/ \___|_| \_/\_/ \__,_|
INFO[20:48:38]
INFO[20:48:38] Version 1.11.0
INFO[20:48:38] https://github.com/txn2/kubefwd
INFO[20:48:38]
Forward multiple Kubernetes services from one or more namespaces. Filter services with selector.

Usage:
kubefwd services [flags]

Aliases:
services, svcs, svc

Examples:
kubefwd svc -n the-project
kubefwd svc -n the-project -l app=wx,component=api
kubefwd svc -n default -n the-project
kubefwd svc -n default -d internal.example.com
kubefwd svc -n the-project -x prod-cluster
kubefwd svc -n the-project -m 80:8080 -m 443:1443


Flags:
-x, --context strings specify a context to override the current context
-d, --domain string Append a pseudo domain name to generated host names.
--exitonfailure Exit(1) on failure. Useful for forcing a container restart.
-h, --help help for services
-c, --kubeconfig string absolute path to a kubectl config file
-n, --namespace strings Specify a namespace. Specify multiple namespaces by duplicating this argument.
-l, --selector string Selector (label query) to filter on; supports '=', '==', '!=' (e.g. -l key1=value1,key2=value2) and 'in' (e.g. -l "app in (value1, value2)").
-m, --mapping strings Specify a port mapping. Specify multiple mapping by duplicating this argument.
-v, --verbose Verbose output.

References

[1] Kubernetes Port Forwarding for Local Development - IMTI - Craig Johnston - https://imti.co/kubernetes-port-forwarding/

[2] GitHub - txn2/kubefwd: Bulk port forwarding Kubernetes services for local development. - https://github.com/txn2/kubefwd

[3] Kubectl Reference Docs - https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#port-forward

[4] Home - Telepresence - https://www.telepresence.io/

[5] Debugging k8s services: 3 tools for 3 scenarios · Erkan Erol - https://erkanerol.github.io/post/debugging-k8s-services/