Prevent IP address exhaustion for serverless Spark workloads

This document describes strategies to prevent IP address exhaustion when you run Managed Service for Apache Spark serverless batch workloads and interactive sessions. It includes a decision table of IP allocation options, a two-command walkthrough for adding a dedicated Spark subnet to your existing Virtual Private Cloud (VPC) network, and architecture recipes for isolating workloads in a separate VPC network.

How serverless Spark allocates IP addresses

Managed Service for Apache Spark runs serverless workloads on VMs that it provisions in your project: one VM for the Spark driver and one VM for each Spark executor. Each VM consumes an internal IP address allocated from the configured workload subnet.

Managed Service for Apache Spark manages IP addresses dynamically:

  • Initial allocation: A serverless workload requires a minimum of one driver and two executors, initially consuming three subnet IP addresses.
  • Autoscaling: Serverless autoscaling dynamically adds executor VMs as demand grows. Each added executor consumes an additional subnet IP address.
  • IP release: Subnet IP addresses are released when the workload finishes. Serverless runtime versions 3.0 and later release VPC IP addresses quickly after workload completion.

The challenge: In shared or enterprise networks with small subnets (such as a /24 subnet, which provides 252 usable addresses after Google Cloud reserves four addresses), concurrent serverless Spark workloads can rapidly exhaust available IP addresses. This causes provisioning failures for Spark jobs and other resources in the subnet.

Options to prevent IP address exhaustion

Before you design complex network topologies, consider the full spectrum of approaches to prevent IP address exhaustion, from resource tuning to network isolation:

Approach When to use IP space and scalability Networking cost
Reduce VM and IP count per workload Workloads run with default resource settings (4 cores per executor). No network changes required. Increase spark.executor.cores to 8 or 16 (cutting executor VM and IP counts by 2x to 4x for the same total vCPUs) and bound peak bursts with spark.dynamicAllocation.maxExecutors. No additional cost.
Google APIs only (Private Google Access) Workloads access only Google APIs and managed services, such as Cloud Storage, BigQuery, and BigLake. Use a standalone Spark VPC network and subnet with Private Google Access enabled. No connection to your primary network is required. No additional cost.
Expand existing subnet in place Contiguous, unallocated IP address space is available in the primary network. Run gcloud compute networks subnets expand-ip-range to expand the existing subnet CIDR block with zero downtime. No additional cost.
Dedicated Spark subnet in existing VPC (recommended) Primary RFC 1918 space is exhausted, but workloads must reach other VMs or services in the existing primary or Shared VPC network. Add a dedicated subnet in the existing VPC using unused RFC 1918 space, shared address space (RFC 6598, such as 100.64.0.0/16), Class E (240.0.0.0/4), RFC 2544 (198.18.0.0/15), or Privately Used Public IPv4 (PUPI). A /16 provides more than 65,000 IP addresses. No additional spoke, gateway, or NAT data-processing fees (standard inter-zone egress applies).
Dedicated Spark VPC with Private Service Connect (PSC) Spark workloads in an isolated network need access to managed databases (such as Cloud SQL, AlloyDB for PostgreSQL, or Dataproc Metastore), self-managed databases, or internal services hosted on VMs, Google Kubernetes Engine (GKE), or on-premises behind an internal load balancer. Independent Spark VPC network. Supports 100% overlapping CIDR blocks on both sides; exports zero routes to the primary network. Standard PSC endpoint hourly and data-processing charges.
Dedicated Spark VPC with VPC Network Peering VPC isolation is required, and the chosen non-RFC 1918 range (such as 100.64.0.0/16) is unused across peered networks. Independent Spark VPC network peered directly with the primary network by using VPC Network Peering. No spoke or NAT fees; standard inter-zone or inter-region egress applies.
Dedicated Spark VPC with Network Connectivity Center (NCC) and Private NAT (last resort) VPC isolation is required, workloads must reach arbitrary host IPs across multiple spokes, and Spark IPs must be hidden behind a small NAT pool. Independent Spark VPC network attached to an NCC hub, translating Spark traffic through Private NAT to a small /28 NAT pool. Continuous hourly per-spoke and Private NAT gateway charges plus per-GiB data-processing fees on Spark-to-primary traffic.

Walkthrough: Add a dedicated Spark subnet in an existing VPC

The simplest and most cost-effective solution for most organizations is to add a dedicated subnet for Spark workloads in your existing VPC network. Managed Service for Apache Spark supports non-RFC 1918 IPv4 subnets, letting you allocate large CIDR blocks without consuming scarce RFC 1918 space.

Before you begin

  1. Confirm that your account has the required Identity and Access Management (IAM) roles:
    • To create subnets, you need the Compute Network Admin role (roles/compute.networkAdmin) or equivalent permissions in the project.
    • To submit workloads with Runtime 4.0, Managed Service for Apache Spark uses end-user credentials by default. For details on batch submission roles, see Serverless deployment roles.
  2. Enable the Dataproc API and the Dataproc Resource Manager API in your project:

    gcloud services enable dataproc.googleapis.com dataprocrm.googleapis.com
    

