When running a lot of services in a Kubernetes cluster, one of the harder things to get right is how to access these from your local computer. If you only have one or two services, you can use the kubectl port-forward command:

$ kubectl --namespace default --address 0.0.0.0 port-forward my-pod 9000

As soon as you have more of them, this becomes hard to manage. Also, you have to remember the proper pod names and their associated port numbers. Also, when you have multiple services with the same port number, it becomes even harder as the port-forward requires a unique port number for each instance.

Last week, I did some research to see if there are better ways for managing this. The best solution I found is called kubefwd. It's described as:

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.

Installing it can be done on Mac via homebrew:

$ brew install txn2/tap/kubefwd

Once installed, you can run it using sudo with a specific namespace:

$ sudo kubefwd svc -n default
INFO[13:36:14]  _          _           __             _
INFO[13:36:14] | | ___   _| |__   ___ / _|_      ____| |
INFO[13:36:14] | |/ / | | | '_ \ / _ \ |_\ \ /\ / / _  |
INFO[13:36:14] |   <| |_| | |_) |  __/  _|\ V  V / (_| |
INFO[13:36:14] |_|\_\\__,_|_.__/ \___|_|   \_/\_/ \__,_|
INFO[13:36:14]
INFO[13:36:14] Version 1.17.3
INFO[13:36:14] https://github.com/txn2/kubefwd
INFO[13:36:14]
INFO[13:36:14] Press [Ctrl-C] to stop forwarding.
INFO[13:36:14] 'cat /etc/hosts' to see all host entries.
INFO[13:36:14] Loaded hosts file /etc/hosts
INFO[13:36:15] Successfully connected context: cluster02
INFO[13:36:15] Port-Forward: 127.1.27.1 dev-redis:6379 to pod dev-redis-866545f5b7-sjf4w:6379
INFO[13:36:15] Port-Forward: 127.1.27.2 example-server:80 to pod example-server-6bd95bb46c-wqr6c:8080
INFO[13:36:15] Port-Forward: 127.1.27.3 prod-redis:6379 to pod prod-redis-7955fc5d6f-ljcsp:6379

Upon startup, it gets the list of services, sets up a unique IP address for it locally (basically an alias for 127.0.0.1) and adds it to the /etc/hosts file with the name of service as the hostname. In our example, this means that the following hostnames are then available:

  • dev-redis
  • example-server
  • prod-redis

You can then connect to these services using the aliases. To connect to dev-redis, you can do:

$ redis-cli -h dev-redis
dev-redis:6379>

For a webserver like example-server, you can use cURL to connect:

$ curl http://example-server -i
HTTP/1.1 200 OK
Content-Type: application/json
published_at: Mon, 30 Nov 2020 12:43:08 GMT
Content-Length: 79

{"headers":{"Accept":"*/*","User-Agent":"curl/7.64.1"},"host":"example-server"}

You can read more about the background of why they developed kubefwd here. The source code can be found on github.com/txn2/kubefwd.