Skip to content

🌐 VPC & Networking

Level: Core Solves: Thiết kế network architecture cho enterprise workloads với security, scalability, và hybrid connectivity trên GCP

🎯 Mục tiêu (Outcomes)

Sau khi áp dụng kiến thức trong trang này, bạn sẽ có khả năng:

  • Thiết kế GCP VPC với Custom-mode và proper subnet sizing
  • Triển khai Shared VPC cho multi-project environments
  • Cấu hình Firewall Rules với hierarchical policies
  • Enable Private Connectivity qua PSC và Private Google Access
  • Implement Hybrid Connectivity với HA VPN hoặc Interconnect
  • Phân tích VPC Flow Logs cho security và troubleshooting

Khi nào dùng

PatternUse CaseLý do
Shared VPCMulti-project organizationCentralized network management
Custom-mode VPCProduction workloadsFull CIDR control
Private Service ConnectAccess Google APIsNo public IPs, security
HA VPNQuick hybrid setupEasy, encrypted, 99.99% SLA
InterconnectHigh bandwidth needsLow latency, high throughput

Khi nào KHÔNG dùng

PatternVấn đềThay thế
Auto-mode VPCPreset CIDRs, conflictsCustom-mode VPC
Network tags for firewallMutable, security riskService account-based rules
Classic VPNSingle tunnel, no SLAHA VPN
Public IPs for internal servicesSecurity exposurePrivate Google Access
VPC Peering for many projectsDoesn't scaleShared VPC

⚠️ Cảnh báo từ Raizo

"Một team dùng Auto-mode VPC. 6 tháng sau cần kết nối với on-prem network có overlapping CIDR (10.128.0.0/20). Không thể change VPC mode. Phải recreate tất cả resources. Luôn dùng Custom-mode VPC từ đầu."

Core Concepts

GCP VPC Characteristics

┌─────────────────────────────────────────────────────────────────┐
│                    GCP VPC vs AWS VPC                           │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  GCP VPC                              AWS VPC                   │
│  ───────                              ───────                   │
│  • GLOBAL resource                    • REGIONAL resource       │
│  • Subnets are regional               • Subnets are zonal (AZ)  │
│  • Auto-mode or Custom-mode           • Always custom           │
│  • Firewall rules at VPC level        • Security Groups + NACLs │
│  • Routes are global                  • Routes per route table  │
│                                                                 │
│  KEY INSIGHT: GCP VPC spans all regions automatically.          │
│  Subnets define regional boundaries, not VPC.                   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Enterprise VPC Architecture

┌─────────────────────────────────────────────────────────────────┐
│                 ENTERPRISE VPC DESIGN                           │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  VPC: prod-vpc (Global)                                         │
│  ┌─────────────────────────────────────────────────────────┐    │
│  │                                                         │    │
│  │  Region: asia-southeast1                                │    │
│  │  ┌─────────────────────────────────────────────────┐    │    │
│  │  │  Subnet: prod-ase1-app                          │    │    │
│  │  │  CIDR: 10.10.0.0/20 (4,096 IPs)                 │    │    │
│  │  │  • GKE nodes, Cloud Run, App workloads          │    │    │
│  │  └─────────────────────────────────────────────────┘    │    │
│  │  ┌─────────────────────────────────────────────────┐    │    │
│  │  │  Subnet: prod-ase1-data                         │    │    │
│  │  │  CIDR: 10.10.16.0/20                            │    │    │
│  │  │  • Cloud SQL, Memorystore, Dataproc             │    │    │
│  │  └─────────────────────────────────────────────────┘    │    │
│  │                                                         │    │
│  │  Region: us-central1                                    │    │
│  │  ┌─────────────────────────────────────────────────┐    │    │
│  │  │  Subnet: prod-usc1-app                          │    │    │
│  │  │  CIDR: 10.20.0.0/20                             │    │    │
│  │  └─────────────────────────────────────────────────┘    │    │
│  │                                                         │    │
│  └─────────────────────────────────────────────────────────┘    │
│                                                                 │
│  ⚠️ Use Custom-mode VPC for enterprise (not Auto-mode)          │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Shared VPC

Hub-Spoke with Shared VPC

