How can I expose a custom port on k8s?

6/13/2019

The script I want to deploy listens to :4123 and k8s probably expose only :80 by default. How can I expose :4123 such that my script will be able to accept requests?

I tried port forwarding but there's a permission error to forward :80 to :4123 and k8s didn't allow to deploy an image that listens to :80 (since it's probably busy already).

-- James Larkin
kubernetes

2 Answers

6/14/2019

As advised by @fiunchinho, local port forwarding might help in your case. Adding --address 0.0.0.0 to this command makes it avaiable for all your interfaces like below:

$ kubectl port-forward --address 0.0.0.0 nginx-55bd7c9fd-6fpnx 8888:80

You can also expose it via External LoadBalancer like below:

kubectl expose <your-deploy> --port 80 --target-port 4123 --type LoadBalancer

Note: You need to have cloud provider in order to use type: LoadBalancer. For more info check Cloud providers in the Kubernetes documentation.

See Kubernetes documentation for more details:

Forward a local port to a port on the pod

Exposing an External IP Address to Access an Application in a Cluster

-- mebius99
Source: StackOverflow

6/13/2019

You can choose which port to use locally, so you can just choose that the local port 8888 will be forwarded to the port 4123 in your container

kubectl port-forward your-pod 8888:4123

You can use 8888 or any other free port on your computer.

-- Jose Armesto
Source: StackOverflow