Port forwarding is not working on Mac

I’m new to microk8s, and I’m trying out things by depolying a simple apache2 to see things working on my Mac M1:

◼ ~ $ microk8s kubectl run apache --image=ubuntu/apache2:2.4-22.04_beta --port=80
pod/apache created
◼ ~ $ microk8s kubectl get pods
NAME                                   READY   STATUS             RESTARTS       AGE
apache                                 1/1     Running            0              5m37s
◼ ~ $ microk8s kubectl port-forward pod/apache 3000:80
Forwarding from 127.0.0.1:3000 -> 80

but:

◼ ~ $ curl http://localhost:3000
curl: (7) Failed to connect to localhost port 3000 after 5 ms: Connection refused

:thinking: I gues I’m doing something wrong?

Isn’t the gotcha here that on MAC microk8s is running in a vm and that you should use the IP address of your VM instead of localhost ?

good point: I’ve tried to query multipass, the vm, to get the IP and query it, but its ip does not expose the port apparently:

◼ ~ $ microk8s kubectl port-forward pod/apache 8080:80                                                      130
Forwarding from 127.0.0.1:8080 -> 80
◼ ~ $ multipass info --all                                                                                                  1
Name:           primary
State:          Running
IPv4:           192.168.64.3
Release:        Ubuntu 22.04.1 LTS
Image hash:     b27163374c83 (Ubuntu 22.04 LTS)
Load:           0.06 0.02 0.00
Disk usage:     1.6G out of 4.7G
Memory usage:   179.2M out of 961.9M
Mounts:         /Users/mbertamini => Home
                    UID map: 501:default
                    GID map: 20:default

Name:           microk8s-vm
State:          Running
IPv4:           192.168.64.2
                10.1.254.64
Release:        Ubuntu 18.04.6 LTS
Image hash:     192f799a57ab (Ubuntu 18.04 LTS)
Load:           0.83 0.97 1.00
Disk usage:     5.4G out of 48.3G
Memory usage:   873.7M out of 3.9G
Mounts:         --
◼ ~ $ curl http://192.168.64.3:8080
curl: (7) Failed to connect to 192.168.64.3 port 8080 after 1010 ms: Connection refused
◼ ~ $ curl http://192.168.64.2:8080                                                                                         7
curl: (7) Failed to connect to 192.168.64.2 port 8080 after 112 ms: Connection refused

I’ve tried to open the dashboard-proxy, which should work by forwarding a port, and it does work!:

◼ ~/Code/Personal $ microk8s dashboard-proxy
Checking if Dashboard is running.
Dashboard will be available at https://192.168.64.2:10443

...

◼ ~ $ curl -kis -o /dev/null -D - https://192.168.64.2:10443
HTTP/2 200
accept-ranges: bytes
cache-control: no-cache, no-store, must-revalidate
content-type: text/html; charset=utf-8
last-modified: Thu, 10 Jun 2021 12:35:06 GMT
content-length: 1338
date: Wed, 28 Dec 2022 12:11:39 GMT

I think with port-forward you can specify —address=‘0.0.0.0’ - that prefix is double hyphen - so that it binds to all interfaces; after that you-might try to reach it via the vm ip address; currently you only bind to the loopback.

yep, tried it but unsuccessfully:

$ microk8s kubectl port-forward pod/apache 8080:80 --address="0.0.0.0"                                    1
Forwarding from 0.0.0.0:8080 -> 80


....


$ curl https://192.168.64.2:8080                                                                                        7
curl: (7) Failed to connect to 192.168.64.2 port 8080 after 108 ms: Connection refused

I’m starting figuring out what’s going on. I’ve accessed the running VM and just executed the following:

ubuntu@microk8s-vm:~$ sudo microk8s kubectl port-forward pod/apache 8080:80 --address="0.0.0.0"

and from the local machine, outside the VM:

$ curl -kis -o /dev/null -D - http://192.168.64.2:8080
HTTP/1.1 200 OK
Date: Wed, 28 Dec 2022 13:28:29 GMT
Server: Apache/2.4.52 (Ubuntu)
Last-Modified: Tue, 06 Dec 2022 20:21:06 GMT
ETag: "29af-5ef2e8b2bf080"
Accept-Ranges: bytes
Content-Length: 10671
Vary: Accept-Encoding
Content-Type: text/html

it works! the weird thing is that if I now try to execute the port forward from outside the VM, it executes without any problem, like the port is free:

$ microk8s kubectl port-forward pod/apache 8080:80 --address="0.0.0.0"
Forwarding from 0.0.0.0:8080 -> 80

when the expected result I guess it should be this one:

$ microk8s kubectl port-forward pod/apache 8080:80 --address="0.0.0.0"
Unable to listen on port 8080: Listeners failed to create with the following errors: [unable to create listener: Error listen tcp4 0.0.0.0:8080: bind: address already in use]
error: unable to listen on any of the requested ports: [{8080 80}]