本文档介绍了两种用于负载均衡的 Dataplane V2 转发模式,以及如何为 Google Distributed Cloud 启用它们。Dataplane V2 支持两种负载均衡转发模式:
来源网络地址转换 (SNAT)
直接服务器返回 (DSR)
您只能在创建集群时配置负载均衡转发模式。
SNAT 负载均衡
来源网络地址转换 (SNAT) 是 Dataplane V2 负载均衡的默认转发模式。但是,在 SNAT 模式下,系统不会为后端 Pod 保留客户端 IP 地址。当来自客户端的数据包到达负载均衡器节点时,它会进行转换并转发到具有后端 Pod 的目标工作器节点。后端 Pod 看到请求来自负载均衡器节点,而不是客户端位置。因此,回复会返回到负载均衡器节点,然后进行反向转换并发送回客户端。
SNAT 的数据包流
使用 Dataplane V2 负载均衡的 SNAT 转发模式时,从外部客户端到后端 Pod 的数据包流如下所示:
系统会将 LoadBalancer 类型的 Service 分配给负载均衡器节点,并且其 IP 地址 172.16.20.16 由该节点上运行的 MetalLB speaker 通告。
Dataplane V2 会将使用 SNAT 的源 IP 地址和端口转换为 <LB_NODE_IP>:52000,并将数据包转发到工作器节点。
响应会发送回负载均衡器节点,目标地址将进行反向转换。
下图显示了 SNAT 模式的数据包流:
DSR 负载均衡
直接服务器返回 (DSR) 可以解决 SNAT 负载均衡问题。在 DSR 模式下,负载均衡器节点使用 IP 选项来保存客户端来源地址。
工作器节点可以恢复原始数据包并将其转发到后端 Pod。后端 Pod 可以查看数据包来自客户端 IP 地址,而不是负载均衡器节点 IP 地址。因此,返回数据包直接返回客户端 IP 地址,而不是返回负载均衡器节点。
此模式不仅可以解决客户端 IP 地址可见性问题,还可以节省负载均衡器节点的带宽。返回流量不必经过负载均衡器节点,负载均衡器节点也不再需要执行连接跟踪。这种方法可以节省内存并释放转发端口。对于下载流量远远高于请求流量的非对称工作负载,DSR 流程会大幅减少带宽。
DSR 的数据包流
使用 Dataplane V2 负载均衡的 DSR 转发模式时,从外部客户端到后端 Pod 的数据包流如下所示:
系统会将 LoadBalancer 类型的 Service 分配给负载均衡器节点,并且其 IP 地址 172.16.20.16 由该节点上运行的 MetalLB speaker 通告。
Dataplane V2 将数据包转发到工作器节点,并使用 IP 选项保存原始客户端来源 IP 地址和端口。
apiVersion:baremetal.cluster.gke.io/v1kind:Clustermetadata:name:lb-modenamespace:cluster-lb-mode...spec:clusterNetwork:forwardMode:dsr# valid options are dsr or snatpods:cidrBlocks:-192.168.0.0/16services:cidrBlocks:-10.96.0.0/20...
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-01。"],[],[],null,["This document describes the two Dataplane V2 forwarding modes for load balancing\nand how to enable them for Google Distributed Cloud. Dataplane V2 supports two\nforwarding modes for load balancing:\n\n- Source network address translation (SNAT)\n- Direct Server Return (DSR)\n\nThe forwarding mode for load balancing can be configured when you create a\ncluster only.\n\nSNAT load balancing\n\nSource network address translation (SNAT) is the default forwarding mode for\nDataplane V2 load balancing. However, in SNAT mode, the client IP address isn't\npreserved for backend Pods. When the packet from the client arrives at the load\nbalancer node, it's translated and forwarded to the destination worker node with\nthe backend Pod. The backend Pod sees that the request is coming from the load\nbalancer node, instead of the client location. As a result, the reply is\nreturned to the load balancer node and reversed translated and sent back to the\nclient.\n\nPacket flow for SNAT\n\nWith SNAT forwarding mode for Dataplane V2 load balancing, here's the packet\nflow from an external client to a backend Pod and back:\n\n- Service of type LoadBalancer is assigned to a load balancer node and its IP\n address `172.16.20.16` is advertised by the MetalLB speaker running on that\n node.\n\n- Dataplane V2 translates the source IP address and port with SNAT to\n `\u003cLB_NODE_IP\u003e:52000` and forwards the packet to the worker node.\n\n- The response is sent back to the load balancer node and the destination\n address is reverse translated.\n\nThe following diagram shows the packet flow for SNAT mode:\n\nDSR load balancing\n\nDirect Server Return (DSR) overcomes issues with SNAT load balancing. In DSR\nmode, the load balancer node uses IP Options to save the client source address.\nThe worker node can recover the original packet and forward it to the backend\nPod. The backend Pod can see that the packet is coming from client IP address\ninstead of the load balancer node IP address. As a result, the return packet\ndirectly returns to the client IP address instead of traveling back to the load\nbalancer node.\n\nThis mode not only solves the client IP address visibility problem, it also\nsaves the bandwidth for the load balancer node. Return traffic doesn't have to\ngo through the load balancer node, and the load balancer node doesn't have to do\nconnection tracking anymore. This approach saves memory and frees the forwarding\nport. For asymmetric workloads, where download traffic is much higher than\nrequest traffic, the DSR flow reduces bandwidth significantly.\n\nPacket flow for DSR\n\nWith DSR forwarding mode for Dataplane V2 load balancing, here's the packet flow\nfrom and external client to a backend Pod and back:\n\n- Service of type LoadBalancer is assigned to the load balancer node and its\n IP address `172.16.20.16` is advertised by the MetalLB speaker running on\n that node.\n\n- Dataplane V2 forwards the packet to the worker node and uses IP Options to\n save the original client source IP address and port.\n\n- Dataplane V2 on the worker node forwards the packet to the backend Pod.\n\n- The backend Pod recovers the source IP address and port and replies to the\n client.\n\nThe following diagram shows the packet flow for DSR mode:\n\nEnable DSR mode\n\nTo enable DSR mode, add the\n[`spec.clusterNetwork.forwardMode`](/kubernetes-engine/distributed-cloud/bare-metal/docs/reference/cluster-config-ref#clusternetwork-forwardmode)\nfield to your cluster configuration file and set it to `dsr`. As noted earlier,\nSNAT mode is enabled by default. You can specify SNAT mode explicitly by setting\n`forwardMode` to `snat`. The forwarding mode for Dataplane V2 load balancing can\nbe configured at cluster creation time only.\n| **Note:** For version 1.16 clusters, the ability to configure Dataplane V2 forwarding mode for load balancing is a ([Preview](/products#product-launch-stages)) capability. While in Preview, this feature uses an annotation `preview.baremetal.cluster.gke.io/dpv2-lbmode-dsr:\nenable` to enable DSR mode. For cluster upgrades from version 1.16 to 1.28.0 and higher, Google Distributed Cloud honors the annotation and sets `forwardMode` in the cluster spec accordingly. \n\n apiVersion: baremetal.cluster.gke.io/v1\n kind: Cluster\n metadata:\n name: lb-mode\n namespace: cluster-lb-mode\n ...\n spec:\n\n clusterNetwork:forwardMode: dsr # valid options are dsr or snat\n pods:\n cidrBlocks:\n - 192.168.0.0/16\n services:\n cidrBlocks:\n - 10.96.0.0/20\n ...\n\nThe `forwardMode` value can't be modified after the cluster is created. Make sure you've\nconfigured the correct option for your needs before you create the cluster."]]