Kubernetes port-forward
Pod通过端口转发映射本地端口到指定的应用端口.在需要调试部署的pod、svc等资源是否提供正常访问时使用。实例:1.我在k8s集群中部署了prometheus的服务,用于收集系统的信息的svc采用的是ClusterIP模式,端口不能直接对外访问,但是我现在想通过的方式在浏览器测试资源是否正常,用实现,其实这种方法访问方式就类似使用NodePort的访问模式。2、将本地端口9800映射到svc的9
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
通过kubectl port-forward
将Pod
端口到本地
K8S的Service
如果是ClusterIP
类型,又临时需要以NodePort
方式访问,可以通过kubectl port-forward
命令将端口映射到本地。
kubectl port-forward svc/kiali 20001:20001 -n istio-system
如上所述,将kiali的20001
端口映射到本地的20001
端口。通过localhost:20001
既可直接访问。
执行后kubectl port-forward
后即可访问
k8s(14) : 临时转发容器端口到外部
kubectl port-forward service/hello 8080:8080 --address 0.0.0.0 -n test
前面的8080
为宿主机端口,后面的8080
为pod
内部端口
更多推荐
所有评论(0)