Kubernetes for Developers
上QQ阅读APP看书,第一时间看更新

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. The Node.js example runs on a different port than the Python example (port 3000 instead of port 5000), so the command needs to be updated accordingly:

kubectl port-forward nodejs-568183341-2bw5v 3000:3000

The output should be something like the following:

Forwarding from 127.0.0.1:3000 -> 3000
Forwarding from [::1]:3000 -> 3000

This is forwarding any and all traffic that gets created on your local machine at TCP port 3000 to TCP port 3000 on the nodejs-568183341-2bw5v Pod.

Just as with the Python example, you don't get a Command Prompt back yet because the command is actively running to keep this particular tunnel alive. As a reminder, you can cancel or quit the kubectl command by pressing Ctrl + C and port forwarding will immediately end.

While the command is still running, open a browser and put in this URL: http://localhost:3000. The response should come back that says, Index Page. When we invoked the kubectl run command, I specifically choose port 3000 to match the default from Express.