Create a dedicated Spark subnet

To create a dedicated subnet using RFC 6598 shared address space (100.64.0.0/16, providing 65,532 usable addresses) with Private Google Access enabled, run the gcloud compute networks subnets create command:

gcloud compute networks subnets create spark-subnet \
    --network=PRIMARY_VPC_NAME \
    --region=us-central1 \
    --range=100.64.0.0/16 \
    --enable-private-ip-google-access

Replace PRIMARY_VPC_NAME with the name of your existing VPC network.

Submit a test batch workload

To verify your configuration, submit a test workload using GA Spark runtime version 4.0 and the built-in SparkPi example JAR. This command requires no external Python scripts or Cloud Storage staging buckets:

gcloud dataproc batches submit spark \
    --version=4.0 \
    --region=us-central1 \
    --subnet=spark-subnet \
    --jars=file:///usr/lib/spark/examples/jars/spark-examples.jar \
    --class=org.apache.spark.examples.SparkPi \
    -- 100

Runtime 4.0 automatically creates and manages regional system firewall policies on the subnet, executes workloads with your end-user credentials, and writes driver output to Cloud Logging.

Architecture recipes for isolated VPC networks

If organizational policies require strict network isolation between Spark workloads and primary resources, deploy Spark in a dedicated VPC network by using one of the following architectural patterns.

Dedicated Spark VPC with Private Service Connect

Use Private Service Connect (PSC) when Spark workloads in a dedicated VPC network need access to managed databases (such as Cloud SQL, AlloyDB for PostgreSQL, Dataproc Metastore, or Memorystore), self-managed databases, or internal services hosted on VMs, GKE, or on-premises behind an internal load balancer:

  • How it works: Workloads send requests to PSC endpoints (forwarding rules) provisioned in the Spark subnet. The endpoint routes traffic across Google's internal network to the target service attachment or producer service.
  • Key advantages: Supports 100% overlapping CIDR blocks between the Spark VPC and consumer or producer networks. It exports zero routes into the primary network, avoids spoke and NAT gateway fees, and connects to managed databases that Private Services Access (PSA) cannot reach transitively.
  • Implementation: Create the Spark subnet and configure PSC endpoints by following Configure Private Service Connect for services, Publish services using Private Service Connect, and Managed Service for Apache Spark network configuration.

Dedicated Spark VPC with Network Connectivity Center and Private NAT

Use NCC and Private NAT as a last resort when workloads in an isolated VPC must reach arbitrary host IP addresses across multiple spokes, and the primary network cannot accommodate large Spark CIDR ranges:

  • Architecture:
    • Attach the primary VPC and the Spark VPC as VPC spokes to an NCC hub located in the primary or central networking project.
    • Configure the Spark spoke with fail-closed export filters (--include-export-ranges=10.99.0.0/28) so that only the small NAT subnet is advertised to the hub, keeping the large Spark workload subnet private.
    • Deploy a Private NAT gateway on a Cloud Router in the Spark network to translate egress traffic bound for the hub into the dedicated 10.99.0.0/28 NAT range.
    • Add an ingress firewall rule in the primary network allowing incoming traffic from 10.99.0.0/28.
  • Networking costs: Unlike same-VPC or peering configurations, this architecture incurs continuous fixed hourly per-spoke charges and Private NAT gateway charges, plus per-GiB data-processing fees on all Spark-to-primary traffic. For current rates, see Network Connectivity Center pricing and Cloud NAT pricing.
  • Key constraints:
    • Both networks must be custom-mode VPC networks (auto-mode is not supported).
    • A VPC network can attach to only one NCC hub.
    • Exported subnets (such as 10.99.0.0/28) must not overlap any subnet in connected spokes, and the Spark subnet must not overlap any subnet exported by the primary VPC network or other hub spokes (unless those spokes also exclude that range from export, though Private NAT cannot reach destinations in overlapping subnets).
    • NCC spokes don't transitively route to VPC-peered PSA ranges, and Private NAT drops traffic to NCC-propagated PSC endpoints.
    • Private NAT supports TCP and UDP only (no ICMP or ping).
    • PRIVATE_NAT subnets cannot be resized after creation. Pre-size the NAT range appropriately and monitor router.googleapis.com/nat/allocated_ports and dropped_sent_packets_count.
    • Configure a Cloud DNS peering zone if workloads resolve primary resources by hostname rather than IP address.
  • Implementation: For generic hub, spoke, Cloud Router, and gateway commands, see Private NAT for NCC and Set up Private NAT. Verify end-to-end path reachability by using Connectivity Tests.

What's next