
Port forwarding
Now we can use that name to ask kubectl to set up a proxy that will forward all traffic from a local port we specify to a port associated with the Pod we determine. Get the full name of the Pod that was created with your deployment by looking at the Pods using the following command:
kubectl get pods
In my example, the result was flask-1599974757-b68pw, which can then be used with the port-forward command:
kubectl port-forward flask-1599974757-b68pw 5000:5000
The output should be something like the following:
Forwarding from 127.0.0.1:5000 -> 5000
Forwarding from [::1]:5000 -> 5000
This is forwarding any and all traffic that gets created on your local machine at TCP port 5000 to TCP port 5000 on the Pod flask-1599974757-b68pw.
You will note that you don't have a Command Prompt back yet, which is because the command is actively running to keep this particular tunnel we've requested alive. If we cancel or quit the kubectl command, typically by pressing Ctrl + C, then port forwarding will immediately end. kubectl proxy works in the same fashion, so when you use commands such as kubectl port-forward or kubectl proxy, you will probably want to open another Terminal window to run that command in by itself.
While the command is still running, open a browser and put in this URL: http://localhost:5000. The response should come back that says Index Page. When we invoked the kubectl run command, I specifically choose port 5000 to match the default from Flask.