为 public 个 GKE 集群设置 Cloud NAT

Setup Cloud NAT for public GKE clusters

我想使用 Cloud NAT 设置 NAT 网关,以便 VMs/Pods 在 public GKE 集群中使用静态 IP 地址。

我面临的问题是 NAT 网关似乎只在 VM 没有其他选项时使用,即:

GCP forwards traffic using Cloud NAT only when there are no other matching routes or paths for the traffic.

但在 public GKE 集群的情况下,VM 具有临时外部 IP,因此它们不使用网关。

根据文档:

If you configure an external IP on a VM's interface [...] NAT will not be performed on such packets. However, alias IP ranges assigned to the interface can still use NAT because they cannot use the external IP to reach the Internet.

With this configuration, you can connect directly to a GKE VM via SSH, and yet have the GKE pods/containers use Cloud NAT to reach the Internet.

这就是我想要的,但我看不出要在这里设置什么。

alias IP ranges assigned to the interface can still use NAT 是什么意思,如何设置?

这里的想法是,如果您为集群使用本机 VPC(Ip 别名),your pods will not use SNAT when routing out of the cluster。如果没有 SNAT,pods 将不会使用节点的外部 IP,因此应该使用 Cloud NAT。

不幸的是,目前情况并非如此。虽然 Cloud NAT 仍处于测试阶段,但某些设置尚未完全到位,因此 pods 仍在使用 SNAT,即使使用 IP 别名也是如此。由于节点 IP 的 SNAT,pods 将不会使用 Cloud NAT。

话虽这么说,为什么不使用私有集群呢?它更安全并且可以与 Cloud NAT 配合使用。您不能直接通过 SSH 连接到节点,但是 A) 您可以在您的项目中创建一个可以 SSH using the internal IP flag 的堡垒 VM 实例,并且 B) 在大多数情况下您通常不需要通过 SSH 连接到节点。

"Unfortunately, this is not currently the case. While Cloud NAT is still in Beta, certain settings are not fully in place and thus the pods are still using SNAT even with IP aliasing. Because of the SNAT to the node's IP, the pods will not use Cloud NAT."

确实,正如 Patrick W 上面所说,它目前并没有像记录的那样工作。我也尝试过,并在 Kubernetes Engine 频道中与 GCP Slack 组的人们进行了交谈。他们还在测试中确认它仅适用于 GKE 私有集群。我们还没有开始使用私有集群。我找不到关于这个简单问题的可靠文档:如果我创建一个私有集群,我还能在该集群中拥有 public K8S 服务(又名负载均衡器)吗?所有关于私有 GKE 集群的文档都表明您不希望任何外部流量进入,但我们 运行 在我们的 GKE 集群上提供面向 Internet 的生产服务。

我就 Cloud NAT 问题向 GCP 支持部门提交了工单,他们是这样说的:

"I have been reviewing your configuration and the reason that Cloud NAT is not working is because your cluster is not private. To use Cloud NAT with GKE you have to create a private cluster. In the non-private cluster the public IP addresses of the cluster are used for communication between the master and the nodes. That’s why GKE is not taking into consideration the Cloud NAT configuration you have. Creating a private cluster will allow you to combine Cloud NAT and GKE.

I understand this is not very clear from our documentation and I have reported this to be clarified and explained exactly how it is supposed to work."

我回答说请他们按照记录的方式工作,而不是更改他们的文档。我正在等待他们的更新...

这可能对某人有帮助:

您可以简单地 运行 这个版本为 TF 11 的 terraform 脚本。它将创建 VM 并将其用作 NAT 网关并通过它路由所有 public VM 流量。

https://github.com/GoogleCloudPlatform/terraform-google-nat-gateway/tree/master/examples/gke-nat-gateway

对于生产用例,还有 HA 设置:

https://github.com/GoogleCloudPlatform/terraform-google-nat-gateway/tree/master/examples/ha-nat-gateway

将 google 的 Cloud NAT 与 public GKE 集群一起使用有效!

首先需要使用保留的外部 IP 设置云 NAT 网关和路由器。

完成后,ip-masq-agent 配置需要更改为 而不是 伪装外部 IP 的 pod IP,这些外部 IP 是集群内部请求的目标。更改配置是在 ip-masq-agent 的 ConfigMap 的 nonMasqueradeCidrs 列表中完成的。

其工作方式是,对于 nonMasqueradeCidrs 列表中 IP 的每个传出请求,都不会进行 IP 伪装。所以请求似乎不是来自节点 IP,而是来自 pod IP。然后,此内部 IP 由 Cloud NAT gateway/router 自动进行 NAT。结果是请求似乎来自云NAT路由器的(稳定)IP。

来源: