$ kubectl port-forward -hForward one or more local ports to a pod. This command requires the node to have 'socat' installed.
Use resource type/name such as deployment/mydeployment to select a pod. Resource type defaults to 'pod' if omitted.
If there are multiple pods matching the criteria, a pod will be selected automatically. The forwarding session ends
when the selected pod terminates, and rerun of the command is needed to resume forwarding.
Examples:
# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in the pod
kubectl port-forward pod/mypod 5000 6000
# Listen on ports 5000 and 6000 locally, forwarding data to/from ports 5000 and 6000 in a pod selected by the
deployment
kubectl port-forward deployment/mydeployment 5000 6000
# Listen on port 8443 locally, forwarding to the targetPort of the service's port named "https" in a pod selected by
the service
kubectl port-forward service/myservice 8443:https
# Listen on port 8888 locally, forwarding to 5000 in the pod
kubectl port-forward pod/mypod 8888:5000
# Listen on port 8888 on all addresses, forwarding to 5000 in the pod
kubectl port-forward --address 0.0.0.0 pod/mypod 8888:5000
# Listen on port 8888 on localhost and selected IP, forwarding to 5000 in the pod
kubectl port-forward --address localhost,10.19.21.23 pod/mypod 8888:5000
# Listen on a random port locally, forwarding to 5000 in the pod
kubectl port-forward pod/mypod :5000
NodePort 方式其次较常用的为 NodePort 方式,将 K8s 中 service 的类型修改为 NodePort 方式,会得到一个端口范围在 30000-32767 端口范围内的宿主机端口,同样宿主机具有公网 IP 就可以实现对服务的暴露,但是 NodePort 会占用宿主机端口,一个 Service 对应一个 NodePort,该方式仅为四层,无法实现 SSL 证书的卸载,如果将服务转发到单个 Node 节点的 NodePort 也无法实现高可用,一般需要在 NodePort 前搭配负载均衡来添加多个后端 NodePort 已实现高可用。