Kubernetes port forwarding for local development, Contributions welcome!
Read Kubernetes Port Forwarding for Local Development for background and a detailed guide to kubefwd.
kubefwd is a command line utility built to port forward some or all pods within a Kubernetes namespace. 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.
When working on our local workstation, my team and I often build applications that access services through their service names and ports within a Kubernetes namespace. kubefwd allows us to develop locally with services available as they would be in the cluster.
Tested directly on macOS and Linux based docker containers.
kubefwd assumes you have kubectl installed and configured with access to a Kubernetes cluster. kubefwd uses the kubectl current context. The kubectl configuration is not used. However, its configuration is needed to access a Kubernetes cluster.
Ensure you have a context by running:
kubectl config current-context
If you are running MacOS and use homebrew you can install kubefwd directly from the txn2 tap:
brew install txn2/tap/kubefwd
To upgrade:
brew upgrade kubefwd
scoop install kubefwd
To upgrade:
scoop update kubefwd
Forward all services from the namespace the-project to a Docker container named the-project:
docker run -it --rm --privileged --name the-project \
-v "$(echo $HOME)/.kube/":/root/.kube/ \
txn2/kubefwd services -n the-project
Execute a curl call to an Elasticsearch service in your Kubernetes cluster:
docker exec the-project curl -s elasticsearch:9200
Check out the releases section on Github for alternative binaries.
Fork kubefwd and build a custom version. We welcome any useful pull requests.
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.
sudo kubefwd svc -n the-project
Forward all svc for the namespace the-project
where labeled system: wx
:
sudo kubefwd svc -l system=wx -n the-project
$ kubefwd svc --help
2019/03/09 21:13:18 _ _ __ _
2019/03/09 21:13:18 | | ___ _| |__ ___ / _|_ ____| |
2019/03/09 21:13:18 | |/ / | | | '_ \ / _ \ |_\ \ /\ / / _ |
2019/03/09 21:13:18 | <| |_| | |_) | __/ _|\ V V / (_| |
2019/03/09 21:13:18 |_|\_\\__,_|_.__/ \___|_| \_/\_/ \__,_|
2019/03/09 21:13:18
2019/03/09 21:13:18 Version 1.7.3
2019/03/09 21:13:18 https://github.com/txn2/kubefwd
2019/03/09 21:13:18
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 env=dev,component=api
kubefwd svc -n default -l "app in (ws, api)"
kubefwd svc -n default -n the-project
kubefwd svc -n default -d internal.example.com
kubefwd svc -n the-project -x prod-cluster
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 fil (default "/Users/cjimti/.kube/config")
-n, --namespace strings Specify a namespace. Specify multiple namespaces by duplicating this argument.
-l, --selector string Selector (label query) to filter on; supports '=', '==', and '!=' (e.g. -l key1=value1,key2=value2).
-v, --verbose Verbose output.
Local
go run ./cmd/kubefwd/kubefwd.go
Run in the golang:1.11.5 docker container:
docker run -it --rm --privileged \
-v "$(pwd)":/kubefwd \
-v "$(echo $HOME)/.kube/":/root/.kube/ \
-w /kubefwd golang:1.11.5 bash
sudo go run -mod vendor ./cmd/kubefwd/kubefwd.go svc
Build test release:
goreleaser --skip-publish --rm-dist --skip-validate
Build and release:
GITHUB_TOKEN=$GITHUB_TOKEN goreleaser --rm-dist
multipass launch -n testvm
cd ./dist
multipass copy-files *.snap testvm:
multipass shell testvm
sudo snap install --dangerous kubefwd_64-bit.snap
Apache License 2.0
Opens source utility proudly sponsored by Deasil Works, Inc. & Craig Johnston