Chapters

IP addressing

The fundamentals course covered what an IP address is, private and public ranges, and NAT. This chapter only covers what GCP adds on top: which addresses a VM really has, why its external IP is not on the VM at all, and how to stop an address from changing when you least expect it.

Foundations · chapter 4 of 26 · 13 min read

Every kind of address in a VPC

Builds on2. Private and public addresses

A VM's network card can carry several addresses, and other resources have addresses of their own. The one column to watch is the second: whether the address actually exists inside the VM.

#AddressOn the VM's card?Comes fromScopeKadé's exampleChapter
1Primary internal IPv4yesThe subnet's primary rangeRegional10.10.1.10 on kade-api-13
2Alias IP rangeyesThe subnet's primary or secondary rangeRegionalnone yet (section 08)2, 3
3External IPv4noGoogle's public pool, mapped by 1:1 NATRegionalthe old 34.87.120.153
4IPv6 (internal or external)yesThe subnet's /64; no NATRegionalnone; Kadé is IPv4 only3
5Internal load balancer IP-A subnet's primary range, held by a forwarding ruleRegionalnone yet11
6Global external IP-Google's anycast poolGlobalload balancer 34.120.88.103, 11
7Regional external IP for a service-Google's pool in one regionRegionalCloud NAT 34.87.200.73, 7
8Global internal IP-Any free internal range outside your subnetsGlobala Private Service Connect endpoint for Google APIs9

Only rows 6 and 8 are global, and no address is zonal. Even when a zonal VM holds an address, the address belongs to a region, so it can outlive the VM's zone (section 07).

The external IP is not on the VM

Builds on10. NAT, and 1:1 NAT

The internal IP is real in every sense: it is on the network card, it shows in ip addr, and the app can bind to it. The external IP is only a mapping kept by the data plane on the VM's host (chapter 1.2). It is the 1:1 NAT from the fundamentals course, done beside every VM instead of in a box at the edge.

coming inYour laptop203.0.113.45dst 34.87.120.15data plane on the hostaccess config "external-nat"type ONE_TO_ONE_NAT34.87.120.15 ⇄ 10.10.1.10in: rewrite destinationout: rewrite sourcedst 10.10.1.10kade-api-1 (guest)$ ip addrinet 10.10.1.10/3234.87.120.15 isnowhere in heregoing outPayGatesees 34.87.120.15src 34.87.120.15src 10.10.1.10The VM always sends and receives with 10.10.1.10. The rewrite happens outside it, on the host.
  • Run by Google
  • Your VPC
  • Allowed traffic and replies

The external IP is attached as an "access config" of type ONE_TO_ONE_NAT on the VM's network card. The name says exactly what it is.

Wide by design - scroll it sideways.

Four things follow from this, and each one causes real bugs:

#ConsequenceWhat goes wrongDo this instead
1The VM does not know its external IPAn app that reads its own address to build URLs or callbacks gets 10.10.1.10Ask the metadata server (gcloud check below), or use a DNS name
2The app cannot bind to the external IPlisten 34.87.120.15:8080 fails: that address is not on any interface (Fundamentals ch 31)Bind to 0.0.0.0 or the internal IP
3Outside services see the external IPAn allow-list (PayGate, a partner, Cloud SQL authorized networks) must hold the external IP, not the internal one. The VM never sees that address, so it is easy to get wrongUse a static IP for anything an allow-list depends on (section 03)
4Firewall rules on the receiving side see the sender's external IPIf one of your own VMs calls another through an external IP, the internal allow rule does not match (section 06)Use internal IPs between your own VMs

Ephemeral and static

Every internal and external address is either ephemeral (it belongs to the VM for a while) or static (a named address resource you own until you release it). The two kinds behave very differently for internal and external addresses:

#Ephemeral internalStatic internalEphemeral externalStatic external
1Comes fromNext free address in the subnetAn address you reserve in a subnetGoogle's pool, when the VM startsAn address you reserve
2VM stopped and startedsame addresssame addressnew addresssame address
3VM deletedReleasedKept, ready to reuseReleasedKept, ready to reuse
4Listed by addresses listNoYesNoYes
5CostFreeFreeCharged while the VM runsCharged while in use; charged more while idle

Row 2 is Kadé's outage: an ephemeral external address does not survive a stop. Ephemeral internal addresses do, which is why kade-db has kept 10.10.2.5 so far. It would lose it only if the VM were deleted and recreated.

So the rule is simple: if anything depends on an address, make it static. A DNS record, an allow-list, a partner's config, or an app setting all count.

Try it: what happens to the address?

The address

What happens

Same address. Ephemeral internal addresses stay with the VM across stop and start. This is why kade-db has kept 10.10.2.5 so far.

Regional and global external addresses

A static external address is reserved as either regional or global (chapter 1.1), and one cannot be used where the other is needed:

