The core split: with or without an external IP
Builds on10. NAT
How a VM reaches the internet depends entirely on whether it has an external IP:
| # | The VM has… | Path out | The world sees |
|---|---|---|---|
| 1 | An external IP | Direct, through 1:1 NAT on its host (chapter 3) | The VM's own external IP |
| 2 | No external IP | None: internal IPs cannot be routed on the internet | Nothing; it cannot get out |
| 3 | No external IP and Cloud NAT | Through Cloud NAT | The NAT IP |
- Your VPC
- Allowed traffic and replies
Cloud NAT does nothing for a VM that has its own external IP. Its traffic still leaves as that IP; the NAT is ignored.
Wide by design - scroll it sideways.
The other ways traffic can leave a VPC, for completeness:
| # | Way out | What it is for | Chapter |
|---|---|---|---|
| 1 | Private Google Access | Google APIs only (Cloud Storage, Secret Manager, and so on), without the internet | 9 |
| 2 | A proxy or NAT VM with a tagged route | Inspecting or filtering traffic yourself. Usually not worth running | 4 |
| 3 | Private NAT | Private-to-private translation between networks whose ranges overlap. Not for the internet | 7 |
| 4 | VPN or Interconnect | Traffic to the office or another network | 15 |
What Cloud NAT is, and what it is not
Cloud NAT is Google's managed way to give private VMs outbound internet access. Each Cloud NAT setup - its subnets, NAT IPs and port settings - is called a Cloud NAT gateway. Kadé will have one, kade-nat. Despite the name, it has nothing to do with the default-internet-gateway route (chapter 4), although it needs that route to work.
The important part is what it is not.
Not a box
| # | AWS NAT Gateway | Cloud NAT |
|---|---|---|
| 1 | A real thing that sits in a subnet | Configuration only |
| 2 | Has a network interface, an IP and a bandwidth limit | No box, no instance, no NAT-device bandwidth limit |
| 3 | Traffic physically flows through it, and it can fail | No single point of failure |
| 4 | Lives in one availability zone: high availability means one per zone | One gateway covers every zone in its region |
- Run by Google
- Your VPC
- Allowed traffic and replies
Translation happens on each VM's host, in the same data plane as routing and the firewall (chapter 1.2). The "gateway" is a set of instructions every host follows, and kade-router is only where those instructions are kept.
Wide by design - scroll it sideways.
Regional, and hosted on a Cloud Router
A Cloud Router is a regional resource in your VPC, and its name is misleading: it never forwards a packet. It belongs to the control plane, and it has two jobs:
- It speaks BGP. Over a VPN or Interconnect, it swaps routes with the router on the other side (chapter 4, chapter 15).
- It holds Cloud NAT settings. A Cloud NAT gateway cannot exist on its own. It is always created on a Cloud Router, which is why the command reads
routers nats create.
Kadé only needs the second job, so kade-router runs no BGP at all. It exists purely as a home for kade-nat, which is very common. The router comes first, then the NAT on it:
gcloud compute routers create kade-router \ --network=kade-vpc --region=asia-southeast1 gcloud compute routers nats create kade-nat \ --router=kade-router --region=asia-southeast1What "regional" means in practice:
- One gateway, every zone. A gateway serves subnets in its own region and covers every zone in it at once. There is nothing to add for high availability: if zone a fails, kade-api-2's host in zone b keeps translating its own traffic.
- A second region needs a second gateway. A Cloud Router belongs to one VPC and one region, so each region that needs internet access gets its own router and its own Cloud NAT. Kadé runs only in asia-southeast1, so one is enough.
- Separate jobs, separate routers. One router could do both jobs, but chapter 15's VPN gets its own,
kade-vpn-router. That way, changing or deleting the VPN's BGP setup can never touch internet access.
Outbound only
A one-way door. Replies get back in because Cloud NAT remembers each connection the VM opened. A connection that starts outside matches nothing and is dropped before any firewall rule is checked.
- Run by Google
- Your VPC
- Allowed traffic and replies
- Firewall and access rules
One-way does not mean locked down: the VM can still connect to any address. To limit where it goes, use egress firewall rules (chapter 5.1).
Wide by design - scroll it sideways.
For traffic coming in you need a load balancer (chapter 11) or an external IP, which are different mechanisms entirely.
How it works with the rest of the VPC
| # | Fact | What it means |
|---|---|---|
| 1 | It needs a route to the default internet gateway | Delete the default route and Cloud NAT stops working (chapter 4 section 08) |
| 2 | Egress firewall rules run first | They are checked against the VM's internal IP before translation. A deny still blocks the traffic |
| 3 | It turns on Private Google Access automatically | For every subnet range it serves, and it cannot be turned off while NAT covers that range. Traffic to Google APIs never goes through NAT (chapter 9) |
What a gateway covers
A Cloud NAT gateway applies to subnet ranges you choose in its region:
| # | Setting | Covers | When |
|---|---|---|---|
| 1 | All subnets, all ranges | Every primary and secondary range in the region, including subnets added later | Simple setups; the default in the console |
| 2 | All subnets, primary ranges only | Every subnet's primary range | Secondary ranges should not get internet access |
| 3 | A list of subnets | Only the subnets (and ranges) you name | You want to decide per subnet. Kadé's choice |
NAT IPs: automatic or manual
| # | Automatic | Manual | |
|---|---|---|---|
| 1 | Who picks the IPs | Google, from its pool | You, from static external IPs you reserved (chapter 3) |
| 2 | Can they change? | yes Google adds and removes IPs as needed | no only you change them |
| 3 | Number of IPs | Grows with demand | Fixed; you must add more if ports run out (section 05) |
| 4 | Good for | Nothing depends on the source IP | An allow-list somewhere depends on it |
PayGate's allow-list means Kadé needs manual. The address must be regional, in the same region as the gateway (chapter 3 section 04). When you later add a second NAT IP, tell partners before adding it, because new connections may leave from either address.
Ports: the limit that actually bites
Builds on31. Ports and the 5-tuple
NAT works by giving each outgoing connection its own source port on the NAT IP, so replies can find their way back. That makes ports the real capacity of a gateway:
| # | Number | Meaning |
|---|---|---|
| 1 | 64,512 ports per NAT IP | 65,536 minus the 1,024 well-known ports |
| 2 | 64 ports per VM by default | Each VM reserves a block of source ports (the "minimum ports per VM" setting) |
| 3 | 64,512 ÷ 64 = 1,008 VMs per NAT IP | How many VMs one IP can serve at the default |
| 4 | 64 connections to one destination | The catch: a port can be reused for different destinations, but one VM can have only as many connections open to the same IP and port as it has reserved ports |
Row 4 is what hurts in practice. If kade-api-1 opens more than 64 connections to api.paygate.lk:443 at the same time, connection 65 is dropped, and the NAT logs show OUT_OF_RESOURCES. The fundamentals course (ch 10) mentioned this error; this is where it comes from. Closed connections also hold their port for a while (TIME_WAIT, section 06), so a burst of short connections can use up ports faster than you expect.
See it: two VMs, two destinations
kade-api-1 opens connections to
kade-api-2 opens connections to
3 connections open on 3 ports. Each connection to the same destination needs its own port.
Drawn with 8 ports per VM so every port fits. The default is 64, and the rule is the same: a row is one destination, a column is one port, and a port can be used once per row.
| # | Fix | Effect |
|---|---|---|
| 1 | Reuse connections in the app (HTTP keep-alive, connection pooling) | Fewer connections, fewer ports. The best fix, and free |
| 2 | Raise the minimum ports per VM | More connections per destination, fewer VMs per IP |
| 3 | Turn on dynamic port allocation | VMs start small and get more ports when busy, up to a maximum you set |
| 4 | Add NAT IPs | More ports in total. Tell allow-list owners first |
Try it: will the ports last?
- Total NAT ports
- 1 × 64,512 = 64,512
- Most VMs at this minimum
- 1,008
- Ports reserved now
- 192
- Max open to one destination, per VM
- 64
- Sustained new connections per second to one destination
- about 0.53, because each closed port waits 120 s (TIME_WAIT) before reuse
OUT_OF_RESOURCES: each VM has 64 ports, so only 64 connections can be open to PayGate at once. Connections 65 to 150 are dropped. Pool connections, raise the minimum, or turn on dynamic port allocation.
Timeouts
Cloud NAT forgets a connection after it has been idle for a while. The defaults:
| # | Timeout | Default | What it means for Kadé |
|---|---|---|---|
| 1 | TCP established, idle | 1,200 s (20 min) | A connection with no traffic for 20 minutes is silently dropped. Long-lived connections need keep-alives shorter than that |
| 2 | TCP transitory (opening or closing) | 30 s | Half-open connections are cleared quickly |
| 3 | TCP TIME_WAIT | 120 s | A closed connection's port stays reserved this long before it can be used for the same destination again |
| 4 | UDP | 30 s | DNS and other short UDP exchanges |
| 5 | ICMP | 30 s | Ping |
Scenarios
Each tab is one trip on the same map of Kadé. The three boxes in the middle are the checks the host makes, in order: route, firewall, then NAT. Each scenario stops at a different one, or turns off before the NAT.
Works: PayGate sees 34.87.200.7, which is on its list
PayGate sees one address, 34.87.200.7, whichever VM made the call. The same holds for kade-api-2, kade-worker and Cloud Run (chapter 10), as long as each is covered by the gateway and has no external IP.
Show as config
PayGate's allow-list: 34.87.200.7/32 kade-nat: manual IP kade-nat-ip (34.87.200.7), subnets sn-app, sn-data To keep external IPs off for good (chapter 17): constraints/compute.vmExternalIpAccess = deny all
Private NAT, briefly
Cloud NAT has a second type. Private NAT translates between two private networks, usually because their ranges overlap and cannot be renumbered, for example two VPCs joined through Network Connectivity Center, or a VPC and an on-premises network over VPN. It takes its addresses from a subnet with purpose PRIVATE_NAT (chapter 2 section 06), and it never gives internet access. Kadé's address plan has no overlaps, so it does not need it.
Cost, logs and monitoring
| # | Where to look | What it shows |
|---|---|---|
| 1 | NAT logging, errors only | Every dropped connection with its reason, such as OUT_OF_RESOURCES. Cheap and worth having on |
| 2 | NAT logging, translations | Every connection that was translated. Useful for audits, but more log volume |
| 3 | Monitoring metrics | dropped_sent_packets_count by reason, and allocated ports per VM |
| 4 | gcloud compute routers get-nat-mapping-info | Which NAT IP and port ranges each VM has right now |
A route, a rule, an address: the full picture
Builds on5. The default gateway10. NAT
With Cloud NAT in place, every piece of the question "will this packet get through?" has been covered. Three separate things decide it, and all three must line up:
- A route says which way. The VPC route table picks the most specific match, and
0.0.0.0/0is used only when nothing more specific matches (chapter 4). - A firewall rule says whether it is allowed. This applies even between two VMs in the same subnet. Outgoing is allowed and incoming is denied, unless a rule says otherwise (chapter 5.1).
- A public address is needed to talk to the internet. An external IP works both ways (chapter 3). Cloud NAT works outwards only.
- Route
- Traffic that gets through
- Firewall rule
Every trip passes the same three checks, in this order. A trip that stays inside the VPC, or inside Google, never needs a public address. Before this chapter, kade-api-1's calls to PayGate had a route and an allowed firewall, and stopped at the third check.
Wide by design - scroll it sideways.
Out of a VM:
| # | From → to | Route that matches | Also needs | Source the receiver sees |
|---|---|---|---|---|
| 1 | VM → VM, same subnetkade-api-1 → kade-api-2 | Subnet route 10.10.1.0/24 | An ingress rule (ch 5.1) | The sender's internal IP, 10.10.1.10 |
| 2 | VM → VM, other subnet, same VPCkade-api-1 → kade-db | Subnet route 10.10.2.0/24 | An ingress rule | The sender's internal IP, 10.10.1.10 |
| 3 | VM → VM in another VPC | None, by default | Peering or a VPN (ch 14, 15) | The sender's internal IP, with peering |
| 4 | VM with an external IP → internet | 0.0.0.0/0 | Nothing more | The VM's external IP |
| 5 | VM without an external IP → internetkade-api-1 → PayGate | 0.0.0.0/0 | Cloud NAT (this chapter) | The NAT IP, 34.87.200.7 |
| 6 | VM without an external IP → Google APIs | 0.0.0.0/0 | Private Google Access on the subnet (ch 9) | It stays inside Google |
| 7 | VM → another VM's external IP | 0.0.0.0/0 | The sender needs an external IP or NAT | The sender's public IP. Avoid this (ch 3) |
Into a VM, from the internet:
| # | Target | Reachable? | Why | Source the VM sees |
|---|---|---|---|---|
| 1 | A VM with an external IP, on port 443 | Only if an ingress rule allows it | Incoming is denied by default | The client's public IP |
| 2 | A VM with no external IP, such as kade-db | Never directly | There is no public address to send to | - |
| 3 | A VM with only Cloud NAT | Never directly | Cloud NAT is outgoing only | - |
To reach a VM with no external IP from outside, put something in front that has one. For websites and APIs that is a load balancer: the public IP lives on the load balancer, which forwards to the VMs on their internal IPs (chapter 11). Behind a proxy load balancer such as Kadé's, the VM sees the load balancer's address and finds the client's in a header (chapter 12.1). For SSH and admin it is IAP TCP forwarding, which needs only a firewall rule for Google's IAP range 35.235.240.0/20 (chapter 6.1).
Back to Kadé
Commands in this chapter
Commands 1 to 4 are read-only. Commands 5 to 7 change the project.
See the outgoing IP from inside a VM
- What it shows: The source address the internet sees for this VM.
- What to look for:
34.87.200.7. A different address means the VM has its own external IP (section 07, tab A). A timeout means no NAT covers its subnet.
curl -s ifconfig.meDescribe the NAT gateway
- What it shows: Which subnets it covers, IP allocation mode, NAT IPs, port settings, timeouts and logging.
- What to look for:
natIpAllocateOption: MANUAL_ONLY, the expected subnets, andminPortsPerVm.
gcloud compute routers nats describe kade-nat \ --router=kade-router --region=asia-southeast1See which ports each VM holds
- What it shows: For every VM using the gateway, its NAT IP and port ranges.
- What to look for: VMs with far more ports than others (busy under dynamic allocation), or VMs missing from the list (not covered).
gcloud compute routers get-nat-mapping-info kade-router --region=asia-southeast1Find NAT drops in the logs
- What it shows: Connections Cloud NAT dropped in the last day, with the reason.
- What to look for:
OUT_OF_RESOURCES(ports ran out, section 05). Needs error logging on.
gcloud logging read \ 'resource.type="nat_gateway" AND jsonPayload.allocation_status="DROPPED"' \ --freshness=1d --limit=20Reserve the NAT IP (changes the project)
- What it shows: Creates the regional static address for the gateway.
- What to look for: The address in
gcloud compute addresses list, statusRESERVEDuntil the gateway uses it. Send this address to PayGate now.
gcloud compute addresses create kade-nat-ip --region=asia-southeast1Create the Cloud Router (changes the project)
- What it shows: A regional Cloud Router that only hosts the NAT settings. No BGP sessions.
- What to look for: It appears in
gcloud compute routers list.
gcloud compute routers create kade-router \ --network=kade-vpc --region=asia-southeast1Create the NAT gateway (changes the project)
- What it shows: Cloud NAT with the fixed IP, for two named subnets, with error logging.
- What to look for: Command 1 on kade-api-1 prints
34.87.200.7within a minute or two.
gcloud compute routers nats create kade-nat \ --router=kade-router --region=asia-southeast1 \ --nat-external-ip-pool=kade-nat-ip \ --nat-custom-subnet-ip-ranges=sn-app,sn-data \ --enable-logging --log-filter=ERRORS_ONLY