kubectl on WSL2: "error: error upgrading connection: error sending request: Post"

186 Views Asked by At

i have WSL2 with Ubuntu 22.04.3 LTS on Windows 10, all the latest updates. "kubectl port-forward" command in wsl used to work just fine, exactly like it does on the windows host machine. until today i suddenly discovered that it does not any more (but from windows everything still works). on wsl i am now getting this:

kubectl port-forward --kubeconfig=/home/my_config.yaml --namespace=my_namespace service/my_service 9092:9092

error: error upgrading connection: error sending request: Post "https://remote_ip:port/k8s/clusters/abc/api/v1/namespaces/my_namespace/pods/my_pod/portforward": read tcp local_ip:port->remote_ip:port: read: connection reset by peer

what happenned? i have like three programs installed in wsl, IDE and kubectl, all with default settings, port-forwarding worked in wsl out-of-the box, and now it doesn't..

this command requires kubectl to be able to open a port in wsl? how come it worked and now doesn't?

PS: cluster is perfectly accessible, any of these work:

kubectl get pod
kubectl cluster-info
kubectl config view
kubectl get all --all-namespaces

EDIT: just installed Lens desktop in wsl, it has no problem doing this exact port-forward. so its only this command line use of kubectl, as quoted above, stopped working.

1

There are 1 best solutions below

1
Dion V On

The error "read:connection reset by peer" indicates an issue during the port forwarding establishment, often due to an inability to connect to the kubernetes API server's port of limitations in WSL2 or related applications.

You can try these steps as troubleshooting to fix the concern:

  1. Restart services: you can restart WSL2 by using this command "wsl --shutdown" then followed by "wsl --unregister <distro\_name\>" and then "wsl --register <distro\_name\>". If using Docker within WSL2, restart it: "sudo systemctl restart docker"

  2. Clean Kubernetes Configuration: Delete and re-create your kubeconfig.yaml file using the appropriate method or create a new configuration with kubectl config cluster(...).

  3. Test with nestat: On the WSL2 side, use nestat -lptn before and after running kubectl port-forward to check for opened ports.

  4. Use a different port: Trying a different port number for the forwarded port to rule-out port-specific conflicts.

  5. Verify kubectl installation: Reinstall kubectl if necessary or use a different version that may work.

  6. Check Permissions: Ensure that kubectl has the necessary permissions to access the pod: kubectl auth can-i create podbindings -n my\_namespace

  7. WSL2 Integration Check: If the issue started after the new software or WSL2 updates, temporarily remove or rollback to isolate the cause