通过代理ssh到另一个虚拟机时如何访问kubernetes集群中的服务?

How to access to the services in kubernetes cluster when ssh to another VMs via proxy?

假设我们通过网络在裸机服务器中搭建两个虚拟机,一个是master,另一个是worker。我 ssh 给 master 并使用 kubeadm 构建一个集群,其中有三个 pods 和一个服务 type: ClusterIP。因此,当我想要访问集群时,我会在 master 中执行 kubectl proxy。现在我们可以在 ssh 的 VM 中使用 curlwget 探索 API,就像这样:

$ curl http://localhost:8080/api/

到目前为止,一切顺利!但我想通过笔记本电脑访问这些服务?上面的localhost是指裸机服务器!当集群放在另一台机器上时,我的笔记本电脑如何通过代理访问服务?

当我在笔记本电脑上执行 $ curl http://localhost:8080/api/ 时,它说: 127.0.0.1 refused to connect 这是有道理的!但是解决这个问题的方法是什么?

如果你在sshing到master的时候转发了8080端口,你可以在你的笔记本电脑上使用localhost来访问集群上的api。

您可以尝试将 -L 标志添加到您的 ssh 命令中:

$ ssh -L 8080:localhost:8080 your.master.host.com

那么curllocalhost就可以了。

您还可以为 kubectl proxy 命令指定一个额外的参数,让您的反向代理服务器侦听非默认 IP 地址 (127.0.0.1) - 暴露在外部

kubectl proxy --port=8001 --address='<MASTER_IP_ADDRESS>' --accept-hosts="^.*$"

您可以通过发出以下命令来获取主 IP 地址:kubectl cluster-info