Labs / Cloud / Containers & VPCs

Lab 3 of 3 · Containers & VPCs

Cloud routing: how a private subnet gets out, and how two VPCs meet

Build a NAT gateway for a private subnet, see why nothing gets in through it, and route a peering link.

Start this lab 4 steps

The idea

Lumen Books keeps its order system in a private subnet, where nothing on the internet can reach it. Give it a way out for its updates, see why the way out lets nothing in, and join a partner's VPC.

One table has a default line
One table has a default line
Rewritten twice on the way out
Rewritten twice on the way out
Out and back, never in
Out and back, never in
A route on each side
A route on each side

Steps

  1. A private server with no way out

    Priya runs Lumen Books, an online bookshop, in a VPC at a cloud provider. The shop's web server, web, sits in the public subnet, 10.0.0.0/24, with an elastic IP, 203.0.113.40.

    The order system, app, holds every customer's address, so it sits in the private subnet, 10.0.1.0/24. Each subnet reads its own route table, and the private one has no route to the internet. Nobody out there can reach app.

    That's the point of a private subnet, but app still needs its security updates. They come from mirror, a package mirror on the internet whose page names the address each request came from.

    Ask mirror for its page from web and from app. Then read the private subnet's table on vpcRouter, the Router that plays the provider's.

    web:~# curl -sS -m 3 http://198.51.100.10/
    app:~# curl -sS -m 3 http://198.51.100.10/
    vpcRouter:~# ip route show table private
  2. Build a NAT gateway

    A NAT gateway is the cloud's answer: a machine in the public subnet that lends its own address to the private subnet's requests. Here it's natGateway, at 10.0.0.5, with an elastic IP of its own, 203.0.113.60.

    On AWS, you press "Create NAT gateway" and add one line to the private subnet's route table: 0.0.0.0/0 to the NAT gateway. Here you build what the button builds, in two commands.

    First, the NAT rule on natGateway. MASQUERADE gives every packet from 10.0.1.0/24 the gateway's own address on the way out, and conntrack, the kernel's list of conversations, turns each answer back into app's address.

    Second, the route on vpcRouter: the private table's default route, through 10.0.0.5.

    Then fetch the updates from app again, while the lab records on igw, the internet gateway, where the VPC meets the provider.

    natGateway:~# iptables -v -t nat -A POSTROUTING -s 10.0.1.0/24 -j MASQUERADE
    vpcRouter:~# ip route add default via 10.0.0.5 table private
    app:~# curl -sS -m 3 http://198.51.100.10/
  3. A door that opens one way

    app can start conversations with the internet now. Can the internet start one with app?

    Two doors look open. natGateway has a public address, 203.0.113.60, and it forwards packets for app. And app's own address, 10.0.1.20, is right there in the gateway's rules.

    Try both from visitor, a machine out on the internet. Then read the filter rules on natGateway with iptables -S, which lists each rule as the command that would add it.

    visitor:~# curl -sS -m 3 http://203.0.113.60/
    visitor:~# curl -sS -m 3 http://10.0.1.20/
    natGateway:~# iptables -S
  4. A route to the partner's VPC

    Lumen Books' accountants, Tally & Co, run their reports in a VPC of their own, 10.1.4.0/24, and need app's orders every night. The internet is the wrong road: app has no public address, and orders don't belong on the internet anyway.

    The two VPCs are joined by VPC peering, a private link between two VPCs that the provider runs for you. Here it's the network peering, between vpcRouter and partnerRouter, Tally's VPC router.

    A peering link carries nothing until a route points at it, in both VPCs. Tally's table needs a route to 10.0.1.0/24, and Lumen's private table needs a route back to 10.1.4.0/24: the answer starts in the private subnet, so it reads that table.

    Ask app for its orders from reports, Tally's report server, and read partnerRouter's routes. Then add a route on each side and ask again.

    reports:~# curl -sS -m 3 http://10.0.1.20/
    partnerRouter:~# ip route
    partnerRouter:~# ip route add 10.0.1.0/24 via 100.64.251.2
    vpcRouter:~# ip route add 10.1.4.0/24 via 100.64.251.3 table private
    reports:~# curl -sS -m 3 http://10.0.1.20/

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