更新:我想这不适用于生产强度,原因与上述@dinvlad 的更新相同,即 IAP 中的速率限制。我将把我原来的帖子留在这里,因为它确实解决了网络连接问题,并说明了底层网络机制。
此外,即使我们不将它用于 Cloud Build,我的方法也提供了一种从我的笔记本电脑隧道到 K8s 私有主节点的方法。因此,我可以在我的笔记本电脑上编辑 K8s yaml 文件(例如,使用 VS Code),并立即kubectl
从我的笔记本电脑上执行,而不必将代码发送到堡垒主机并kubectl
在堡垒主机内执行。我发现这大大提高了开发时间的生产力。
原始答案
=================
我想我可能会对上面@dinvlad 提供的出色解决方案有所改进。
我认为无需安装 HTTP 代理服务器即可简化解决方案。仍然需要堡垒主机。
我提供以下概念证明(没有 HTTP 代理服务器)。这个 PoC 说明了底层网络机制,而不涉及Google Cloud Build (GCB) 的干扰。(当我将来有时间时,我会在 Google Cloud Build 上测试完整的实现。)
认为:
- 我有一个 GKE 集群,其主节点是私有的,例如,IP 地址为 10.xxx
- 我有一个名为
my-bastion
. 它只有私有IP,没有外部IP。私有 IP 在master authorized networks
GKE 集群的 CIDR 内。因此,从内部my-bastion
,kubectl
对私有 GKE 主节点起作用。因为my-bastion
没有外网IP,所以我家的笔记本是通过IAP连接的。
- 我家中的笔记本电脑使用我的家庭互联网公共 IP 地址,无法轻松连接到上面的私有 GKE 主节点。
我的目标是kubectl
在我的笔记本电脑上针对该私有 GKE 集群执行。从网络架构的角度来看,我家用笔记本电脑的位置就像Google Cloud Build服务器。
理论:知道gcloud compute ssh
(以及相关的 IAP)是 SSH 的包装器,SSH动态端口转发应该为我们实现该目标。
实践:
## On laptop:
LAPTOP~$ kubectl get ns
^C <<<=== Without setting anything up, this hangs (no connectivity to GKE).
## Set up SSH Dynamic Port Forwarding (SOCKS proxy) from laptop's port 8443 to my-bastion.
LAPTOP~$ gcloud compute ssh my-bastion --ssh-flag="-ND 8443" --tunnel-through-iap
在我笔记本电脑的另一个终端:
## Without using the SOCKS proxy, this returns my laptop's home public IP:
LAPTOP~$ curl https://checkip.amazonaws.com
199.xxx.xxx.xxx
## Using the proxy, the same curl command above now returns a different IP address,
## i.e., the IP of my-bastion.
## Note: Although my-bastion doesn't have an external IP, I have a GCP Cloud NAT
## for its subnet (for purpose unrelated to GKE or tunneling).
## Anyway, this NAT is handy as a demonstration for our curl command here.
LAPTOP~$ HTTPS_PROXY=socks5://127.0.0.1:8443 curl -v --insecure https://checkip.amazonaws.com
* Uses proxy env variable HTTPS_PROXY == 'socks5://127.0.0.1:8443' <<<=== Confirming it's using the proxy
...
* SOCKS5 communication to checkip.amazonaws.com:443
...
* TLSv1.2 (IN), TLS handshake, Finished (20): <<<==== successful SSL handshake
...
> GET / HTTP/1.1
> Host: checkip.amazonaws.com
> User-Agent: curl/7.68.0
> Accept: */*
...
< Connection: keep-alive
<
34.xxx.xxx.xxx <<<=== Returns the GCP Cloud NAT'ed IP address for my-bastion
最后,关键时刻kubectl
:
## On laptop:
LAPTOP~$ HTTPS_PROXY=socks5://127.0.0.1:8443 kubectl --insecure-skip-tls-verify=true get ns
NAME STATUS AGE
default Active 3d10h
kube-system Active 3d10h