Optimizing port allocation and advanced NAT rule-based IP mapping involves configuring Cloud NAT to efficiently translate internal IP addresses to external IP addresses for outbound internet traffic. This process requires calculating the number of static IP addresses needed based on ports per VM to prevent port exhaustion, and using advanced NAT rules to route specific egress traffic through designated IPs.
Cloud NAT must have enough external IP addresses to provide sufficient NAT source IP address and source port tuples for all the VMs it serves. Each external IP address provides 64,512 available ports. You calculate the number of supported VMs by dividing the total available ports by the configured minimum ports per VM. For example, a single manual NAT IP with a minimum of 64 ports per VM supports up to 1,008 VMs. If you need to support more VMs or allocate more ports per VM (for example, to prevent port exhaustion or accommodate rapid TCP connection cycles), you must add more NAT IP addresses to the gateway. Increasing the minimum ports per VM reduces the number of VMs a single IP can support; setting it to 4,096 ports per VM means one IP supports only 15 VMs.
Advanced NAT rules allow you to control which external IP address is used for outbound traffic based on the packet's source. These rules use Common Expression Language (CEL) to define match conditions, such as a specific source IP range. You create these rules within a Cloud NAT gateway configuration, assigning each rule a priority from 0 (highest) to 65,000 (lowest). For instance, you can create a rule with priority 100 to match traffic from source range 10.10.1.0/24 and translate it to use a specific manual external IP address (IP_ADDRESS_1). A second rule with priority 200 can match traffic from 10.10.2.0/24 and use IP_ADDRESS_2. Traffic not matching any rule uses the default NAT IP addresses. To use these rules, Endpoint-Independent Mapping must be disabled on the NAT gateway.
Cloud NAT offers two methods for allocating ports to VMs: static and dynamic allocation. Static port allocation assigns a fixed number of ports to each VM, which is suitable when you need predictable capacity or are using Endpoint-Independent Mapping. Dynamic port allocation allows the gateway to monitor a VM's port usage and automatically allocate additional ports up to a configured maximum, which helps utilize IP addresses more efficiently but cannot be used with Endpoint-Independent Mapping. Changing the allocation method can be disruptive: switching from static to dynamic only breaks connections if the new maximum ports per VM is set below the old minimum or below 1024. Switching from dynamic back to static always breaks all active NAT connections.
When you increase the minimum ports per VM with static port allocation, there is no traffic interruption, but you must first ensure the NAT gateway has enough assigned IP addresses. With dynamic port allocation, increasing the minimum or maximum ports per VM does not break existing connections. However, reducing the maximum ports per VM in a dynamic allocation setup immediately breaks all existing NAT connections. The Cloud NAT gateway follows a specific port reservation procedure for each VM, which considers the VM's internal IP addresses and any alias IP ranges, and adjusts the minimum ports per VM to at least 1,024 if static allocation is used for subnets with a netmask smaller than /32.
Manual IP allocation for external whitelisting and capacity management involves reserving and assigning static external IPv4 addresses to Cloud NAT gateways, enabling external systems to whitelist specific IP addresses for traffic originating from cloud workloads. Unlike automatic IP allocation, manual allocation provides predictable, consistent egress IP addresses that remain stable even as infrastructure scales, which is essential for organizations that require strict control over which IP addresses can access their services. This approach requires careful planning of IP address ranges and coordination with external systems that enforce whitelist-based access controls.
Cloud NAT allocates IP addresses from a predefined NAT IP address range, and administrators can configure this range to use specific static IP addresses rather than allowing automatic assignment. When you reserve static external IP addresses and assign them to a NAT gateway, the gateway uses only those addresses for outbound traffic, ensuring that external systems always see traffic originating from known, predictable IP addresses. This is particularly important for regulatory compliance, security auditing, and integration with third-party services that restrict access to approved IP ranges. The static IP addresses remain associated with the NAT gateway until explicitly removed, providing continuity for long-lived connections and established trust relationships with external systems.
Manual IP allocation directly impacts capacity management because each static IP address has limits on the number of concurrent connections it can support. When you allocate IP addresses manually, you must calculate the expected traffic volume and ensure sufficient IP addresses are available to handle peak loads without exhausting connection limits. If you need to scale capacity, you can add additional static IP addresses to the NAT configuration, but this requires careful coordination to avoid disrupting existing connections. The trade-off involves balancing the predictability of manual allocation against the flexibility of automatic allocation, which can dynamically adjust to traffic patterns but provides less certainty for external whitelisting purposes.
External systems that enforce IP-based whitelisting require advance notice of IP address changes when capacity modifications occur. When scaling capacity by adding new static IPs, you must update the whitelist entries in external systems before the new addresses carry traffic, otherwise those connections may be blocked. Similarly, when removing IP addresses, you should ensure all active connections have drained before revoking the address to prevent dropped sessions. This operational requirement makes manual IP allocation more complex than automatic allocation but provides the control necessary for strict security policies.
Cloud NAT (network address translation) is Google Cloud's managed service that provides outbound internet connectivity to internal virtual machine (VM) instances without external IP addresses. When configured with automatic IP address allocation, the control plane dynamically provisions and scales public IP addresses to match the egress connection demands of running workloads. This approach prevents backend workloads from being directly reachable from the public internet while ensuring outbound connections scale automatically during traffic surges.
Cloud NAT enables private resources inside a Virtual Private Cloud (VPC) network or Shared VPC host project to initiate outbound connections to external internet destinations. Outbound traffic flows from the workload through the VPC network and relies on a route pointing to the VPC's default internet gateway. Cloud NAT intercepts this egress traffic, translates the internal private IP address to a provisioned external IP address, and statefully routes incoming reply packets back to the originating internal resource. Because Cloud NAT is configured per subnet, administrators can deploy it centrally within a Shared VPC host project to provide secure egress across multiple attached service projects.
Automatic IP allocation allows Google Cloud to manage the lifecycle, provisioning, and scaling of external IP addresses assigned to the NAT gateway without manual intervention. As the number of active workloads or concurrent egress connections increases, the underlying platform automatically allocates additional IP address resources to meet the demand. To accommodate sudden traffic spikes and workload scale-up events, cloud allocation systems reserve IP addresses in blocks and retain addresses during scale-down transition periods to avoid connection drops. This automated scaling mechanism removes the operational burden of estimating port capacity and continuously sizing IP pools.
Automatic IP allocation is optimal for dynamic, auto-scaling environments such as development clusters, CI/CD pipelines, and microservices workloads where egress traffic fluctuates unpredictably. Using automatic allocation simplifies network operations because administrators do not need to manually calculate, reserve, or attach static external IP addresses as backend instances scale. Conversely, manual IP allocation is required when external destination systems enforce strict firewall rules that require known, fixed IP addresses to be explicitly allowlisted. Choosing between automatic and manual allocation requires balancing operational simplicity and dynamic elasticity against external security allowlisting requirements.
Dynamic port allocation is a configuration for a Cloud NAT gateway that automatically adjusts the number of source ports assigned to a virtual machine (VM) based on its current traffic needs. You configure this by setting a minimum and maximum number of ports per VM. The gateway then scales the allocated ports for each VM independently, allowing a VM with bursty traffic to use more ports without permanently taking them from less active VMs.
The scaling mechanism works in power-of-two increments. When a VM's port usage approaches its current allocation, the gateway doubles the number of ports assigned to that VM, up to the configured maximum limit. When the VM's traffic subsides and ports are no longer in use, the gateway gradually reduces the allocation back toward the minimum. This dynamic behavior is incompatible with Endpoint-Dependent Mapping (EDM); if you enable EDM on a Public NAT gateway, you must use static port allocation instead.
Port exhaustion occurs when a Cloud NAT gateway or a specific VM runs out of available source IP and port combinations, causing new outbound connections to fail. You diagnose this by monitoring specific Cloud Monitoring metrics. The key metric is router.googleapis.com/nat/dropped_sent_packets_count, which tracks dropped packets and groups them by reason, such as OUT_OF_RESOURCES (when a VM exhausts its ports) or ENDPOINT_INDEPENDENT_CONFLICT (when a port mapping conflict happens).
To optimize the gateway and prevent these drops, you can tune connection timeouts and ensure adequate NAT IP capacity. You can reduce the TCP TIME_WAIT timeout from its default of 120 seconds to as low as 5 seconds, which allows source ports to be reused more quickly. You can also adjust the TCP Transitory and Established Connection Idle Timeouts to better match your application's behavior. To scale capacity, you add more static external IP addresses to the NAT gateway's pool or enable automatic IP allocation, ensuring the total number of available ports meets the demand from all connected VMs.
Static port allocation is a configuration where you set a fixed, minimum number of source ports for each VM behind a Cloud NAT gateway. This method is required if you enable Endpoint-Independent Mapping (EIM) on a Public NAT gateway. With static allocation, each VM gets the same reserved number of ports, which does not change even if some VMs are idle while others are very active.
Planning capacity with static allocation involves calculating the required number of NAT IP addresses. Each NAT IP provides 64,512 usable source ports. You multiply the number of VMs by the ports allocated per VM to get the total ports needed, then divide by 64,512 to find the minimum number of NAT IPs required. For example, 100 VMs each with 1024 ports need about 102,400 total ports, requiring at least two NAT IP addresses (since 2 * 64,512 = 129,024 ports).
A key operational trade-off is the connection limit per destination. The number of ports per VM defines the maximum simultaneous connections that VM can have to a single destination endpoint (a specific IP, port, and protocol). Enabling EIM can introduce mapping conflicts if a single VM tries to use many of its internal source ports to talk to the same destination, potentially reducing this effective limit and causing dropped packets.
Secure Web Proxy architectures in Google Cloud give administrators centralized control over internet egress traffic by inspecting, filtering, and securing outbound connections from cloud workloads. These architectures support explicit proxy configurations, transparent proxying, and integration with Google Cloudâs security services to enforce compliance and protect against threats. The choice of deployment model depends on whether clients must be individually configured or can derive proxy settings automatically.
Explicit proxy configurations require client applications to be directly configured with the proxy serverâs hostname, port, and optional authentication credentials. In Google Cloud, this is done using environment variables such as HTTPS_PROXY, HTTP_PROXY, and NO_PROXY, or through application-level properties like https.proxyHost and https.proxyPort for Java client libraries. When authentication is needed, credentials can be supplied via custom HttpTransportFactory implementations or secure credential providers that avoid storing secrets in logs. The gcloud CLI supports explicit proxy configuration through properties set with gcloud config set proxy/type, proxy/address, and proxy/port, and authentication credentials are optionally set using environment variables CLOUDSDK_PROXY_USERNAME and CLOUDSDK_PROXY_PASSWORD to prevent credential exposure.
Proxy auto-configuration (PAC) files define dynamic proxy routing rules based on URL patterns, allowing centralized control without configuring each client individually. Google Cloud services typically use a JSON-based proxy configuration file that specifies httpProxy, httpsProxy, and noProxy fields. The noProxy list accepts IP addresses, CIDR ranges, domain names, and the asterisk character to bypass the proxy for specific destinations; domain names prefixed by a dot (such as .google.com) tell the proxy that all subdomains should also bypass it. Notably, the httpProxy and httpsProxy fields do not accept https:// prefixesâeven when the proxy server uses HTTPS, the URL must start with http://, because requests to port 443 automatically use HTTPS encryption.
Google Kubernetes Engine (GKE) and related services support proxy configurations at the cluster level to control egress from containerized workloads. For GKE on AWS, the proxy configuration is stored as a JSON string in AWS Secrets Manager and referenced during cluster creation using the --proxy-secret-arn and --proxy-secret-version-id flags, routing control plane and node pool traffic through the proxy. GKE attached clusters use a similar approach where the configuration is stored as a Kubernetes Secret and referenced during cluster registration or update. Google Distributed Cloud (formerly Anthos) supports proxy configuration through its cluster configuration file using the proxy.url and proxy.noProxy fields, and additional configuration is required for GKE Identity Service when using OIDC authentication behind a proxy.
When deploying proxy architectures, VPC endpoints provide private connectivity to AWS services without requiring public internet access, which is critical for GKE on AWS environments that must route traffic through a proxy while maintaining security boundaries. Required VPC endpoints include interface endpoints for services such as Auto Scaling, EC2, EFS, Load Balancing, Key Management Service, Secrets Manager, and Security Token Service, plus a gateway endpoint for S3. Security groups must allow outbound traffic from control plane and node pool security groups to the proxy server address and port. The proxy allowlist must include domains like .gcr.io, container.googleapis.com, gkeconnect.googleapis.com, oauth2.googleapis.com, and regional variants to ensure connectivity to Google Cloud services.
In environments where direct connectivity to Certificate Authority (CA) services is blocked, Cloud Service Mesh can be configured to route CA traffic through an explicit CONNECT-based HTTPS proxy. This is done by deploying an Istio ProxyConfig custom resource that sets the CA_PLUGIN_PROXY_URL environment variable in sidecar proxies. The sidecar establishes a CONNECT handshake with the proxy, which then forwards CA-destined traffic to endpoints like meshca.googleapis.com and privateca.googleapis.com. The ProxyConfig must be applied to the cluster before restarting workloads in service mesh-labeled namespaces, allowing pods to obtain signed workload certificates from the CA.
Proper firewall configuration is essential: rules must allow traffic from cluster nodes to Google Cloud service endpoints through the proxy server. For Google Distributed Cloud, firewall rules must permit outbound traffic from cluster node IP addresses on source ports 1024-65535 to endpoints including cloudresourcemanager.googleapis.com, gkeconnect.googleapis.com, gkehub.googleapis.com, oauth2.googleapis.com, logging.googleapis.com, monitoring.googleapis.com, and storage.googleapis.com on port 443. The proxy server itself must be allowlisted for all required Google Cloud domains. Additionally, the noProxy list should include the VPC IP range and internal service domains to prevent routing loops for internal traffic.
Integrating and managing proxy operations involves configuring proxy services to control, inspect, and secure internet egress traffic from cloud environments. This includes setting up forward proxies for outbound connections, integrating them with other cloud security services, and managing their configuration and lifecycle to enforce security policies and ensure compliance.
Secure Web Proxy is a managed Google Cloud service that acts as a forward proxy to inspect and control egress traffic from your VPC network to the internet. To configure it, you first create a Secure Web Proxy resource that defines the proxy endpoint. You must then create a Target HTTPS Proxy (for HTTPS traffic) or a Target HTTP Proxy (for HTTP traffic) that references a URL map and an SSL certificate. The target proxy terminates the TLS connection from clients for inspection. The traffic flow begins with a client inside your VPC; the request is routed to the Secure Web Proxy based on firewall rules and route configurations. The proxy then forwards traffic to the internet, applying any configured security and access policies. For services like Apigee, proxy configuration involves uploading a proxy bundle (a ZIP file) to the runtime environment using the Apigee API and deploying it to a specific environment; the deployment makes the proxy active and ready to accept traffic.
Proxy configuration is typically defined in a structured file (JSON or YAML) that specifies the httpProxy, httpsProxy, and noProxy settings. Both httpProxy and httpsProxy fields must use the http:// scheme even when connecting to a proxy server over HTTPS; they define the proxy serverâs hostname, port, and optional authentication credentials. The noProxy field is a comma-separated list of destinations (IPs, CIDR ranges, or domains) that should bypass the proxy; it is critical to include internal service domains like kubernetes.default.svc.cluster.local and the VPC IP range to prevent routing loops. This configuration file is stored as a secret in a service like AWS Secrets Manager or as a Kubernetes Secret, and its ARN or name is referenced when creating or updating cloud resources. Integrating a proxy with services like GKE on AWS, GKE attached clusters, or Cloud Service Mesh requires granting the serviceâs IAM roles (such as the control plane and node pool roles) permission to read the proxy secret (e.g., secretsmanager:GetSecretValue). The service then injects this configuration into its components.
When managing proxy operations, you must ensure cloud workloads have network connectivity to the proxy serverâs IP and port. This often requires configuring VPC firewall rules or security groups to allow egress traffic from the workload subnet to the proxy. The proxy server itself must be configured to allow traffic to necessary Google Cloud service domains (e.g., *.googleapis.com, gcr.io). If TLS inspection is enabled on the Secure Web Proxy, additional services like Certificate Authority Service (privateca.googleapis.com) must be included in the service perimeter if using VPC Service Controls, because the proxy needs to generate and sign certificates. Lifecycle management involves updating the secret when proxy details change and then updating the dependent cloud resources. For a GKE on AWS cluster, use the gcloud container aws clusters update command with the new --proxy-secret-arn and --proxy-secret-version-id. The IAM policy for the control plane and node pool roles must be updated to include the new secret ARN before updating the cluster, or the operation fails. Removing proxy configuration involves updating the resource to point to an empty configuration or a secret that bypasses proxying.
Proxy security and access policies control how network traffic exits a cloud environment and enforce boundaries between internal workloads and external services. Administrators configure these policies to inspect encrypted sessions, restrict outbound traffic to approved domains using URL filtering, and enforce identity constraints on incoming connections. Combining transport layer controls, certificate validation mechanisms, and identity-aware proxies creates a layered defense for cloud resources.
Transport Layer Security (TLS) inspection allows a proxy to act as an intercepting intermediary to decrypt, inspect, and re-encrypt outbound traffic. For TLS inspection to work, client workloads must trust the organizationâs private root certificate authority (CA) and must send the Server Name Indication (SNI) extension during the TLS handshake. The proxy does not support Encrypted Client Hello (ECH)âformerly Encrypted SNIâbecause it cannot access the pre-established server keys required to decrypt initial handshake parameters. Servers using private or self-signed certificates cannot be intercepted, and the proxy does not perform certificate revocation list (CRL) checks. Managing certificates requires understanding CA caching: the Network Security Service Agent generates intermediate CAs using a designated private CA pool to issue leaf certificates dynamically. If an administrator removes a CA from the private CA pool, the proxy continues serving cached certificates signed by that CA for up to 28 hours. To immediately stop using cached certificates, administrators must update the regional TLS inspection policy to target a completely new CA pool, forcing the proxy to generate fresh certificates.
URL filtering enables organizations to allow or block egress traffic to specific domains at Layer 7. To apply URL filtering across a VPC network, administrators create security profiles and group them into a security profile group. A regional or global firewall policy then applies this group to outbound traffic by configuring policy rules with the apply_security_profile_group action. The firewall policy must be associated with the same VPC network where the inspected workloads reside, routing traffic through dedicated zonal firewall endpoints that handle Layer 7 processing and optional TLS inspection. The flow is: workload egress traffic enters the VPC network, is matched by the firewall policy rule, and is redirected to a firewall endpoint which performs TLS inspection and URL filtering before forwarding to the destination.
Identity-Aware Proxy (IAP) provides principal-based access control and device validation for web applications without requiring traditional VPN connections. When implementing certificate-based access (CBA), administrators create custom access levels in Access Context Manager to authenticate incoming device certificates. If using Certificate Manager trust configurations, the access level condition validates the certificate path using the expression certIsPkiAttested(origin, ["certificatemanager.googleapis.com/projects/PROJECT_ID/locations/global/trustConfigs/TRUST_CONFIG_NAME"]) == true. For devices that use self-signed certificates managed via Endpoint Verification, the access level uses the expression certificateBindingState(origin, device) == CertificateBindingState.CERT_MATCHES_EXISTING_DEVICE. Once created, the access level is attached directly to the target application resource settings within IAP. Target HTTPS proxies terminate the client mTLS connection using SSL certificates whose lifecycle states must reach ACTIVE before successfully serving requests.
Client workloads route their egress connections through an HTTP forward proxy by defining specific proxy environment variables and configurations. A standard proxy configuration file requires three core parameters: httpProxy, httpsProxy, and noProxy. Both httpProxy and httpsProxy fields require the http:// prefix rather than https://, even when forwarding traffic targeted for secure port 443. The noProxy attribute accepts a comma-separated list of IP addresses, CIDR ranges, and domain names to bypass the proxy, where a leading dot like .google.com mandates subdomain matching, and an asterisk wildcard (*) bypasses proxying for all traffic.
router.googleapis.com/nat/dropped_sent_packets_count, with reasons like OUT_OF_RESOURCES indicating a VM has run out of allocated ports.You calculate the number of supported VMs by dividing the total available ports (64,512 per IP) by the configured minimum ports per VM. For example, with a minimum of 64 ports per VM, one IP supports up to 1,008 VMs.
Static allocation reserves a fixed number of source ports for each VM, which never changes. Dynamic allocation allows the number of ports per VM to automatically scale up and down between a minimum and maximum limit based on the VM's current traffic, improving port utilization for uneven workloads.
You must use static port allocation if you need to enable Endpoint-Independent Mapping (EIM) on a Public Cloud NAT gateway, as dynamic allocation is incompatible with this feature.
httpProxy and httpsProxy fields in proxy configuration use http:// even when the proxy server uses HTTPS?The http:// prefix is required because the fields define the proxy server's address and port; requests sent to port 443 are automatically upgraded to HTTPS by the proxy without needing an https:// prefix in the configuration.
Professional Cloud Network Engineer
Prepare and test your skills
Prepare and test your skills