port-forward 通過端口轉發映射本地端口到指定的應用端口.
在需要調試部署的pod、svc等資源是否提供正常訪問時使用。
命令格式:
kubectl port-forward <pod_name> <forward_port> --namespace <namespace> --address <IP默認:127.0.0.1>
實例:
1.我在k8s集群中部署了prometheus 的node-exporter服務,用於收集系統的信息.node-exporter的svc采用的是ClusterIP模式,端口不能直接對外訪問,但是我現在想通過nodeip:port的方式在瀏覽器測試node-exporter資源是否正常,用port-forward實現,其實這種方法訪問方式就類似使用NodePort的訪問模式。
[root@localhost ~]# kubectl get svc -n monitoring NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE grafana NodePort 192.168.87.255 <none> 3000:30712/TCP 2d3h kube-state-metrics ClusterIP None <none> 8443/TCP,9443/TCP 2d3h node-exporter ClusterIP None <none> 9100/TCP 2d3h
2、將本地端口9800映射到svc的9100端口,如果不指定address則默認為127.0.0.1的地址.
[root@localhost manifests]# kubectl -n monitoring port-forward svc/node-exporter 9800:9100 --address 10.0.8.101 Forwarding from 10.0.8.101:9800 -> 9100 Handling connection for 9800
3.在瀏覽器輸入nodeip:本地9800端口進行訪問.

4、訪問pod
[root@localhost ]# kubectl get pod NAME READY STATUS RESTARTS AGE temp-chart-6d4cf56db6-rnbwb 1/1 Running 0 29m temp-chart-6d4cf56db6-rsxhr 1/1 Running 0 29m [root@localhost ]# [root@localhost ]# kubectl port-forward temp-chart-6d4cf56db6-rsxhr 8090:80 --address 10.0.8.101 Forwarding from 10.0.8.101:8090 -> 80 Handling connection for 8090
參考文檔:https://www.cnblogs.com/Dev0ps/p/11783128.html
