Skip to content
devops.academy

Module 4: Networking — The Complete Guide

Goal

Networking is the invisible layer every DevOps problem eventually touches: "why can't service A reach service B?" This module gives you the mental models (IP, subnets, routing, NAT, DNS) and hands-on tools to design and debug networks — locally and in AWS.


1. The Big Picture — How a Request Travels

When a user opens https://myapp.com, this happens:

text
Browser → DNS lookup (myapp.com → 52.14.9.100)
        → TCP connection to 52.14.9.100:443
        → TLS handshake (encryption)
        → HTTP request
        → Load balancer → your server (private IP 10.0.2.15) → app on port 8080

Every arrow above is a place things can break, and each has its own debugging tool. By the end of this module you'll know them all.

Analogy

Loading a web page is like mailing a letter across the country. You look up the recipient's address in the phone book (DNS), the postal service carries the envelope through a chain of sorting offices (routing), and a clerk at the destination building walks it up to the right office suite (the port). When a letter goes missing, you check each leg of the journey in turn — which is exactly what network debugging is.

Layers you actually need (simplified OSI)

LayerWhatDevOps examples
L7 ApplicationHTTP, DNS, SSHnginx routing, curl
L4 TransportTCP/UDP, portsload balancers, ss, firewalls
L3 NetworkIP addresses, routingsubnets, route tables, NAT
L2 LinkMAC addresses, switchesmostly invisible in cloud

When someone says "L4 load balancer vs L7 load balancer" (Module 8), this table is what they mean: L4 forwards TCP connections blindly; L7 understands HTTP and can route by URL path.


2. IP Addresses

An IPv4 address is 4 bytes: 192.168.1.10. Two categories you must distinguish instantly:

Public IPs — routable on the internet, globally unique (e.g., 52.14.9.100). Private IPs — only valid inside a private network. Reserved ranges (memorize!):

text
10.0.0.0    – 10.255.255.255   (10.0.0.0/8)      ← most cloud VPCs
172.16.0.0  – 172.31.255.255   (172.16.0.0/12)   ← Docker defaults live here
192.168.0.0 – 192.168.255.255  (192.168.0.0/16)  ← home networks

Special addresses: 127.0.0.1 (localhost — the machine talking to itself), 0.0.0.0 ("all interfaces" — when an app binds to 0.0.0.0:8080 it accepts connections from anywhere; binding to 127.0.0.1:8080 means local-only. Classic bug: app works via SSH curl localhost:8080 but is unreachable externally — it bound to 127.0.0.1).

Ports identify the program on a machine: IP:port = full address. Well-known: 22 SSH, 80 HTTP, 443 HTTPS, 5432 Postgres, 3306 MySQL, 6379 Redis.

Analogy

An IP address is a building's street address, and the port is the apartment number: the mail truck only needs the street address to reach the building, but the apartment number decides whose door gets the knock. Public IPs are real street addresses anyone in the world can post to; private IPs are internal room numbers — Room 204 means something inside your own building but is useless on an envelope mailed across town. Binding to 127.0.0.1 is like accepting only notes slipped under your own door from inside the apartment.


3. CIDR & Subnetting (Interview Favorite — Learn This Cold)

CIDR notation 10.0.0.0/16 means: the first 16 bits are the network part, the rest is for hosts.

text
/8   → 16,777,216 addresses      (10.0.0.0/8)
/16  → 65,536 addresses          (10.0.0.0 – 10.0.255.255)
/24  → 256 addresses             (10.0.1.0 – 10.0.1.255)
/28  → 16 addresses
/32  → exactly 1 address (a single host — used in firewall rules)

Rule: addresses = 2^(32 − prefix). Each subnet loses a few (network address, broadcast; AWS reserves 5 per subnet).

Analogy

A CIDR block is a postcode district: the prefix pins down the district, and the remaining digits distinguish individual houses inside it. A short prefix like /8 is an entire region with millions of homes, a /24 is one neighborhood street of 256 doors, and a /32 is a single specific front door. Subnetting is the postal service drawing smaller district boundaries inside a bigger one so each carrier has a tidy patch to cover.

