#devops #kubernetes #tools

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:

1$ 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:

1$ brew install txn2/tap/kubefwd

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

 1$ sudo kubefwd svc -n default
 2INFO[13:36:14]  _          _           __             _
 3INFO[13:36:14] | | ___   _| |__   ___ / _|_      ____| |
 4INFO[13:36:14] | |/ / | | | '_ \ / _ \ |_\ \ /\ / / _  |
 5INFO[13:36:14] |   <| |_| | |_) |  __/  _|\ V  V / (_| |
 6INFO[13:36:14] |_|\_\\__,_|_.__/ \___|_|   \_/\_/ \__,_|
 7INFO[13:36:14]
 8INFO[13:36:14] Version 1.17.3
 9INFO[13:36:14] https://github.com/txn2/kubefwd
10INFO[13:36:14]
11INFO[13:36:14] Press [Ctrl-C] to stop forwarding.
12INFO[13:36:14] 'cat /etc/hosts' to see all host entries.
13INFO[13:36:14] Loaded hosts file /etc/hosts
14INFO[13:36:15] Successfully connected context: cluster02
15INFO[13:36:15] Port-Forward: 127.1.27.1 dev-redis:6379 to pod dev-redis-866545f5b7-sjf4w:6379
16INFO[13:36:15] Port-Forward: 127.1.27.2 example-server:80 to pod example-server-6bd95bb46c-wqr6c:8080
17INFO[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:

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

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

1$ curl http://example-server -i
2HTTP/1.1 200 OK
3Content-Type: application/json
4published_at: Mon, 30 Nov 2020 12:43:08 GMT
5Content-Length: 79
6
7{"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.