┌─────────────────────────────────────────────────────────────────┐
│                    SHARED VPC ARCHITECTURE                      │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  HOST PROJECT (Network Admin)                                   │
│  ┌─────────────────────────────────────────────────────────┐    │
│  │  prj-network-hub                                        │    │
│  │  ┌─────────────────────────────────────────────────┐    │    │
│  │  │              Shared VPC                          │    │    │
│  │  │  ┌─────────┐ ┌─────────┐ ┌─────────┐            │    │    │
│  │  │  │Subnet A │ │Subnet B │ │Subnet C │            │    │    │
│  │  │  │Team-1   │ │Team-2   │ │Shared   │            │    │    │
│  │  │  └────┬────┘ └────┬────┘ └────┬────┘            │    │    │
│  │  └───────┼───────────┼───────────┼─────────────────┘    │    │
│  └──────────┼───────────┼───────────┼──────────────────────┘    │
│             │           │           │                           │
│  SERVICE PROJECTS (Workload Teams)                              │
│  ┌──────────┼───────────┼───────────┼──────────────────────┐    │
│  │          ▼           ▼           ▼                      │    │
│  │  ┌─────────────┐ ┌─────────────┐                        │    │
│  │  │ prj-team-1  │ │ prj-team-2  │                        │    │
│  │  │ (GKE, VMs)  │ │ (Cloud Run) │                        │    │
│  │  └─────────────┘ └─────────────┘                        │    │
│  └─────────────────────────────────────────────────────────┘    │
│                                                                 │
│  BENEFITS:                                                      │
│  • Centralized network management                               │
│  • Consistent firewall policies                                 │
│  • Simplified connectivity (no peering needed)                  │
│  • Clear separation of duties                                   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Shared VPC IAM Roles

RoleScopeResponsibility
Shared VPC AdminOrganization/FolderEnable Shared VPC, attach projects
Network AdminHost ProjectCreate/manage VPC, subnets, firewall
Network UserSubnet levelUse subnet in service project
Compute Network UserSubnet levelCreate VMs in subnet
bash
# Grant Network User on specific subnet
gcloud compute networks subnets add-iam-policy-binding SUBNET_NAME \
  --region=REGION \
  --member="serviceAccount:SA@PROJECT.iam.gserviceaccount.com" \
  --role="roles/compute.networkUser" \
  --project=HOST_PROJECT

Firewall Rules

Firewall Rule Hierarchy

┌─────────────────────────────────────────────────────────────────┐
│                 FIREWALL RULE PRIORITY                          │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  Priority 0 (Highest)                                           │
│  ┌─────────────────────────────────────────────────────────┐    │
│  │  Hierarchical Firewall Policies (Org/Folder level)      │    │
│  │  • Enforced across all projects                         │    │
│  │  • Cannot be overridden by project rules                │    │
│  └─────────────────────────────────────────────────────────┘    │
│                          │                                      │
│                          ▼                                      │
│  Priority 1000 (Default)                                        │
│  ┌─────────────────────────────────────────────────────────┐    │
│  │  VPC Firewall Rules (Project level)                     │    │
│  │  • Lower number = higher priority                       │    │
│  │  • Evaluated until first match                          │    │
│  └─────────────────────────────────────────────────────────┘    │
│                          │                                      │
│                          ▼                                      │
│  Priority 65535 (Lowest)                                        │
│  ┌─────────────────────────────────────────────────────────┐    │
│  │  Implied Rules                                          │    │
│  │  • Deny all ingress (65535)                             │    │
│  │  • Allow all egress (65535)                             │    │
│  └─────────────────────────────────────────────────────────┘    │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Firewall Best Practices

yaml
# Allow internal communication
- name: allow-internal
  direction: INGRESS
  priority: 1000
  sourceRanges: ["10.0.0.0/8"]
  allowed:
    - IPProtocol: tcp
    - IPProtocol: udp
    - IPProtocol: icmp

# Allow health checks from GCP
- name: allow-health-checks
  direction: INGRESS
  priority: 1000
  sourceRanges: 
    - "35.191.0.0/16"    # Health check ranges
    - "130.211.0.0/22"
  targetTags: ["allow-health-check"]
  allowed:
    - IPProtocol: tcp
      ports: ["80", "443", "8080"]

# Deny all other ingress (explicit)
- name: deny-all-ingress
  direction: INGRESS
  priority: 65000
  sourceRanges: ["0.0.0.0/0"]
  denied:
    - IPProtocol: all

💡 Network Tags vs Service Accounts

Prefer Service Account-based firewall rules over network tags. Service accounts are immutable and tied to identity, while tags can be modified by anyone with compute.instances.setTags permission.

Private Services Access

