Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 35 ยท Droplets and Networking

VPC and Private Networking on DigitalOcean

Learn how DigitalOcean VPC networks isolate resources per region, how private IPs work, how to plan CIDR ranges, peer VPCs, and keep databases off the public internet.

By default every Droplet has a public IPv4 address, reachable from anywhere. Your database, cache and internal services usually should not be. A VPC (Virtual Private Cloud) gives resources a private network so they can talk to each other without crossing the public internet. This lesson explains how VPCs work on DigitalOcean, how to choose address ranges, and how to use them well.

What a VPC is

A VPC is a private network that is tied to one region. When you create a Droplet, a managed database, a load balancer or a Kubernetes cluster in a region, it joins a VPC there (the region's default VPC unless you pick another). Resources in the same VPC:

  • get a private IP address from the VPC's range, for example 10.114.0.2;
  • can reach each other on those private addresses;
  • are isolated from other customers' networks and from your other VPCs;
  • exchange traffic for free, and it does not count toward the bandwidth allowance.

The public IP does not go away: a Droplet usually has both a public and a private address. A Cloud Firewall (next lessons) decides what may reach which one.

flowchart TB
  Internet(("Internet"))
  subgraph VPC["VPC 10.114.0.0/20 in fra1"]
    LB["Load balancer"]
    W1["web-1<br/>10.114.0.2"]
    W2["web-2<br/>10.114.0.3"]
    DB[("Managed DB<br/>10.114.0.5")]
  end
  Internet --> LB
  LB --> W1
  LB --> W2
  W1 --> DB
  W2 --> DB

Only the load balancer is public. Web servers and the database talk over private addresses.

Choosing address ranges

VPC ranges come from the private IPv4 blocks defined by RFC 1918 (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16). A CIDR such as 10.114.0.0/20 means "addresses from 10.114.0.0 to 10.114.15.255", about 4,096 addresses. The number after the slash is how many leading bits are fixed; a smaller number means a bigger network.

If you do not choose, DigitalOcean assigns a default range to the region's default VPC. For new VPCs you can pick the range yourself. Plan it with these rules:

  • Do not overlap ranges you may connect later (other VPCs, an office network, a VPN). Overlapping networks cannot be routed to each other.
  • Leave room to grow. A /20 or larger is comfortable for most teams; DigitalOcean also reserves a few addresses in each range.
  • Document which VPC is for which environment (production, staging).

Creating a VPC with doctl (read-only example):

Terminal
doctl vpcs create --name prod-fra1 --region fra1 --ip-range 10.20.0.0/20
doctl vpcs list --format ID,Name,Region,IPRange,Default
doctl compute droplet create db-1 --vpc-uuid <vpc-id> ...

A resource's VPC is chosen at creation. It can be moved between VPCs in some cases, but plan to get it right the first time.

Using private addresses

Inside the Droplet the private address appears on an interface (often eth1):

Terminal
ip -4 addr show eth1

Applications should connect to peers by private IP or private hostname. Managed databases give you a hostname starting with private- for exactly this purpose, so traffic from your Droplets stays inside the VPC.

You can bind internal services to the private address only, so they are not listening on the public interface at all:

TEXT
# Example: PostgreSQL on your own Droplet
listen_addresses = 'localhost,10.114.0.5'

Combined with a firewall rule that only allows the web tier's tag to reach port 5432, this is a very strong default.

Peering and reaching other networks

  • VPC peering connects two VPCs (in the same or different regions) so they can reach each other privately. Their ranges must not overlap.
  • A VPN such as WireGuard on a gateway Droplet lets your office or laptop reach private addresses. Route the VPC's CIDR through the tunnel.
  • Public access to a private-only server goes through a bastion host (ProxyJump, see SSH keys).

Remember that a VPC is a regional boundary: a Droplet in nyc3 cannot use a private address to reach one in fra1 unless the VPCs are peered.

Planning with Python's ipaddress module

The standard library module ipaddress makes CIDR arithmetic painless. This is real, working code (the ranges are examples, not a DigitalOcean API).

Python Plan VPC ranges and check membership

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Stand-in: should this connection use the public or private address?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Good habits

  • Put databases, caches and queues on private addresses only.
  • Use one VPC per environment so a mistake in staging cannot touch production.
  • Pair VPCs with tag-based Cloud Firewalls: the VPC provides the path, the firewall provides the policy.
  • Remember that private does not mean encrypted: use TLS for sensitive data, which managed databases require anyway.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A VPC on DigitalOcean is scoped to...

    VPCs are regional. Resources in other regions need peering or the public internet.
  2. 2. Which traffic is free and not counted against bandwidth?

    Private VPC traffic is free; public traffic counts toward the pooled allowance.
  3. 3. Why must VPC ranges not overlap if you want to connect them?

    Routing needs unambiguous destinations; overlapping CIDRs make some addresses ambiguous.
  4. 4. How many addresses does a /20 contain?

    32 - 20 = 12 host bits, and 2^12 = 4,096.

Summary

  • A VPC is a regional private network; resources in it get private IPs and exchange traffic for free.
  • Plan non-overlapping CIDR ranges with room to grow; use one VPC per environment.
  • Bind databases and internal services to private addresses and use private- hostnames for managed databases.
  • Peering, VPN and bastion hosts connect other networks to a VPC; the firewall defines who may talk to whom.

Next: Cloud Firewalls