The command kubectl port-forward
allow us to expose a port.
When it should be used?
It is the ideal solution for when would like to expose a service remotely and has not there an ingress set up to expose the resource. For example, a backend or database application which should receive requests just from a front-end solution.
Example of Usage
$kubectl port-forward :
NOTE: The container port can be found in the pod or deployment resources. You can use, for example, the command $ kubectl describe
to check it. Also, you can get the spec by using the following command.
kubectl get pods <pod-name> --template='{{(index (index .spec.containers 0).ports 0).containerPort}}{{"\n"}}'
Then, after that, you will be able to have access to the container in the pod by the exposed port. For example, would be possible to ping it ($ ping http://127.0.0.1:
.port-to-exposed)
NOTE: See here a full example to understand how it works better. Also, feel free to check related post by searching for the Kubernetes tag in this website.
Sponsored by: