使用 RFC 1918 私人 IP 位址空間搭配直接 VPC 外連時,如果有大量 Cloud Run 工作負載,可能會導致 IP 用盡。您可以採用下列策略,使用其他 IP 位址範圍來管理 IP 位址用盡的問題。
使用非 RFC 1918 IPv4 位址
除了 RFC 1918 IPv4 位址範圍,Cloud Run 也支援 RFC 6598 和 Class E/RFC 5735 範圍。所有Google Cloud 服務和功能都支援這些非 RFC 1918 範圍,包括虛擬私人雲端網路、Cloud Load Balancing 和 Private Service Connect。
為獲得最佳相容性,建議您從 RFC 6598 (100.64.0.0/10) 範圍開始。如果您已在其他地方使用這個範圍,請考慮使用 Class E/RFC 5735 (240.0.0.0/4)。Class E 是龐大的空間,提供超過 2680 萬個 IP 位址,因此可長期支援您的成長。不過,E 級有一定的限制。舉例來說,Windows 和部分地端硬體不支援這項功能。請參閱這篇文章,瞭解如何利用 Class E IPv4 位址空間,在 GKE 中緩解 IPv4 用盡的問題。
使用 Cloud NAT 或 Private Service Connect
如果使用非 RFC 1918 範圍的 Cloud Run 工作負載需要存取僅接受 RFC 1918 的內部部署目的地,請使用下列任一解決方案:
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[],[],null,["# Best practices for Cloud Run networking\n\nThis page outlines the best practices for configuring networking options for\nCloud Run resources. Before you create your resources, we recommend\nthat you review all the sections on this page to understand the networking\noptions that Cloud Run supports, as well as their implications. \n**Best practices**:\n[Monitor IP address usage](#monitor-ip-usage). \n[Use non-RFC 1918 IP addresses](#non-rfc-1918). \n[Use IPv4 and IPv6 (dual-stack) subnets](#dual-stack-subnets). \n[Use connection pooling and reuse connections](#connection-pooling). \n[Faster external throughput to the internet](#external-throughput). \n[Faster internal throughput to a Google API](#internal-vpc-throughput). \n\nMonitor IP address usage\n------------------------\n\nIf you're using Direct VPC egress, [make sure that you have enough IP addresses\nfor your subnet](/run/docs/configuring/vpc-direct-vpc#before_you_begin). The\nnumber of IP addresses you use depends on the number of instances\nthat your workloads run, so we recommend monitoring your IP address usage. Be\nsure that your IP usage over time stays within the bounds supported by the\nsubnet.\n\nTo estimate your IP address usage:\n\n1. In the Google Cloud console, go to the Cloud Monitoring Metrics explorer page:\n\n [Go to Cloud Monitoring Metrics explorer](https://console.cloud.google.com/monitoring/metrics-explorer)\n2. Look up the number of instances in your project by using\n the metric type [`run.googleapis.com/container/instance_count`](/monitoring/api/metrics_gcp_p_z#gcp-run).\n Cloud Monitoring lets you view this metric's value over time.\n\n3. Multiply the instance count metric's value by 2 to get an estimate of the\n number of IP addresses in use.\n\nIP address exhaustion strategies\n--------------------------------\n\nHaving a large number of Cloud Run workloads can cause IP exhaustion\nchallenges when using the RFC 1918 private IP address space with Direct VPC\negress. The following strategies can help you manage IP address exhaustion by\nusing alternative IP address ranges.\n\n### Use non-RFC 1918 IPv4 addresses\n\nAside from the RFC 1918 IPv4 address ranges, Cloud Run also supports\n[RFC 6598](https://datatracker.ietf.org/doc/html/rfc6598#section-7) and\n[Class E/RFC 5735](https://tools.ietf.org/html/rfc5735) ranges. All\nGoogle Cloud services and features work with these non-RFC 1918 ranges,\nincluding VPC networks, Cloud Load Balancing, and\nPrivate Service Connect.\n\nFor the best compatibility, we recommend starting with the RFC 6598\n(100.64.0.0/10) range. If you're already using this range elsewhere,\nconsider using Class E/RFC 5735 (240.0.0.0/4). Class E is a huge space with over\n268 million IP addresses available, so it'll support your growth for a long\ntime. However, Class E has some limitations. For example, it's not supported on\nWindows and on some on-premises hardware. Read about\n[leveraging Class E IPv4 Address space to mitigate IPv4 exhaustion issues in GKE](https://cloud.google.com/blog/products/containers-kubernetes/how-class-e-addresses-solve-for-ip-address-exhaustion-in-gke).\n\n### Use Cloud NAT or Private Service Connect\n\nIf your Cloud Run workload using a non-RFC 1918 range needs to\nreach an on-premises destination that accepts only RFC 1918, use one of the\nfollowing solutions:\n\n- Use [Hybrid NAT](/nat/docs/private-nat) to perform address translation and egress using a small RFC 1918 range.\n- Expose the on-premises service as a [Private Service Connect](/vpc/docs/configure-private-service-connect-producer) hybrid service.\n\n### Use IPv4 and IPv6 (dual-stack) subnets\n\nAlthough it won't reduce IPv4 exhaustion, moving your apps to [IPv6](/vpc/docs/ipv6-support)\nis a good first step. [Set up dual-stack resources](/run/docs/configuring/vpc-dual-stack-subnet)\nto avoid IPv4 exhaustion problems in the future.\n\nPort exhaustion reduction strategies\n------------------------------------\n\nThe following section describes strategies for reducing port exhaustion with\nCloud Run.\n\n### Use connection pooling and reuse connections\n\nWhen sending a large number of requests to a single destination IP address,\nuse connection pooling to maintain and reuse connections to the destination.\nHigh connection rates to a single IP address can exhaust outbound ports and\ncause connection refused errors.\n\nPerformance and throughput strategies\n-------------------------------------\n\nThis section covers scalable options for improving network performance and\nthroughput towards the internet and Google services.\n\n### Use Direct VPC egress for faster network egress throughput\n\nTo achieve faster throughput across network egress connections, use Direct VPC\negress to route traffic through your VPC network.\n\n#### Example 1: External traffic to the internet\n\nIf you're sending external traffic to the public internet, route all traffic\nthrough the VPC network by setting\n[`--vpc-egress=all-traffic`](/sdk/gcloud/reference/run/deploy#--vpc-egress).\nWith this approach, you must set up Cloud NAT to reach the public internet.\n\nTo enable Cloud Run to use a Cloud NAT gateway for\nPublic NAT or Private NAT, see\n[Cloud NAT Direct VPC egress interactions](/nat/docs/nat-product-interactions#interactions-direct-vpc).\n\n#### Example 2: Internal traffic to a Google API\n\nIf you're using Direct VPC egress to send traffic to a Google API, such as\nCloud Storage, choose one of the following options:\n\n- Specify [`private-ranges-only`](/sdk/gcloud/reference/run/deploy#--vpc-egress) (default) with [Private Google Access](/vpc/docs/configure-private-google-access):\n 1. Set the flag `--vpc-egress=private-ranges-only`.\n 2. Enable Private Google Access.\n 3. [Configure DNS for Private Google Access](/vpc/docs/configure-private-google-access#config-domain). Make sure your target domain (such as `storage.googleapis.com`) maps to one of the following internal IP address ranges:\n - `199.36.153.8/30`\n - `199.36.153.4/30`\n- Specify [`all-traffic`](/sdk/gcloud/reference/run/deploy#--vpc-egress) with [Private Google Access](/vpc/docs/configure-private-google-access):\n 1. Set the flag `--vpc-egress=all-traffic`.\n 2. Enable Private Google Access.\n\nUse the default MTU setting for Cloud Run\n-----------------------------------------\n\nDon't change the [maximum transmission unit (MTU)](/vpc/docs/mtu) setting of a\nVPC network when using it with Cloud Run. Use the\ndefault MTU of 1,460 bytes instead.\n\nWhat's next\n-----------\n\n- [Compare Direct VPC egress and VPC connectors.](/run/docs/configuring/connecting-vpc)\n- [Use tags for testing, traffic migration and rollbacks.](/run/docs/rollouts-rollbacks-traffic-migration#tags)"]]