Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kubernetes Port Forwarding - Error listen tcp4 127.0.0.1:88: bind: permission denied

I am using minikube on my local machine. Getting this error while using kubernetes port forwarding. Can anyone help?

mjafary$ kubectl port-forward  sa-frontend 88:80

Unable to listen on port 88: All listeners failed to create with the following errors: 
Unable to create listener: Error listen tcp4 127.0.0.1:88: bind: permission denied, Unable to create listener: Error listen tcp6 [::1]:88: bind: permission denied
error: Unable to listen on any of the requested ports: [{88 80}] 
like image 982
Jaf Avatar asked Dec 14 '18 07:12

Jaf


People also ask

How port forwarding works in Kubernetes?

Port Forwarding in Kubernetes You can use kubectl to set up a proxy that will forward all traffic from a local port that you specify to a port associated with the Pod that you determine. This is especially useful when you want to directly communicate from your local machine to a given port on a Pod.

How do I get rid of port forwarding in Kubernetes?

The port is only forwarded while the kubectl process is running, so you can just kill the kubectl process that's forwarding the port. In most cases that'll just mean pressing CTRL+C in the terminal where the port-forward command is running.

How do you check what port a pod is listening on with kubectl?

shell into the pod and try running netstat -tulpn gives you all the ports open.

What port does kubectl use?

By default, the Kubernetes API server listens on port 6443 on the first non-localhost network interface, protected by TLS.


2 Answers

kubectl fails to open the port 88 because it is a privileged port. All ports <1024 require special permissions.

There are many ways to solve your problem.

  • You can stick to ports >= 1024, and use for example the port 8888 instead of 88: kubectl port-forward sa-frontend 8888:80
  • You could use kubectl as root: sudo kubectl port-forward sa-frontend 88:80 (not recommended, kubectl would then look for its config as root)
  • You could grant the kubectl binary the capability to open privileged ports. This answer explains in depth how to do this.

If you want to go for the 3rd option, here is a short way of doing it:

sudo setcap CAP_NET_BIND_SERVICE=+eip /usr/bin/kubectl

This will let kubectl open any port while still running with the privileges of a regular user. You can check if this worked by using

sudo getcap /usr/bin/kubectl 
/usr/bin/kubectl = cap_net_bind_service+eip

Be aware that this grants the permission to whoever uses the binary. If you want finer grained permissions, use authbind.

Note: as ng-sek-long commented, kubectl is not necessarily installed as /usr/bin/kubectl. You should replace it with the path to the kubectl binary on your machine.

like image 81
user48678 Avatar answered Sep 19 '22 17:09

user48678


As mentioned by user48678 you can bypass the limitation using sudo.

Add -E flag to pass environment.

mjafary$ sudo -E kubectl port-forward  sa-frontend 88:80

If you don't pass -E, the KUBECONFIG environment variable will not be set for example.

like image 42
vhs Avatar answered Sep 17 '22 17:09

vhs