NetworkingNetworking
Thuộc bộ tài liệu Cloud knowledge base — GCP deep dive. Bổ sung cho DevOps Roadmap.
Tổng quan
Networking là nơi GCP khác AWS rõ rệt nhất, và khác biệt đó thuộc về kiến trúc, không phải bề mặt. Trong GCP, một VPC network là resource global: một VPC trải mọi region, và bạn thêm subnet regional vào nó khi mở rộng. Không có VPC theo từng region và không cần peering chỉ để nối hai region của chính mạng bạn — backbone riêng của Google đã cõng traffic đó.
Trên tấm vải global đó là những mảnh bạn lắp mỗi ngày: subnet (không gian IP),
route (gói đi đâu), firewall rule (được phép gì), Cloud Load Balancing
(từ một global anycast IP duy nhất xuống tới internal L4), Cloud NAT (egress
cho VM private), Cloud DNS và Cloud CDN. Trang này ánh xạ chúng, chỉ ra
nơi GCP hành xử khác AWS, và trình bày ví dụ gcloud cùng Terraform.
Kiến thức nền tảng
VPC network — global, với subnet regional
VPC network là một mạng ảo định nghĩa bằng phần mềm và là global. Subnet
của nó là regional và cắt ra các dải IP (CIDR). Vì VPC là global, một VM ở
us-central1 và một VM ở europe-west1 trên cùng VPC giao tiếp qua backbone của
Google bằng internal IP — không peering, không gateway.
| Khái niệm | AWS | GCP | Ghi chú |
|---|---|---|---|
| Mạng ảo | VPC (regional) | VPC (global) | VPC của GCP không có region |
| Nơi giữ dải IP | Subnet (trong 1 AZ) | Subnet (regional, trải các zone) | Subnet GCP phủ mọi zone của region |
| Quy tắc cấp gói | Security Group + NACL | Firewall rule (cấp VPC) | Xem bên dưới |
| Nối liên region | VPC Peering / TGW | Native (cùng global VPC) | Trong cùng VPC là tự động |
Chế độ subnet:
- Auto mode — Google tự tạo một subnet mỗi region từ dải cố định
10.128.0.0/9. Tiện cho demo, nhưng các dải cố định và có thể đụng nhau khi interconnect. Tránh trong production. - Custom mode — bạn tự định nghĩa mọi subnet và CIDR của nó. Luôn dùng custom mode cho production.
Mỗi subnet còn có secondary IP range dùng cho alias IP — cơ chế mà GKE dùng để cấp cho Pod và Service dải routable riêng (“VPC-native” cluster).
Route
Route cho gói biết đi đâu. Mọi VPC đều có:
- Một default route ra internet (
0.0.0.0/0→ default internet gateway), chỉ có tác dụng với VM có external IP hoặc đi qua Cloud NAT. - Subnet route — tự tạo cho dải của mỗi subnet.
- Custom static route — ví dụ đẩy
10.8.0.0/16sang một VPN tunnel hoặc một NAT appliance instance (next hop = instance, IP, VPN tunnel hoặc ILB). - Dynamic route — học qua Cloud Router bằng BGP (cho Cloud VPN / Interconnect).
Route là resource global của VPC nhưng có thể giới hạn theo network tag của instance.
Firewall rule (và khác AWS thế nào)
Firewall rule của GCP hoạt động ở cấp VPC, không phải per-instance như AWS Security Group. Khác biệt chính:
| Khía cạnh | AWS Security Group | GCP VPC firewall rule |
|---|---|---|
| Gắn vào | Một ENI/instance | VPC, nhắm bằng tag / SA / dải IP |
| Chiều | Stateful, in/out | Stateful, INGRESS hoặc EGRESS mỗi rule |
| Allow/Deny | Chỉ allow | Allow và Deny rule |
| Priority | Không (mọi allow là hợp) | Priority số (0–65535, nhỏ thắng) |
| Mặc định | Deny inbound, allow outbound | Ngầm định deny all ingress, allow all egress |
| Nhắm vào | Thành viên của SG | Tag mạng, service account, hoặc dải IP |
Luôn tồn tại hai rule ngầm ở priority 65535: deny all ingress, allow all egress. Bạn xếp rule priority cao hơn (số nhỏ hơn) lên trên. Vì có cả allow và deny kèm priority, bạn diễn đạt được “cho phép dải này nhưng chặn một IP ồn ào bên trong nó” — điều SG của AWS một mình không làm được.
Nhắm bằng tag hay service account là kiểu GCP đặc trưng: thay vì liệt kê IP,
bạn nói “cho phép tcp:443 ingress tới instance gắn tag https-server”, hoặc
“…tới instance chạy dưới serviceAccount:web@…”. Nhắm theo SA mạnh hơn vì tag ai
sửa được instance cũng đặt được.
Shared VPC vs VPC Peering
Hai cách nối mạng — chọn theo cấu trúc org:
| Shared VPC | VPC Peering | |
|---|---|---|
| Mô hình | Một host project sở hữu VPC; service project gắn vào và deploy vào subnet của nó | Hai VPC độc lập trao đổi route |
| Ranh giới | Cùng organization, quản trị mạng tập trung | Cùng hoặc khác org/project |
| IAM | Team mạng trung tâm kiểm soát subnet/firewall; team app chỉ có compute | Mỗi bên giữ toàn quyền |
| Bắc cầu | Không áp dụng (một mạng) | Không bắc cầu — A↔B và B↔C không cho A↔C |
| Khi nào dùng | Muốn một team sở hữu mạng cho nhiều project app | Cần nối hai VPC do hai bên sở hữu riêng |
Shared VPC là mẫu landing-zone doanh nghiệp chuẩn: một team mạng/bảo mật sở hữu subnet và firewall rule trong host project, còn các team ứng dụng deploy workload vào các subnet đó từ service project của mình mà không đụng tới mạng.
Khái niệm chính
Cloud NAT & Cloud Router
VM không có external IP vẫn thường cần ra internet (cập nhật gói, gọi API ngoài). Cloud NAT cung cấp egress NAT managed, regional mà không phải chạy hay scale NAT-instance nào. Nó là dịch vụ định nghĩa bằng phần mềm (không phải instance nghẽn cổ chai) và tích hợp với Cloud Router, cái quản control plane và cũng chạy BGP cho route động qua Cloud VPN / Interconnect.
# Cloud Router + Cloud NAT regional để VM private ra internet outbound
gcloud compute routers create nat-router \
--network=prod-vpc --region=us-central1
gcloud compute routers nats create prod-nat \
--router=nat-router --region=us-central1 \
--nat-all-subnet-ip-ranges \
--auto-allocate-nat-external-ips
Cloud NAT là egress-only — không cho inbound không mời. Inbound tới VM private đi qua load balancer hoặc IAP.
Cloud Load Balancing
Load balancer của Google là một họ; phân nhánh đầu tiên là global vs regional, thứ hai là external vs internal, thứ ba là L7 (HTTP/S) vs L4 (TCP/UDP).
| Load balancer | Phạm vi | Tầng | Dùng cho |
|---|---|---|---|
| Global external Application LB | Global (một anycast IP) | L7 HTTP(S) | Web app công khai, routing toàn cầu, Cloud CDN, Cloud Armor |
| Regional external Application LB | Regional | L7 HTTP(S) | Web app giới hạn region, yêu cầu về địa phương |
| External passthrough Network LB | Regional | L4 TCP/UDP | Giao thức không phải HTTP, giữ nguyên client IP |
| Internal Application LB | Regional | L7 HTTP(S) | Routing microservice nội bộ |
| Internal passthrough Network LB | Regional | L4 TCP/UDP | L4 nội bộ (database, dịch vụ nội bộ) |
Global external Application Load Balancer là tính năng đặc trưng của GCP: một anycast IP duy nhất quảng bá toàn cầu, đưa mỗi người dùng tới backend khỏe mạnh gần nhất qua backbone của Google, kèm sẵn Cloud CDN và Cloud Armor (WAF/DDoS). Đây là tổ hợp “CloudFront + global accelerator + ALB” của AWS gói trong một sản phẩm. Các khối cấu thành: forwarding rule → target proxy → URL map → backend service → backend (MIG/NEG) → health check.
Cloud DNS
Cloud DNS là DNS authoritative managed của Google với SLA uptime 100%.
- Public zone phục vụ record ra internet.
- Private zone phân giải tên chỉ trong các VPC được chọn (split-horizon).
- DNS peering và forwarding tích hợp với resolver on-prem/VPC khác.
- DNSSEC ký các public zone.
gcloud dns managed-zones create app-zone \
--dns-name="app.example.com." --description="app zone" --visibility=public
gcloud dns record-sets create www.app.example.com. \
--zone=app-zone --type=A --ttl=300 --rrdatas="203.0.113.10"
Cloud CDN
Cloud CDN cache nội dung tại edge POP của Google, đứng trước một backend service trên global external Application Load Balancer. Bật nó là một cờ trên backend service; dùng cache key, TTL và signed URL/cookie cho nội dung riêng tư. Vì đi trên cùng LB, bạn có global anycast + CDN + WAF như một stack.
Kết nối riêng: Private Google Access & Private Service Connect
Tiếp cận Google API (Cloud Storage, BigQuery) và managed service mà không đi qua internet công cộng:
- Private Google Access — cho VM chỉ có internal IP tiếp cận Google API và dịch vụ qua định tuyến nội bộ. Bật theo từng subnet.
- Private Service Connect (PSC) — phơi một dịch vụ (một Google API, một managed service như Cloud SQL, hoặc dịch vụ đối tác/của chính bạn) sau một private IP trong VPC của bạn. Bạn dùng nó như thể nó nằm trong mạng của bạn; traffic không bao giờ chạm internet. Là bản thay thế hiện đại cho nhiều mẫu private-access.
- VPC Service Controls — một control riêng vẽ một service perimeter quanh resource (ví dụ bucket) để chặn exfiltration dữ liệu ngay cả khi credential hợp lệ.
VPC Flow Logs
VPC Flow Logs lấy mẫu các flow mạng (5-tuple, bytes, packets, latency, quyết định allow/deny) cho một subnet và export sang Cloud Logging (và tiếp sang BigQuery). Bật theo từng subnet. Dùng cho forensics mạng, phân tích bảo mật, nắm pattern traffic/chi phí, và gỡ lỗi kết nối — bản tương đương AWS VPC Flow Logs của GCP.
Hierarchical firewall policy
Ngoài firewall rule per-VPC, hierarchical firewall policy gắn ở cấp Organization hoặc Folder và được thừa kế xuống mọi VPC bên dưới. Chúng cho một team bảo mật trung tâm cưỡng chế allow/deny bắt buộc (ví dụ luôn cho phép dải health-check, luôn chặn một CIDR đã biết là độc hại) mà team project không ghi đè được. Đây là bản firewall của org policy phân cấp. Network firewall policy (global/regional) là kiểu nhóm mới ở cấp VPC bổ sung hoặc thay thế mô hình per-rule cổ điển.
Ví dụ: VPC + subnet + firewall rule
gcloud:
# 1. VPC global custom-mode
gcloud compute networks create prod-vpc --subnet-mode=custom
# 2. Subnet regional (primary + secondary range cho GKE pod)
gcloud compute networks subnets create prod-usc1 \
--network=prod-vpc --region=us-central1 \
--range=10.10.0.0/20 \
--secondary-range=pods=10.20.0.0/16,services=10.30.0.0/20 \
--enable-private-ip-google-access \
--enable-flow-logs
# 3. Cho phép HTTPS ingress chỉ tới instance gắn tag https-server
gcloud compute firewall-rules create allow-https \
--network=prod-vpc --direction=INGRESS --action=ALLOW \
--rules=tcp:443 --source-ranges=0.0.0.0/0 \
--target-tags=https-server --priority=1000
# 4. Cho phép SSH chỉ từ dải IAP
gcloud compute firewall-rules create allow-ssh-iap \
--network=prod-vpc --direction=INGRESS --action=ALLOW \
--rules=tcp:22 --source-ranges=35.235.240.0/20 \
--target-tags=ssh-allowed --priority=1000
(35.235.240.0/20 là dải TCP-forwarding của IAP — SSH qua IAP nghĩa là VM không có
public IP nào cả.)
Terraform tương đương:
resource "google_compute_network" "prod" {
name = "prod-vpc"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "usc1" {
name = "prod-usc1"
network = google_compute_network.prod.id
region = "us-central1"
ip_cidr_range = "10.10.0.0/20"
secondary_ip_range {
range_name = "pods"
ip_cidr_range = "10.20.0.0/16"
}
secondary_ip_range {
range_name = "services"
ip_cidr_range = "10.30.0.0/20"
}
private_ip_google_access = true
log_config { aggregation_interval = "INTERVAL_5_SEC" }
}
resource "google_compute_firewall" "allow_https" {
name = "allow-https"
network = google_compute_network.prod.id
direction = "INGRESS"
priority = 1000
source_ranges = ["0.0.0.0/0"]
target_tags = ["https-server"]
allow {
protocol = "tcp"
ports = ["443"]
}
}
Best Practices
- Luôn dùng VPC custom-mode trong production. Dải cố định của auto mode gây đụng độ khi peering/VPN và giao cho bạn subnet bạn chưa hề định.
- Quy hoạch CIDR không chồng lấn giữa mọi VPC, on-prem và cloud khác. Chồng lấn làm peering, VPN và sáp nhập tương lai đau đớn hoặc bất khả.
- Nhắm firewall rule theo service account, không chỉ network tag. Tag ai sửa được instance cũng đặt được; nhắm theo SA gắn với identity và khó giả mạo hơn nhiều.
- Default-deny và mở hẹp. Dựa vào deny-all-ingress ngầm định và chỉ mở đúng port/nguồn bạn cần; dùng deny rule cho ngoại lệ bên trong dải đã cho phép.
- Dùng Shared VPC cho org nhiều team. Tập trung subnet và firewall trong một host project do team mạng sở hữu; team app chỉ có quyền compute.
- Không cấp external IP cho VM; dùng Cloud NAT cho egress và IAP cho truy cập quản trị. Thu hẹp attack surface đáng kể — không gì có thể tiếp cận inbound từ internet.
- Bật Private Google Access trên subnet private. Cho VM chỉ nội bộ tiếp cận Google API mà không cần external IP hay hairpin qua NAT.
- Ưu tiên Private Service Connect để dùng managed/partner service. Giữ traffic trên private IP và tránh internet công cộng.
- Đặt web app công khai sau global external Application LB + Cloud Armor + Cloud CDN. Một stack cho bạn global anycast, WAF/DDoS và cache biên.
- Gắn health check cho mọi backend service và nhớ cho phép các dải
health-check của Google (
35.191.0.0/16,130.211.0.0/22) trong firewall. - Bật VPC Flow Logs trên subnet production. Thiết yếu cho forensics bảo mật và debug kết nối; tinh chỉnh sample-rate để kiểm soát chi phí.
- Dùng hierarchical firewall policy cho guardrail toàn org. Cưỡng chế allow/deny bắt buộc ở cấp org/folder mà team project không ghi đè được.
- Dùng Cloud DNS private zone cho tên nội bộ, DNSSEC cho public zone, và giữ TTL hợp lý để cân bằng tốc độ failover với chi phí query.
- Dùng Cloud Router + BGP cho kết nối hybrid (Cloud VPN / Interconnect) để route lan truyền động thay vì static route dễ vỡ.
- Thêm perimeter VPC Service Controls quanh dữ liệu nhạy cảm (bucket, BigQuery) để chặn exfiltration ngay cả với credential hợp lệ.
- Quản lý toàn bộ mạng bằng code (Terraform). Sai sót firewall và routing có blast radius lớn; thay đổi có review, có version là thiết yếu.
Tài liệu tham khảo
- VPC network overview
- Subnets
- Routes overview
- VPC firewall rules
- Firewall policies (hierarchical/network)
- Shared VPC
- VPC Network Peering
- Cloud NAT
- Cloud Router
- Cloud Load Balancing overview
- Application Load Balancer overview
- Cloud DNS
- Cloud CDN
- Private Google Access
- Private Service Connect
- VPC Flow Logs
- VPC Service Controls
Part of the Cloud knowledge base — GCP deep dive. Complements DevOps Roadmap.
Overview
Networking is where GCP diverges most sharply from AWS, and the difference is architectural, not cosmetic. In GCP a VPC network is a global resource: one VPC spans every region, and you add regional subnets to it as you expand. There is no per-region VPC and no need for peering just to connect two regions of your own network — Google’s private backbone already carries that traffic.
On top of that global fabric sit the pieces you assemble daily: subnets (IP
space), routes (where packets go), firewall rules (what is allowed),
Cloud Load Balancing (from a single global anycast IP down to internal L4),
Cloud NAT (egress for private VMs), Cloud DNS, and Cloud CDN. This
page maps them, calls out where GCP behaves differently from AWS, and shows
worked gcloud and Terraform examples.
Fundamentals
VPC network — global, with regional subnets
A VPC network is a virtual, software-defined network that is global.
Its subnets are regional and carve out IP ranges (CIDR blocks). Because the
VPC is global, a VM in us-central1 and a VM in europe-west1 on the same VPC
communicate over Google’s backbone using internal IPs — no peering, no gateway.
| Concept | AWS | GCP | Note |
|---|---|---|---|
| Virtual network | VPC (regional) | VPC (global) | GCP VPC has no region |
| IP range holder | Subnet (in one AZ) | Subnet (regional, spans zones) | GCP subnet covers all zones in its region |
| Packet-level rules | Security Group + NACL | Firewall rules (VPC-level) | See below |
| Cross-region link | VPC Peering / TGW | Native (same global VPC) | Intra-VPC is automatic |
Subnet modes:
- Auto mode — Google auto-creates one subnet per region from a fixed
10.128.0.0/9range. Convenient for demos, but the ranges are fixed and can collide on interconnect. Avoid in production. - Custom mode — you define every subnet and its CIDR explicitly. Always use custom mode for production.
Each subnet also has secondary IP ranges used for alias IPs — the mechanism GKE uses to give Pods and Services their own routable ranges (“VPC-native” clusters).
Routes
Routes tell packets where to go. Every VPC has:
- A default route to the internet (
0.0.0.0/0→ default internet gateway), which only takes effect for VMs that have an external IP or go through Cloud NAT. - Subnet routes — automatically created for each subnet’s range.
- Custom static routes — e.g. send
10.8.0.0/16to a VPN tunnel or a NAT appliance instance (next hop = instance, IP, VPN tunnel, or ILB). - Dynamic routes — learned via Cloud Router over BGP (for Cloud VPN / Interconnect).
Routes are global VPC resources but can be scoped by instance network tags.
Firewall rules (and how they differ from AWS)
GCP firewall rules operate at the VPC level, not per-instance like AWS Security Groups. Key differences:
| Aspect | AWS Security Group | GCP VPC firewall rule |
|---|---|---|
| Attached to | An ENI/instance | The VPC, targeted by tag / SA / range |
| Direction | Stateful, in/out | Stateful, INGRESS or EGRESS per rule |
| Allow/Deny | Allow-only | Allow and Deny rules |
| Priority | None (all allows union) | Numeric priority (0–65535, lower wins) |
| Default posture | Deny inbound, allow outbound | Implied deny all ingress, allow all egress |
| Targeting | The SG’s members | Network tags, service accounts, or IP ranges |
Two implied rules always exist at priority 65535: deny all ingress, allow all egress. You layer higher-priority (lower-number) rules on top. Because both allow and deny exist with priorities, you can express “allow the range but deny one noisy IP inside it” — impossible with AWS SGs alone.
Targeting by tag or service account is idiomatic GCP: instead of listing IPs,
you say “allow tcp:443 ingress to instances tagged https-server”, or
“…to instances running as serviceAccount:web@…”. SA-based targeting is stronger
because tags are freely settable by anyone who can edit an instance.
Shared VPC vs VPC Peering
Two ways to connect networks — pick based on org structure:
| Shared VPC | VPC Peering | |
|---|---|---|
| Model | One host project owns the VPC; service projects attach and deploy into its subnets | Two independent VPCs exchange routes |
| Boundary | Same organization, centralized network admin | Same or different orgs/projects |
| IAM | Central network team controls subnets/firewalls; app teams get compute only | Each side keeps full control |
| Transitivity | N/A (one network) | Non-transitive — A↔B and B↔C does not give A↔C |
| Use when | You want one team to own networking for many app projects | You need to connect two separately-owned VPCs |
Shared VPC is the standard enterprise landing-zone pattern: a network/security team owns the host project’s subnets and firewall rules, and application teams deploy workloads into those subnets from their own service projects without touching networking.
Key Concepts
Cloud NAT & Cloud Router
VMs without external IPs still often need outbound internet (package updates, calling external APIs). Cloud NAT provides managed, regional egress NAT with no NAT-instance to run or scale. It is a software-defined service (not a bottleneck instance) and integrates with Cloud Router, which manages the control plane and also runs BGP for dynamic routes over Cloud VPN / Interconnect.
# Regional Cloud Router + Cloud NAT so private VMs can reach the internet outbound
gcloud compute routers create nat-router \
--network=prod-vpc --region=us-central1
gcloud compute routers nats create prod-nat \
--router=nat-router --region=us-central1 \
--nat-all-subnet-ip-ranges \
--auto-allocate-nat-external-ips
Cloud NAT is egress-only — it does not allow unsolicited inbound. Inbound to private VMs comes via a load balancer or IAP.
Cloud Load Balancing
Google’s load balancers are a family; the first split is global vs regional, the second is external vs internal, the third is L7 (HTTP/S) vs L4 (TCP/UDP).
| Load balancer | Scope | Layer | Use for |
|---|---|---|---|
| Global external Application LB | Global (single anycast IP) | L7 HTTP(S) | Public web apps, global routing, Cloud CDN, Cloud Armor |
| Regional external Application LB | Regional | L7 HTTP(S) | Region-scoped web apps, regulatory locality |
| External passthrough Network LB | Regional | L4 TCP/UDP | Non-HTTP protocols, preserve client IP |
| Internal Application LB | Regional | L7 HTTP(S) | Internal microservice routing |
| Internal passthrough Network LB | Regional | L4 TCP/UDP | Internal L4 (databases, internal services) |
The global external Application Load Balancer is a signature GCP feature: a single anycast IP advertised worldwide sends each user to the nearest healthy backend over Google’s backbone, with built-in Cloud CDN and Cloud Armor (WAF/DDoS). This is the AWS “CloudFront + global accelerator + ALB” combination in one product. Its building blocks: forwarding rule → target proxy → URL map → backend service → backend (MIG/NEG) → health check.
Cloud DNS
Cloud DNS is Google’s managed, authoritative DNS with a 100% uptime SLA.
- Public zones serve records to the internet.
- Private zones resolve names only inside chosen VPCs (split-horizon).
- DNS peering and forwarding integrate with on-prem/other VPC resolvers.
- DNSSEC signs public zones.
gcloud dns managed-zones create app-zone \
--dns-name="app.example.com." --description="app zone" --visibility=public
gcloud dns record-sets create www.app.example.com. \
--zone=app-zone --type=A --ttl=300 --rrdatas="203.0.113.10"
Cloud CDN
Cloud CDN caches content at Google’s edge POPs, fronting a backend service on the global external Application Load Balancer. Enabling it is a flag on the backend service; it uses cache keys, TTLs, and signed URLs/cookies for private content. Because it rides the same LB, you get global anycast + CDN + WAF as one stack.
Private connectivity: Private Google Access & Private Service Connect
Reaching Google APIs (Cloud Storage, BigQuery) and managed services without traversing the public internet:
- Private Google Access — lets VMs with only internal IPs reach Google APIs and services via internal routing. Enabled per-subnet.
- Private Service Connect (PSC) — exposes a service (a Google API, a managed service like Cloud SQL, or a partner/your own service) behind a private IP in your VPC. You consume it as if it lived in your network; traffic never touches the internet. The modern replacement for many private-access patterns.
- VPC Service Controls — a separate control that draws a service perimeter around resources (e.g. buckets) to stop data exfiltration even by valid credentials.
VPC Flow Logs
VPC Flow Logs sample the network flows (5-tuple, bytes, packets, latency, allow/deny decision) for a subnet and export them to Cloud Logging (and onward to BigQuery). Enabled per-subnet. Use them for network forensics, security analysis, cost/traffic-pattern insight, and troubleshooting connectivity — GCP’s analog to AWS VPC Flow Logs.
Hierarchical firewall policies
Beyond per-VPC firewall rules, hierarchical firewall policies attach at the Organization or Folder level and are inherited down to all VPCs beneath. They let a central security team enforce mandatory allow/deny (e.g. always allow health-check ranges, always deny a known-bad CIDR) that project teams cannot override. This is the firewall analog of hierarchical org policies. Network firewall policies (global/regional) are the newer VPC-level grouping that complements or replaces the classic per-rule model.
Worked example: VPC + subnet + firewall rule
gcloud:
# 1. Custom-mode global VPC
gcloud compute networks create prod-vpc --subnet-mode=custom
# 2. Regional subnet (primary + secondary range for GKE pods)
gcloud compute networks subnets create prod-usc1 \
--network=prod-vpc --region=us-central1 \
--range=10.10.0.0/20 \
--secondary-range=pods=10.20.0.0/16,services=10.30.0.0/20 \
--enable-private-ip-google-access \
--enable-flow-logs
# 3. Allow HTTPS ingress only to instances tagged https-server
gcloud compute firewall-rules create allow-https \
--network=prod-vpc --direction=INGRESS --action=ALLOW \
--rules=tcp:443 --source-ranges=0.0.0.0/0 \
--target-tags=https-server --priority=1000
# 4. Allow SSH only from the corporate range via IAP
gcloud compute firewall-rules create allow-ssh-iap \
--network=prod-vpc --direction=INGRESS --action=ALLOW \
--rules=tcp:22 --source-ranges=35.235.240.0/20 \
--target-tags=ssh-allowed --priority=1000
(35.235.240.0/20 is Google’s IAP TCP-forwarding range — SSH via IAP means no
public IP on the VM at all.)
Terraform equivalent:
resource "google_compute_network" "prod" {
name = "prod-vpc"
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "usc1" {
name = "prod-usc1"
network = google_compute_network.prod.id
region = "us-central1"
ip_cidr_range = "10.10.0.0/20"
secondary_ip_range {
range_name = "pods"
ip_cidr_range = "10.20.0.0/16"
}
secondary_ip_range {
range_name = "services"
ip_cidr_range = "10.30.0.0/20"
}
private_ip_google_access = true
log_config { aggregation_interval = "INTERVAL_5_SEC" }
}
resource "google_compute_firewall" "allow_https" {
name = "allow-https"
network = google_compute_network.prod.id
direction = "INGRESS"
priority = 1000
source_ranges = ["0.0.0.0/0"]
target_tags = ["https-server"]
allow {
protocol = "tcp"
ports = ["443"]
}
}
Best Practices
- Always use custom-mode VPCs in production. Auto mode’s fixed ranges cause collisions on peering/VPN and hand you subnets you never planned.
- Plan non-overlapping CIDRs across all VPCs, on-prem, and other clouds. Overlaps make peering, VPN, and future mergers painful or impossible.
- Target firewall rules by service account, not just network tags. Tags are editable by anyone who can modify an instance; SA-based targeting is tied to identity and far harder to spoof.
- Default-deny and grant narrowly. Rely on the implied deny-all-ingress and open only the specific ports/sources you need; use deny rules for exceptions inside allowed ranges.
- Use Shared VPC for multi-team orgs. Centralize subnets and firewalls in a host project owned by the network team; app teams get compute-only access.
- Give VMs no external IP; use Cloud NAT for egress and IAP for admin access. Shrinks the attack surface dramatically — nothing is reachable inbound from the internet.
- Enable Private Google Access on private subnets. Lets internal-only VMs reach Google APIs without an external IP or NAT hairpin.
- Prefer Private Service Connect for consuming managed/partner services. Keeps traffic on private IPs and off the public internet.
- Front public web apps with the global external Application LB + Cloud Armor + Cloud CDN. One stack gives you global anycast, WAF/DDoS, and edge caching.
- Attach health checks to every backend service and remember to allow
Google’s health-check ranges (
35.191.0.0/16,130.211.0.0/22) in your firewall. - Enable VPC Flow Logs on production subnets. Essential for security forensics and connectivity debugging; sample-rate-tune to control cost.
- Use hierarchical firewall policies for org-wide guardrails. Enforce mandatory allow/deny at org/folder level that project teams cannot override.
- Use Cloud DNS private zones for internal names, DNSSEC on public zones, and keep TTLs sensible for failover speed vs query cost.
- Use Cloud Router + BGP for hybrid connectivity (Cloud VPN / Interconnect) so routes propagate dynamically instead of brittle static routes.
- Add VPC Service Controls perimeters around sensitive data (buckets, BigQuery) to prevent exfiltration even with valid credentials.
- Manage all networking as code (Terraform). Firewall and routing mistakes are high-blast-radius; reviewed, version-controlled changes are essential.
References
- VPC network overview
- Subnets
- Routes overview
- VPC firewall rules
- Firewall policies (hierarchical/network)
- Shared VPC
- VPC Network Peering
- Cloud NAT
- Cloud Router
- Cloud Load Balancing overview
- Application Load Balancer overview
- Cloud DNS
- Cloud CDN
- Private Google Access
- Private Service Connect
- VPC Flow Logs
- VPC Service Controls