Private Google Access

┌─────────────────────────────────────────────────────────────────┐
│              PRIVATE GOOGLE ACCESS                              │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  WITHOUT Private Google Access:                                 │
│  ┌─────────────────────────────────────────────────────────┐    │
│  │  VM (no external IP) ──X──► Google APIs (storage.google │    │
│  │                              apis.com)                  │    │
│  │  ❌ Cannot reach Google APIs                            │    │
│  └─────────────────────────────────────────────────────────┘    │
│                                                                 │
│  WITH Private Google Access:                                    │
│  ┌─────────────────────────────────────────────────────────┐    │
│  │  VM (no external IP) ────► Google APIs                  │    │
│  │                            (via internal routing)       │    │
│  │  ✅ Traffic stays on Google network                     │    │
│  └─────────────────────────────────────────────────────────┘    │
│                                                                 │
│  Enable per subnet:                                             │
│  gcloud compute networks subnets update SUBNET \                │
│    --enable-private-ip-google-access                            │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Private Service Connect

Hybrid Connectivity

Cloud Interconnect vs Cloud VPN

AspectDedicated InterconnectPartner InterconnectCloud VPN
Bandwidth10-200 Gbps50 Mbps - 50 GbpsUp to 3 Gbps
LatencyLowestLowVariable
Setup TimeWeeksDaysMinutes
CostHighestMediumLowest
EncryptionMACsec optionalMACsec optionalIPsec always
SLA99.99% (with redundancy)99.99%99.99%

HA VPN Architecture

┌─────────────────────────────────────────────────────────────────┐
│                    HA VPN TOPOLOGY                              │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  GCP Region                          On-Premises                │
│  ┌─────────────────────┐            ┌─────────────────────┐    │
│  │                     │            │                     │    │
│  │  ┌───────────────┐  │            │  ┌───────────────┐  │    │
│  │  │ HA VPN Gateway│  │            │  │  VPN Device   │  │    │
│  │  │  Interface 0  │◄─┼────────────┼─►│   Tunnel 0    │  │    │
│  │  │  Interface 1  │◄─┼────────────┼─►│   Tunnel 1    │  │    │
│  │  └───────────────┘  │            │  └───────────────┘  │    │
│  │         │           │            │                     │    │
│  │         ▼           │            │                     │    │
│  │  ┌───────────────┐  │            │                     │    │
│  │  │ Cloud Router  │  │            │                     │    │
│  │  │    (BGP)      │  │            │                     │    │
│  │  └───────────────┘  │            │                     │    │
│  │                     │            │                     │    │
│  └─────────────────────┘            └─────────────────────┘    │
│                                                                 │
│  99.99% SLA requires:                                           │
│  • 2 tunnels from each HA VPN interface                         │
│  • BGP sessions on all tunnels                                  │
│  • Proper failover configuration                                │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

VPC Flow Logs

Configuration

bash
gcloud compute networks subnets update SUBNET_NAME \
  --enable-flow-logs \
  --logging-aggregation-interval=interval-5-sec \
  --logging-flow-sampling=0.5 \
  --logging-metadata=include-all \
  --logging-filter-expr="src_ip != '10.0.0.1'"

Log Analysis (BigQuery)

sql
-- Top talkers by bytes
SELECT
  jsonPayload.connection.src_ip,
  jsonPayload.connection.dest_ip,
  SUM(CAST(jsonPayload.bytes_sent AS INT64)) as total_bytes
FROM `project.dataset.compute_googleapis_com_vpc_flows_*`
WHERE _TABLE_SUFFIX >= '20240101'
GROUP BY 1, 2
ORDER BY total_bytes DESC
LIMIT 10;

-- Denied connections
SELECT
  jsonPayload.connection.src_ip,
  jsonPayload.connection.dest_ip,
  jsonPayload.connection.dest_port,
  COUNT(*) as deny_count
FROM `project.dataset.compute_googleapis_com_vpc_flows_*`
WHERE jsonPayload.reporter = 'DEST'
  AND jsonPayload.disposition = 'DENIED'
GROUP BY 1, 2, 3
ORDER BY deny_count DESC;

Best Practices Checklist

  • [ ] Use Custom-mode VPC (not Auto-mode)
  • [ ] Implement Shared VPC for multi-project environments
  • [ ] Enable Private Google Access on all subnets
  • [ ] Use Service Account-based firewall rules
  • [ ] Implement Hierarchical Firewall Policies at org level
  • [ ] Enable VPC Flow Logs for security analysis
  • [ ] Plan CIDR ranges for future growth (no overlap)
  • [ ] Use HA VPN or Interconnect for hybrid connectivity

⚖️ Trade-offs

Trade-off 1: Shared VPC vs VPC Peering

Khía cạnhShared VPCVPC Peering
Centralized managementYesNo
Firewall policiesUnifiedPer-VPC
ScalingTốtLimited
Setup complexityMediumLow
IAM separationClearSeparate

Khuyến nghị: Shared VPC cho enterprise. VPC Peering cho isolated workloads hoặc third-party.


Trade-off 2: Interconnect vs VPN

Khía cạnhCloud InterconnectHA VPN
BandwidthLên đến 200 Gbps3 Gbps/tunnel
LatencyLowestVariable
Setup timeTuầnPhút
CostCaoThấp
EncryptionOptional (MACsec)Always (IPsec)

Trade-off 3: Flow Logs Sampling Rate

SamplingStorage CostVisibilityUse Case
100%Very highFullSecurity investigations
50%MediumGoodProduction monitoring
10%LowLimitedCost-sensitive
OffNoneNoneNon-critical

🚨 Failure Modes

Failure Mode 1: CIDR Conflicts

🔥 Incident thực tế

Team A và Team B cả hai dùng 10.0.0.0/16. Khi cần connect qua VPC Peering, không thể route. Phải renumber toàn bộ một VPC. 2 tuần downtime.

Cách phát hiệnCách phòng tránh
Peering creation failsCentral IPAM planning
Route conflictsNon-overlapping CIDR allocation
Connectivity issuesUse /16 block per environment

Failure Mode 2: Firewall Misconfiguration

Cách phát hiệnCách phòng tránh
Unexpected connectivityTest in staging
Security scan findingsIaC cho firewall rules
Flow log shows denied trafficHierarchical policies

Failure Mode 3: Hybrid Connectivity Failure

Cách phát hiỆnCách phòng tránh
BGP session downMonitor Cloud Router BGP
Tunnel status unhealthyRedundant tunnels
Latency spikesMulti-path routing

🔐 Security Baseline

Network Security Requirements

RequirementImplementationVerification
No public IPsOrg policy enforcedCloud Asset Inventory
Private Google AccessEnabled all subnetsSubnet audit
Hierarchical FirewallOrg-level policiesPolicy review
VPC Flow LogsEnabled, exportedLog sink verified
SA-based firewallNo network tagsRule audit

Firewall Rule Strategy

┌─────────────────────────────────────────────────────────────────┐
│                FIREWALL RULE LAYERS                             │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  L1: Org Hierarchical Policy (Deny known bad IPs)              │
│  L2: Folder Hierarchical Policy (Prod stricter than dev)       │
│  L3: VPC Firewall Rules (Allow internal, health checks)        │
│  L4: Implied Deny All Ingress                                   │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

📊 Ops Readiness

Metrics cần Monitoring

MetricSourceAlert Threshold
VPN tunnel statusCloud MonitoringAny down
BGP session statusCloud RouterSession down
Firewall denied connectionsFlow LogsSpike
Subnet IP utilizationCloud Monitoring> 80%
Interconnect circuit statusCloud MonitoringAny issues

Runbook Entry Points

Tình huốngRunbook
VPN tunnel downrunbook/vpn-troubleshooting.md
BGP flappingrunbook/bgp-troubleshooting.md
Connectivity issuesrunbook/network-connectivity-debug.md
Firewall blocking trafficrunbook/firewall-investigation.md
Subnet exhaustionrunbook/subnet-expansion.md
Interconnect issuesrunbook/interconnect-troubleshooting.md

Design Review Checklist

VPC Design

  • [ ] Custom-mode VPC
  • [ ] CIDR plan documented
  • [ ] No overlapping ranges
  • [ ] Subnet sizing adequate

Connectivity

  • [ ] Private Google Access enabled
  • [ ] PSC cho managed services
  • [ ] Hybrid connectivity redundant
  • [ ] BGP configured properly

Security

  • [ ] Hierarchical firewall policies
  • [ ] SA-based firewall rules
  • [ ] VPC Flow Logs enabled
  • [ ] No public IPs unnecessary

Operations

  • [ ] Network monitoring configured
  • [ ] Flow logs exported
  • [ ] Runbooks documented
  • [ ] CIDR tracking maintained

📎 Liên kết