Labs / Cloud / Containers & VPCs

Lab 2 of 3 · Containers & VPCs

Virtual networks: how a cloud VPC sorts its subnets and addresses

Read a VPC's route tables, and give a server the public address it never sees.

Start this lab 4 steps

The idea

Ferris Tickets runs in a cloud now: a private network of its own, two subnets, one gateway. Find out what makes a subnet public, and give a server a public address.

The gateway holds the public address
The gateway holds the public address
Two subnets, two route tables
Two subnets, two route tables
A way across, no way in
A way across, no way in
A route out is not an address
A route out is not an address

Steps

  1. An address the server can't see

    Tomas sells tickets for a small theatre through his site, Ferris Tickets. He moved it from a server in his office to a cloud provider, which rents him a VPC, a virtual private cloud: a private network of his own, 10.0.0.0/16, carved out of the provider's huge shared one.

    Renting machines and a network this way is called IaaS, infrastructure as a service. The provider runs the hardware and the network under it, and Tomas runs everything on his machines.

    His web server, web, sits in the VPC at 10.0.0.10. Customers reach it at 203.0.113.50, a public address the provider lends him, which AWS calls an elastic IP.

    Show the addresses web has. Fetch a page from payments, Tomas's card payment company out on the internet, whose page names the address a request came from. Then fetch the site from customer at its public address, while the lab records on web.

    web:~# ip -4 -br addr
    web:~# curl -sS -m 3 http://198.51.100.10/
    customer:~# curl -sS -m 3 http://203.0.113.50/
  2. A route table for each subnet

    Tomas split his VPC into two subnets. public, 10.0.0.0/24, holds web and standby, a second copy of the site. private, 10.0.1.0/24, holds db, the orders database, which nobody on the internet should reach.

    What makes a cloud subnet public or private is one thing: its route table. Every subnet is tied to one route table, and only a public subnet's table has a default route, 0.0.0.0/0, pointing at the internet gateway.

    At a provider, an unseen Router reads these tables, and you edit them on the provider's website. Here that router is vpcRouter, a Linux router with one table per subnet. Linux's ip rule picks the table by the packet's sender: packets from 10.0.0.0/24 read public, and packets from 10.0.1.0/24 read private.

    Read the rules on vpcRouter, then both tables. Then ask payments for its page from db.

    vpcRouter:~# ip rule
    vpcRouter:~# ip route show table public
    vpcRouter:~# ip route show table private
    db:~# curl -sS -m 3 http://198.51.100.10/
  3. Reachable from inside only

    db can't go out, but web has to reach it: every ticket sold is an order written to the database.

    The local route takes care of that. Every table in the VPC knows both subnets, so web reaches db by its private address, and the answer comes back the same way. Nothing crosses igw, and nothing gets rewritten.

    From outside it's another story. 10.0.0.0/8 is a private block: thousands of companies use it at once, so no router on the internet keeps a route to it. isp, the provider's link to the internet, knows only the block every elastic IP comes from.

    Fetch the orders page at 10.0.1.20 from web. Then try db and web by their private addresses from customer, and read the routes on isp.

    web:~# curl -sS -m 3 http://10.0.1.20/
    customer:~# curl -sS -m 3 http://10.0.1.20/
    customer:~# curl -sS -m 3 http://10.0.0.10/
    isp:~# ip route
  4. A public subnet isn't enough

    standby, at 10.0.0.11, is the second copy of the site, in the public subnet next to web. Tomas keeps it for the night web fails, and it has to reach payments to take orders on its own.

    Its subnet's table has the default route to igw, so its packets do head out. What standby lacks is an elastic IP: igw holds rules for web and nobody else, so a request from standby leaves with 10.0.0.11 on it.

    Try payments from standby while the lab records on isp, where the provider meets the internet.

    Then give standby the elastic IP 203.0.113.51. It takes the two rules web has on igw: DNAT, which rewrites the receiver of what comes in, and SNAT, which rewrites the sender of what goes out. Try payments again, then fetch the new address from customer.

    standby:~# curl -sS -m 3 http://198.51.100.10/
    igw:~# iptables -v -t nat -A PREROUTING -d 203.0.113.51 -j DNAT --to-destination 10.0.0.11
    igw:~# iptables -v -t nat -A POSTROUTING -s 10.0.0.11 -j SNAT --to-source 203.0.113.51
    standby:~# curl -sS -m 3 http://198.51.100.10/
    customer:~# curl -sS -m 3 http://203.0.113.51/

Run these steps on real machines

Keep reading

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab