正如《How kubectl port-forward works? https://stackoverflow.com/a/51469150/6309"
kubectl port-forward
将本地端口的连接转发到 Pod 上的端口。
相比kubectl proxy
, kubectl port-forward
更通用,因为它可以转发 TCP 流量kubectl proxy
只能转发 HTTP 流量。
作为示例,请参阅“Kubernetes 端口转发变得前所未有的简单 https://medium.com/pixelpoint/kubernetes-port-forwarding-simple-like-never-before-20a8ab16370f" from 亚历克斯·巴拉什科夫 https://twitter.com/alex_barashkov:
端口转发主要用于访问内部集群资源和调试.
它是如何工作的?
一般来说,使用端口转发,您可以在“本地主机”上获取集群中启动的任何服务。
例如,如果您在 6379 上的集群中安装了 Redis,则使用如下命令:
kubectl port-forward redis-master-765d459796-258hz 7000:6379
您可以将 Redis 从集群转发到 localhost:7000,在本地访问它并执行您想做的任何操作。
对于有限的 HTTP 访问,请参阅 kubectl proxy,并且作为示例,“关于保护 Kubernetes 仪表板的安全 https://blog.heptio.com/on-securing-the-kubernetes-dashboard-16b09b1b7aca" from Joe Beda https://twitter.com/jbeda:
访问集群最简单、最常见的方式是通过 kubectl 代理。这将创建一个本地 Web 服务器,通过 Kubernetes API 服务器将数据安全地代理到仪表板。
如图所示“安装和使用 Kubernetes 仪表板的分步指南 https://medium.com/edureka/kubernetes-dashboard-d909b8b6579c" from Awanish https://twitter.com/edurekaIN:
kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/recommended/kubernetes-dashboard.yaml
使用 kubectl 访问仪表板
kubectl proxy
它将在您的机器和 Kubernetes API 服务器之间代理服务器。
现在,要在浏览器中查看仪表板,请在主虚拟机的浏览器中导航到以下地址:
http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/