Subnetting = slicing a network

You get a VPC of 10.0.0.0/16 and slice it into /24 subnets:

text
10.0.1.0/24   → public subnet, AZ-a   (load balancers, NAT)
10.0.2.0/24   → public subnet, AZ-b
10.0.11.0/24  → private subnet, AZ-a  (app servers)
10.0.12.0/24  → private subnet, AZ-b
10.0.21.0/24  → private subnet, AZ-a  (databases)

Practice reading: is 10.0.1.57 inside 10.0.1.0/24? Yes — first 24 bits (10.0.1) match. Is it inside 10.0.0.0/16? Also yes. Is 10.0.2.5 in 10.0.1.0/24? No.

Why subnet at all? Isolation (databases in a subnet with no internet path), blast-radius control, per-subnet routing rules, and high availability (one subnet per availability zone).


4. Routing — How Packets Find Their Way

Every machine has a route table: "to reach network X, send packets to Y."

bash
ip route
# default via 192.168.1.1 dev eth0        ← everything else goes to the router (gateway)
# 192.168.1.0/24 dev eth0 ...             ← local network: deliver directly

Longest-prefix match wins: a packet to 10.0.2.5 prefers a /24 route over the default route. A router is just a device with multiple networks attached that forwards packets between them using its own route table. In AWS you don't see router boxes — you edit route tables attached to subnets, and that is the router.

Analogy

A route table is the wall chart in a sorting office: mail addressed to the local neighborhood goes straight into the carrier's bag, and everything else goes onto the truck bound for the regional hub — that truck is your default gateway. Longest-prefix match is the clerk always following the most specific instruction on the chart: a line naming one particular street beats the general everything-else bin.


5. NAT & Internet Gateways

Private IPs can't be reached from the internet — so how does a private server download packages?

NAT (Network Address Translation): a NAT device rewrites outgoing packets' source address from the private IP to its own public IP, remembers the connection, and forwards replies back. Your home router does this; in AWS it's a NAT Gateway.

Analogy

NAT is the company mailroom: every outgoing letter gets restamped with the building's one public street address, and the mailroom keeps a ledger of which desk sent what so replies land back on the right desk. Outsiders only ever see the mailroom's address — there is no way for a stranger to address a letter directly to your cubicle.

Key asymmetry to internalize:

  • Internet Gateway (IGW): two-way door. Resources with public IPs in public subnets can be reached from the internet and reach out.
  • NAT Gateway: one-way door. Private resources can initiate outbound connections (apt update, calling APIs), but nothing outside can initiate a connection in.
Internet
Internet Gateway
two-way door
Public subnet 10.0.1.0/24

route: 0.0.0.0/0 → IGW
contains: Load balancer (public IP) and the NAT Gateway

NAT Gateway
one-way door — outbound only
Private subnet 10.0.11.0/24

route: 0.0.0.0/0 → NAT GW
contains: App servers (private IPs only)

This diagram is the standard cloud network design. Databases and app servers hide in private subnets; only the load balancer faces the internet. Attackers can't even attempt SSH against your app servers.

Analogy

An Internet Gateway is the building's front door: it swings both ways, so visitors can walk in and staff can walk out. A NAT Gateway is a one-way mail slot: staff can push letters out and receive the replies to those letters, but no stranger can reach in and start a conversation.


6. DNS — Names to Addresses

DNS translates myapp.com → 52.14.9.100. Resolution order on Linux: /etc/hosts first, then the resolvers in /etc/resolv.conf.

Analogy

DNS is the phone book: you know the name, the book gives you the number to dial. The hosts file is the sticky note taped next to your own phone — you glance at it before ever opening the book, so scribbling a different number on it changes who you call without changing the book anyone else reads.

bash
# /etc/hosts — local override, great for testing
127.0.0.1   localhost
10.0.11.5   internal-api.local

Real trick: test a new server before switching DNS by adding NEW_IP myapp.com to your /etc/hosts — your browser hits the new server while the world still sees the old one.

Record types you'll manage:

TypeMapsExample
Aname → IPv4myapp.com → 52.14.9.100
CNAMEname → another namewww → myapp.com
MXmail servers—
TXTverification, SPF—

TTL = how long resolvers cache a record. Ops move: before a migration, lower TTL to 60s a day ahead, switch the record, raise TTL after. Otherwise users keep hitting the old IP for hours.

Analogy

TTL is how long people are allowed to trust a photocopied phone-book page instead of looking the number up fresh. If you know you are about to change your number, you announce a day ahead that photocopies expire after one minute — otherwise callers keep dialing the old number for hours after you have moved.

Debugging DNS:

bash
dig myapp.com               # full answer + which server responded
dig +short myapp.com        # just the IP
nslookup myapp.com          # classic alternative

7. Firewalls: iptables & Friends

iptables is the Linux kernel firewall. Even in the cloud (where you'll mostly use security groups), Docker and Kubernetes manipulate iptables under the hood — so you must be able to read it.

bash
sudo iptables -L -n -v                 # list rules with packet counts
# Allow SSH and HTTP, drop everything else inbound:
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -j DROP

Chains: INPUT (to this machine), OUTPUT (from it), FORWARD (through it — NAT/routers). Rules match top-down; first match wins.

Analogy

The firewall is the doorman at the building entrance with a checklist: he reads it top to bottom, and the first line that matches a visitor decides whether they are waved in or turned away. The classic blunder is putting turn everyone away above the line that admits your own courier — you have just locked yourself out of your own building.

Classic self-own: adding a DROP rule that cuts off your own SSH session. Always allow ESTABLISHED first, and on real servers test with iptables rules that auto-expire, or use ufw (simple front-end: ufw allow 22/tcp; ufw enable).

In AWS you'll express the same intent as Security Groups (stateful, instance-level) and NACLs (stateless, subnet-level) — Module 8.


8. The Debugging Toolbox

bash
ping -c3 10.0.11.5              # L3 reachability (note: firewalls may block ICMP)
ss -tulpn                       # what's listening locally, which PID owns each port
nc -zv 10.0.11.5 5432           # netcat: "can I open a TCP connection to that port?"
nc -l 9000                      # listen on a port (test if traffic reaches you at all)
traceroute myapp.com            # path packets take; where they die
dig +short myapp.com            # DNS
curl -v https://myapp.com       # full HTTP conversation incl. TLS handshake
curl -s -o /dev/null -w '%{http_code} %{time_total}s\n' https://myapp.com   # status + latency
tcpdump -i eth0 port 5432 -nn   # watch raw packets (last resort, ultimate truth)

The universal "service A can't reach service B" playbook

Memorize this sequence — it solves 90% of connectivity tickets:

text
1. On B:  ss -tulpn | grep 5432        → Is anything listening? On 0.0.0.0 or 127.0.0.1?
2. On B:  curl/nc localhost 5432       → Does it work locally? (app problem vs network problem)
3. On A:  dig db.internal              → Does the name resolve, and to the IP you expect?
4. On A:  nc -zv <B-ip> 5432           → TCP reachable? If not: firewall/security group/route.
5. Check security groups / iptables / NACLs / route tables between them.
6. tcpdump on B while testing from A   → do packets even arrive?

Each step halves the search space. Seniors don't guess; they bisect.

Analogy

Debugging connectivity is tracing a lost parcel: first confirm the recipient actually has a mailbox and is checking it, then confirm the sender wrote the correct address, then walk the delivery route one sorting office at a time to find where the parcel was last seen. Running tcpdump is asking the loading dock directly: did the truck ever even arrive?


9. Linux Network Namespaces — The Secret Behind Containers

A network namespace is an isolated copy of the network stack: its own interfaces, IPs, routes, firewall rules. This is literally how Docker gives each container its own IP. Understanding this demystifies all of container networking.

Analogy

A network namespace gives each tenant in an office building a completely private mailroom: their own address plaque, their own sorting rules, their own doorman — even though it is all one building. A veth pair is a pneumatic tube running between two of those mailrooms, and a bridge is the shared mail chute in the lobby that connects many tubes together.

Hands-on — build a mini "container network" by hand:

bash
# Create two isolated namespaces
sudo ip netns add red
sudo ip netns add blue

# Create a virtual cable (veth pair) and put one end in each
sudo ip link add veth-red type veth peer name veth-blue
sudo ip link set veth-red  netns red
sudo ip link set veth-blue netns blue

# Give each end an IP and bring it up
sudo ip netns exec red  ip addr add 10.10.0.1/24 dev veth-red
sudo ip netns exec blue ip addr add 10.10.0.2/24 dev veth-blue
sudo ip netns exec red  ip link set veth-red up
sudo ip netns exec blue ip link set veth-blue up

# They can now talk — two isolated "machines" on one host!
sudo ip netns exec red ping -c2 10.10.0.2

Scale this idea up: many namespaces plugged into a virtual bridge (a software switch) + NAT rules for outbound internet = exactly Docker's default bridge network. When you run docker run -p 8080:80, Docker adds an iptables NAT rule mapping host port 8080 → container namespace port 80. Nothing magical.


10. Designing a Network in AWS (Applied)

Putting it all together — the design you'll build in this module's project and reuse for the rest of your career:

VPC 10.0.0.0/16
Public subnet 10.0.1.0/24 (AZ-a)Route: local + 0.0.0.0/0 → IGW
ALB (load balancer)
NAT Gateway
Bastion host (SSH jump box)
Public subnet 10.0.2.0/24 (AZ-b)(same, for high availability)
Private subnet 10.0.11.0/24 (AZ-a)Route: local + 0.0.0.0/0 → NAT
App serversSG: allow 8080 from ALB's SG only
Private subnet 10.0.12.0/24 (AZ-b)
Private subnet 10.0.21.0/24 (AZ-a)Route: local only (no internet at all)
RDS databaseSG: allow 5432 from App SG only

Best practices encoded above:

  • Two AZs for everything user-facing → survive a datacenter failure.
  • Security groups reference other security groups, not IPs ("DB accepts 5432 from anything wearing the App SG").
  • Databases: no internet route whatsoever.
  • Humans enter via a bastion (or better, AWS SSM Session Manager) — never direct SSH to app servers.
  • Leave room in your CIDR plan for growth; renumbering later is painful.

Analogy

This layout is a corporate campus: the reception lobby (public subnet) is the only part visitors ever see, the offices sit behind badge-locked doors, and the records vault (the database) has no exterior door at all. Even employees do not carry a vault key — only the office that needs the records is on the vault's access list.


11. Hands-On Project (Do This)

  1. Subnet drills: for 10.0.0.0/16, work out on paper: how many /24s fit? What's the range of 10.0.5.0/24? Is 10.0.5.130 in 10.0.5.128/26?
  2. Namespaces: complete the red/blue namespace lab above; then add a bridge and a third namespace so all three communicate.
  3. Port debugging: run python3 -m http.server 8000 bound to 127.0.0.1 vs 0.0.0.0; observe the difference with ss -tulpn and from another machine with nc -zv.
  4. AWS (free tier): build the VPC diagram above — VPC, 2 public + 2 private subnets, IGW, NAT, route tables. Launch an instance in a private subnet, prove it can apt update (via NAT) but is unreachable from the internet. SSH to it through a bastion.
  5. Break & fix: remove the NAT route and watch apt update hang; add it back. Delete an SG rule and diagnose using the playbook in §8.

12. Self-Check Questions

  • How many usable IPs in a /26? What does /32 mean in a firewall rule?
  • Difference between an Internet Gateway and a NAT Gateway — who can initiate connections?
  • An app binds to 127.0.0.1:8080. What symptom does this cause and how do you spot it with ss?
  • Walk through the six-step playbook for "A can't reach B on port 5432."
  • Why lower DNS TTL before a migration?
  • What is a veth pair and what does it correspond to in Docker?

Next → Module 5: Docker