#ScopeUsed byKadé's exampleTier
1RegionalVMs, Cloud NAT, regional load balancers, VPN gateways, in one regionkade-nat-ip 34.87.200.7 in asia-southeast1 (chapter 7)Premium or Standard
2GlobalGlobal external load balancers onlykade-lb-ip 34.120.88.10Premium only

Network tiers: Premium and Standard

Builds on12. How routing works across the internet

Every external address has a network tier. The tier decides where traffic joins or leaves Google's network, and so how much of the trip happens on the public internet. Premium is the path from chapter 1.1: the shopper enters Google at the nearest edge and rides the backbone to Singapore. Standard skips that edge.

ShopperColomboasia-southeast1Premium tier (default)nearest Google edgeGoogle's own backbone for most of the tripStandard tierpublic internet: other providers' routersGoogle edge
  • Run by Google
  • Your VPC

The difference is who carries the packet. Premium: Google, from near the user. Standard: the public internet, until close to your region.

Wide by design - scroll it sideways.

#PremiumStandard
1Where traffic enters and leaves GoogleThe Google edge nearest the userNear your GCP region
2Rest of the tripGoogle's private backboneThe public internet, through other providers
3Latency and consistencyLower and steadierHigher and more variable
4Outbound data priceHigherLower
5Global load balancers and global IPsSupportedNot supported; regional only
6Good forAnything users wait onBatch jobs, bulk downloads, tools where the path does not matter

The tier is set per address or resource, and a project has a default tier (Premium unless changed). A resource and its address must use the same tier, which is why a global load balancer can only ever have a Premium address. Everything shopper-facing at Kadé is Premium.

Between your own VMs, always use internal addresses

A common mistake is one VM calling another (or itself) through an external IP, often because the external address was copied into a config file. It works, which is why it goes unnoticed. But the packet goes out through 1:1 NAT and comes back in as if it came from the internet:

#kade-api-1 → 10.10.2.5:5432kade-api-1 → kade-db's external IP (if it had one)
1Source kade-db sees10.10.1.10kade-api-1's external IP, or the NAT IP
2Firewall rule needed on kade-dbAn internal rule for 10.10.0.0/16A rule that allows an external address, and so is open wider than it should be
3CostInternal traffic pricingCounted as traffic leaving the zone, even inside one zone
4NeedsNothing extraBoth VMs to have external IPs, or NAT

The same applies to a VM calling its own external IP, which usually means a setting that should say localhost or an internal name. The fix is always the same: internal IPs or internal DNS names (chapter 8) between your own machines.

Choosing and keeping internal addresses

By default a VM gets the next free address in its subnet. You can choose one when you create the VM, or reserve one ahead of time so nothing else takes it:

#GoalHow
1Pick an address when creating a VM--private-network-ip=10.10.1.20, or pass the name of a reserved address
2Keep an address that is already in usePromote it: reserve the same address in the same subnet (section 03)
3Hold an address for something that does not exist yetReserve it now; it stays yours until you use or release it

For Kadé, that means promoting kade-db's 10.10.2.5 now, so the hardcoded address is safe until chapter 8 gives it a name, and reserving 10.10.1.20 for kade-worker, which arrives in chapter 10.

A reserved internal address belongs to its subnet, so to the region, not to the VM's zone (section 01). That matters for a zonal VM like kade-db. If it ever has to be rebuilt in another zone, after a zone failure or simply a move, it can come back with the same address:

region asia-southeast1kade-db-ip · 10.10.2.5reserved in sn-data · regionalzone azone bzone ckade-db10.10.2.5 · lost with zone b✕zone b is downkade-db (new)10.10.2.5 · same addressthe app's hardcoded IP still works
  • Google's: the region and its zones
  • The reserved address
  • kade-db, old and new

The address outlives the zone. Once the old VM is deleted, a new one in any zone of the region can take it.

Wide by design - scroll it sideways.

The new VM asks for it by name: --private-network-ip=kade-db-ip. This keeps the address, not the data. kade-db's disk is zonal and stays in zone b, so the new VM needs a snapshot (global) or a regional disk. Cloud SQL with high availability (chapter 9) handles both for you.

Alias IPs in practice

Chapter 2 introduced alias IP ranges. Adding one, or moving it from one VM to another, is a change to the VM's network card through GCP, not inside the OS:

#StepCommand (short form)
1Give kade-api-1 the extra address 10.10.1.100network-interfaces update kade-api-1 --aliases="10.10.1.100/32"
2Take it away from kade-api-1network-interfaces update kade-api-1 --aliases=""
3Give it to kade-api-2network-interfaces update kade-api-2 --aliases="10.10.1.100/32"
An address can only be on one network card at a time, so a move is always "remove, then add". The full commands are at the end of the chapter.

On Google's standard images, the guest agent adds the alias to the OS's local routing table, so the VM accepts traffic for it without any manual setup. This remove-then-add move is the GCP way to do what gratuitous ARP did on a real LAN (chapter 1.2 section 10). For automatic failover, an internal load balancer with health checks is still the better tool (chapter 11).

IP forwarding and the anti-spoofing check

By default, the data plane checks every packet a VM sends and receives:

  • A VM may only send packets whose source is one of its own addresses.
  • A VM may only receive packets whose destination is one of its own addresses.

Anything else is dropped on the host. This is what stops one VM from pretending to be another. The canIpForward setting (--can-ip-forward when creating a VM) turns the check off, so the VM can pass traffic on for other machines. Only something built to forward traffic needs it: a router VM, a VPN appliance, or a self-built NAT VM.

#QuestionAnswer
1Does Kadé need it?No. Cloud NAT and Cloud VPN are managed services, so Kadé never runs its own forwarding VM
2Why check for it?A VM with forwarding on can relay traffic it should not. Security benchmarks (CIS 4.6) flag it
3Can it be changed later?It used to be creation-only. Today you can change it by exporting the VM's config, editing canIpForward, and applying it with update-from-file, which restarts the VM

IPv6 in GCP, briefly

Builds on30. IPv6

Kadé stays on IPv4, so this is only what is different in GCP:

#PointDetail
1Where it livesA dual-stack subnet gets a /64; each VM network card gets a /96 from it, handed out by DHCPv6
2Internal or externalChosen per subnet. Internal addresses come from the VPC's /48 in fd20::/20; external ones are real internet addresses
3No NATAn external IPv6 address is really on the VM and shows in ip addr, unlike external IPv4 (section 02)
4LimitsCustom mode networks only; external IPv6 needs Premium tier
5FirewallIPv6 needs its own rules. An IPv4 rule never matches IPv6 traffic

Back to Kadé

Commands in this chapter

Commands 1 to 5 are read-only. Commands 6 to 10 change the project.

List reserved addresses

  • What it shows: Every static address in the project, internal and external, with its status.
  • What to look for: RESERVED external addresses nobody uses (billed while idle), and important addresses missing from the list, which means they are ephemeral.
gcloud compute addresses list

See a VM's internal and external addresses

  • What it shows: networkIP (on the card) and any accessConfigs with natIP (the 1:1 NAT mapping).
  • What to look for: An accessConfigs block on a VM that should not have an external IP.
gcloud compute instances describe kade-api-1 --zone=asia-southeast1-a \  --format="yaml(networkInterfaces)"

Ask a VM for its own external IP

  • What it shows: The external IP, as the metadata server knows it. Run inside the VM.
  • What to look for: Empty output means no external IP.
curl -s -H "Metadata-Flavor: Google" \  http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip

Find VMs with IP forwarding on

  • What it shows: VMs where the anti-spoofing check is off.
  • What to look for: An empty list, unless you run a router or appliance on purpose.
gcloud compute instances list --filter="canIpForward=true"

See the project's default network tier

  • What it shows: The tier used when a resource does not say.
  • What to look for: PREMIUM, or empty, which also means Premium.
gcloud compute project-info describe --format="value(defaultNetworkTier)"

Promote kade-db's address to static (changes the project)

  • What it shows: Reserves the address kade-db already uses, so it survives the VM being rebuilt.
  • What to look for: kade-db-ip appears in command 1 with status IN_USE. kade-db keeps running throughout.
gcloud compute addresses create kade-db-ip \  --region=asia-southeast1 --subnet=sn-data --addresses=10.10.2.5

Reserve an internal address for later (changes the project)

  • What it shows: Holds 10.10.1.20 for kade-worker.
  • What to look for: Status RESERVED until a VM uses it. Internal reservations are free.
gcloud compute addresses create kade-worker-ip \  --region=asia-southeast1 --subnet=sn-app --addresses=10.10.1.20

Promote an ephemeral external IP (changes the project)

  • What it shows: The fix for this chapter's outage: keep the external IP a VM already has, so a restart does not change it.
  • What to look for: The same address, now listed as static. Shown for a VM that still has an external IP; replace the address and region with your own.
gcloud compute addresses create kade-api-ip \  --addresses=34.87.121.204 --region=asia-southeast1

Remove an external IP from a VM (changes the project)

  • What it shows: Deletes the access config, so the VM has no external IP.
  • What to look for: No accessConfigs in command 2. An ephemeral address is gone for good; a static one returns to RESERVED and starts costing the idle rate.
gcloud compute instances delete-access-config kade-api-1 \  --zone=asia-southeast1-a --access-config-name="external-nat"

Move an alias IP between VMs (changes the project)

  • What it shows: Removes the alias from kade-api-1, then gives it to kade-api-2.
  • What to look for: The alias appears under aliasIpRanges on kade-api-2 in command 2. Traffic to it stops briefly between the two steps.
gcloud compute instances network-interfaces update kade-api-1 \  --zone=asia-southeast1-a --aliases=""gcloud compute instances network-interfaces update kade-api-2 \  --zone=asia-southeast1-b --aliases="10.10.1.100/32"

Spotted a mistake, or something unclear? Suggest an edit on GitHub