What is Subnet & CIDR? IP Network Segmentation and Routing
DevOps

What is Subnet & CIDR? IP Network Segmentation and Routing

Subnet and CIDR are the foundation of modern network architecture. This article explains subnet masks, /24 notation, host count calculation, and how AWS VPC and Kubernetes use CIDR for network segmentation.

In this series: DevOps
  1. 1 What is an API Gateway? Single Entry Point for Microservices
  2. 2 What is NAT? Network Address Translation Explained
  3. 3 What is GitLab CI/CD? Automated Pipeline for Build, Test, and Deploy
  4. 4 What is Apache Kafka? Distributed Event Streaming Platform Explained
  5. 5 What is Serverless? FaaS, Cold Start, and When to Go Serverless
  6. 6 What is Subnet & CIDR? IP Network Segmentation and Routing
  7. 7 What is Kubernetes? The Most Popular Container Orchestration Platform Today
  8. 8 What is a Proxy? Forward Proxy, Reverse Proxy and SOCKS5 Explained
  9. 9 What is Nginx? Web server, reverse proxy, and load balancer in one
✦ Quick summary
Subnet and CIDR are the foundation of modern network architecture. This article explains subnet masks, /24 notation, host count calculation, and how AWS VPC and Kubernetes use CIDR for network segment...
How was this post?

Subnets and CIDR are two foundational concepts that every infrastructure engineer or DevOps practitioner must understand — from designing AWS VPCs and configuring Kubernetes, to simply understanding why your office network uses 192.168.1.x. This article explains everything from the basics to real-world practice.

IP Addresses: Basic Structure

An IPv4 address is a 32-bit number, typically written as four octets separated by dots. For example: 192.168.1.100 = 11000000.10101000.00000001.01100100 in binary.

Every IP address is divided into two parts:

  • Network part: Identifies the network (like an area code in a phone number)
  • Host part: Identifies the device within that network (like the subscriber number)

Private IP address ranges (reserved for internal networks, not directly routable on the internet):

Range CIDR Address Count Common Use
10.0.0.0 – 10.255.255.255 10.0.0.0/8 ~16.7 million Large enterprise networks, cloud VPCs
172.16.0.0 – 172.31.255.255 172.16.0.0/12 ~1 million Docker bridge networks
192.168.0.0 – 192.168.255.255 192.168.0.0/16 ~65K Home routers, small office networks

What is a Subnet?

A subnet (subnetwork) is the practice of dividing a large IP network into smaller sub-networks. Each subnet has:

  • Network address: The first address of the subnet (cannot be assigned to a host)
  • Host addresses: Addresses that can be assigned to devices
  • Broadcast address: The last address (cannot be assigned to a host)

Why do we need subnets?

  • Security: Separate different zones (DMZ, internal, database)
  • Performance: Reduce broadcast domains, reduce unnecessary traffic
  • Management: Easier to monitor and filter traffic by subnet

A subnet mask indicates how many bits belong to the network portion. For example, 255.255.255.0:

  • 255 = 11111111 (8 bits of 1 = 8 network bits)
  • All three leading octets are 255 → 24 network bits
  • The last octet is 0 → 8 host bits

CIDR Notation: /prefix

CIDR (Classless Inter-Domain Routing) replaces the old classful A/B/C system by using a prefix length (number of network bits) after a forward slash.

Common CIDR reference table:

CIDR Subnet Mask Total Addresses Usable Hosts Used For
/8 255.0.0.0 16,777,216 16,777,214 Very large networks
/16 255.255.0.0 65,536 65,534 VPCs, campus networks
/24 255.255.255.0 256 254 Most common subnet size
/28 255.255.255.240 16 14 Small subnets
/30 255.255.255.252 4 2 Point-to-point links
/32 255.255.255.255 1 1 Host routes

Formula: Usable hosts = 2^(32 - prefix) - 2

Subnet Calculation in Practice

Example 1: 192.168.1.0/24

Network:    192.168.1.0
Subnet mask: 255.255.255.0
First host: 192.168.1.1
Last host:  192.168.1.254
Broadcast:  192.168.1.255
Hosts:      254

Example 2: 10.0.0.0/28 (small subnet for DMZ)

Network:    10.0.0.0
Subnet mask: 255.255.255.240
First host: 10.0.0.1
Last host:  10.0.0.14
Broadcast:  10.0.0.15
Hosts:      14

Tool: ipcalc

Bash
 1# Install ipcalc
 2apt-get install ipcalc  # Ubuntu/Debian
 3brew install ipcalc      # macOS
 4
 5# Calculate subnet details
 6ipcalc 192.168.1.0/24
 7# Output:
 8# Address:   192.168.1.0          11000000.10101000.00000001. 00000000
 9# Netmask:   255.255.255.0 = 24   11111111.11111111.11111111. 00000000
10# Network:   192.168.1.0/24       11000000.10101000.00000001. 00000000
11# HostMin:   192.168.1.1          11000000.10101000.00000001. 00000001
12# HostMax:   192.168.1.254        11000000.10101000.00000001. 11111110
13# Broadcast: 192.168.1.255        11000000.10101000.00000001. 11111111
14# Hosts/Net: 254                   Class C, Private Internet
15
16# View IP addresses of all interfaces
17ip addr show
18# or for a specific interface
19ip addr show eth0
20
21# Check the routing table
22ip route show

AWS VPC and Subnet Design

AWS VPC (Virtual Private Cloud) uses CIDR to segment cloud networks. A standard architecture looks like this:

VPC: 10.0.0.0/16 (65,536 addresses)
├── Public Subnet (AZ-a):   10.0.1.0/24   (254 hosts)
├── Public Subnet (AZ-b):   10.0.2.0/24   (254 hosts)
├── Private Subnet (AZ-a):  10.0.10.0/24  (254 hosts)
├── Private Subnet (AZ-b):  10.0.20.0/24  (254 hosts)
└── Database Subnet (AZ-a): 10.0.100.0/24 (254 hosts)

AWS reserves 5 addresses in every subnet:

  • .0 — Network address
  • .1 — AWS VPC router
  • .2 — AWS DNS server
  • .3 — Reserved for future use
  • .255 — Broadcast (not supported by AWS)

→ A /24 subnet in AWS actually gives you only 251 usable addresses (256 - 5)

Terraform example:

hcl
 1resource "aws_vpc" "main" {
 2  cidr_block = "10.0.0.0/16"
 3}
 4
 5resource "aws_subnet" "public_a" {
 6  vpc_id            = aws_vpc.main.id
 7  cidr_block        = "10.0.1.0/24"
 8  availability_zone = "ap-southeast-1a"
 9}
10
11resource "aws_subnet" "private_a" {
12  vpc_id            = aws_vpc.main.id
13  cidr_block        = "10.0.10.0/24"
14  availability_zone = "ap-southeast-1a"
15}

Kubernetes Pod CIDR

Kubernetes uses a dedicated CIDR range for its pod network, completely separate from node IP addresses:

Bash
1# View pod CIDR for the cluster
2kubectl get nodes -o wide
3# INTERNAL-IP column shows node IPs; POD-CIDR column shows each node's pod CIDR
4
5# Check pod CIDR in kubeadm config
6kubectl get configmap kubeadm-config -n kube-system -o yaml | grep podSubnet
7
8# View pod IPs across all namespaces
9kubectl get pods -A -o wide

Default CNI plugins and their CIDRs:

  • Flannel: 10.244.0.0/16 — each node receives a /24 (up to 256 pods per node)
  • Calico: 192.168.0.0/16 — flexible, supports BGP routing
  • Cilium: 10.0.0.0/8 — uses eBPF, flexible block size

VLSM: Variable Length Subnet Masking

VLSM allows you to use subnets of different sizes within the same network:

Parent network: 10.0.0.0/8

HCM Office (500 employees): 10.1.0.0/23  → 510 hosts
HN Office  (200 employees): 10.2.0.0/24  → 254 hosts
DN Office  (50 employees):  10.3.0.0/26  → 62 hosts
Router-to-Router WAN link:  10.4.0.0/30  → 2 hosts
Loopback interface:          10.5.0.1/32  → 1 host

Without VLSM, every subnet would need to be large enough for the biggest office — wasting hundreds of IP addresses on segments that only need a handful.

What is NAT? Network Address Translation Explained

What is VPN? Virtual Private Network Explained

What is Kubernetes? Container Orchestration Explained

Frequently Asked QuestionsQ&A
What does CIDR /24 mean?
CIDR /24 means the first 24 bits of the IP address are the network prefix, and the remaining 8 bits identify the host. With /24, you have 2^8 = 256 total addresses. Subtract 1 for the network address and 1 for the broadcast address, leaving 254 usable host addresses. For example: 192.168.1.0/24 can use 192.168.1.1 through 192.168.1.254.
Are subnet mask 255.255.255.0 and /24 the same thing?
Yes, they are two representations of the same concept. 255.255.255.0 written in dotted-decimal: 255 = 11111111 (8 bits of 1), three octets of 255 = 24 bits of 1 → /24. CIDR (Classless Inter-Domain Routing) is a more concise and flexible notation because it can represent any prefix length without being restricted to classful A/B/C boundaries.
What is a broadcast address?
The broadcast address is a special address at the end of each subnet, used to send a packet to ALL hosts within that subnet. Routers and switches do not forward broadcasts outside the subnet. In 192.168.1.0/24: broadcast = 192.168.1.255. In 10.0.0.0/28: broadcast = 10.0.0.15. This is why each subnet loses 2 addresses (network + broadcast) that cannot be assigned to hosts.
Why does AWS VPC typically use /16 for the VPC and /24 for subnets?
/16 provides 65,536 addresses — enough space for an entire large organization's cloud infrastructure with room to grow. /24 provides 251 usable addresses per subnet (AWS reserves 5 addresses) — sufficient for one workload tier (web tier, app tier, database tier). Using /24 for the entire VPC would exhaust address space as soon as you have multiple subnets.
What is a Kubernetes pod CIDR?
Pod CIDR is a dedicated IP range allocated to pods inside a Kubernetes cluster, completely separate from node IPs. Each node is assigned a smaller /24 block from the larger pod CIDR. For example: Flannel uses 10.244.0.0/16 (supports up to 256 nodes each with a /24), Calico uses 192.168.0.0/16 by default. The CNI plugin (Flannel, Calico, Cilium) manages this allocation.
What is VLSM and when should you use it?
VLSM (Variable Length Subnet Masking) is a technique that uses subnet masks of different lengths within the same network — instead of requiring all subnets to be the same size. For example: a large office uses /22 (1022 hosts), a small department uses /26 (62 hosts), a point-to-point router link uses /30 (2 hosts). VLSM saves IP address space and accurately reflects the actual needs of each network segment.

Subnet và CIDR là hai khái niệm nền tảng mà bất kỳ kỹ sư hạ tầng hay DevOps nào cũng cần nắm vững — từ thiết kế AWS VPC, cấu hình Kubernetes, đến đơn giản là hiểu tại sao mạng văn phòng của bạn lại dùng 192.168.1.x. Bài viết giải thích từ cơ bản đến thực tiễn.

Địa chỉ IP: Cấu trúc cơ bản

Địa chỉ IPv4 là một số 32-bit, thường viết dưới dạng 4 octet phân tách bởi dấu chấm. Ví dụ: 192.168.1.100 = 11000000.10101000.00000001.01100100 trong hệ nhị phân.

Mỗi địa chỉ IP chia thành hai phần:

  • Network part: Xác định mạng (giống như mã vùng điện thoại)
  • Host part: Xác định thiết bị trong mạng đó (giống như số thuê bao)

Dải địa chỉ Private IP (dành riêng cho mạng nội bộ, không route trực tiếp trên internet):

Dải CIDR Số địa chỉ Thường dùng
10.0.0.0 – 10.255.255.255 10.0.0.0/8 ~16.7 triệu Mạng doanh nghiệp lớn, cloud VPC
172.16.0.0 – 172.31.255.255 172.16.0.0/12 ~1 triệu Docker bridge network
192.168.0.0 – 192.168.255.255 192.168.0.0/16 ~65K Home router, mạng văn phòng nhỏ

Subnet là gì?

Subnet (subnetwork) là việc chia một mạng IP lớn thành các mạng con nhỏ hơn. Mỗi subnet có:

  • Network address: Địa chỉ đầu tiên của subnet (không gán cho host)
  • Host addresses: Các địa chỉ có thể gán cho thiết bị
  • Broadcast address: Địa chỉ cuối cùng (không gán cho host)

Tại sao cần subnet?

  • Bảo mật: Phân tách các zone khác nhau (DMZ, internal, database)
  • Hiệu năng: Giảm broadcast domain, giảm traffic không cần thiết
  • Quản lý: Dễ theo dõi, filter traffic theo subnet

Subnet mask cho biết bao nhiêu bit là phần network. Ví dụ 255.255.255.0:

  • 255 = 11111111 (8 bit 1 = 8 bit network)
  • Ba octet đầu đều là 255 → 24 bit network
  • Octet cuối là 0 → 8 bit host

CIDR ký hiệu: /prefix

CIDR (Classless Inter-Domain Routing) thay thế hệ thống Class A/B/C cổ điển bằng cách dùng prefix length (số bit network) sau dấu gạch chéo.

Bảng CIDR thông dụng:

CIDR Subnet Mask Số địa chỉ Host usable Dùng cho
/8 255.0.0.0 16,777,216 16,777,214 Mạng cực lớn
/16 255.255.0.0 65,536 65,534 VPC, mạng campus
/24 255.255.255.0 256 254 Subnet phổ biến nhất
/28 255.255.255.240 16 14 Subnet nhỏ
/30 255.255.255.252 4 2 Point-to-point link
/32 255.255.255.255 1 1 Host route

Công thức: Host usable = 2^(32 - prefix) - 2

/30 cho WAN link, /31 cho point-to-point hiện đại

Link nối trực tiếp giữa hai router truyền thống dùng /30 (4 địa chỉ, 2 usable). RFC 3021 cho phép dùng /31 — chỉ 2 địa chỉ, không có network/broadcast — tiết kiệm thêm 50% không gian. Nhiều vendor (Cisco IOS 12.2+, Juniper, AWS Transit Gateway) hỗ trợ /31 cho point-to-point. Với /30 thì 2 địa chỉ bị lãng phí; nhân với hàng trăm WAN link trong mạng lớn, tổng lãng phí là đáng kể.

Tính toán Subnet thực hành

Ví dụ 1: 192.168.1.0/24

Network:    192.168.1.0
Subnet mask: 255.255.255.0
First host: 192.168.1.1
Last host:  192.168.1.254
Broadcast:  192.168.1.255
Hosts:      254

Ví dụ 2: 10.0.0.0/28 (subnet nhỏ cho DMZ)

Network:    10.0.0.0
Subnet mask: 255.255.255.240
First host: 10.0.0.1
Last host:  10.0.0.14
Broadcast:  10.0.0.15
Hosts:      14

Công cụ: ipcalc

Bash
 1# Cài ipcalc
 2apt-get install ipcalc  # Ubuntu/Debian
 3brew install ipcalc      # macOS
 4
 5# Tính toán subnet
 6ipcalc 192.168.1.0/24
 7# Output:
 8# Address:   192.168.1.0          11000000.10101000.00000001. 00000000
 9# Netmask:   255.255.255.0 = 24   11111111.11111111.11111111. 00000000
10# Network:   192.168.1.0/24       11000000.10101000.00000001. 00000000
11# HostMin:   192.168.1.1          11000000.10101000.00000001. 00000001
12# HostMax:   192.168.1.254        11000000.10101000.00000001. 11111110
13# Broadcast: 192.168.1.255        11000000.10101000.00000001. 11111111
14# Hosts/Net: 254                   Class C, Private Internet
15
16# Xem địa chỉ IP của các interface
17ip addr show
18# hoặc
19ip addr show eth0
20
21# Kiểm tra route table
22ip route show

AWS VPC và Subnet Design

AWS VPC (Virtual Private Cloud) dùng CIDR để phân vùng mạng cloud. Kiến trúc chuẩn:

VPC: 10.0.0.0/16 (65,536 địa chỉ)
├── Public Subnet (AZ-a):  10.0.1.0/24  (254 host)
├── Public Subnet (AZ-b):  10.0.2.0/24  (254 host)
├── Private Subnet (AZ-a): 10.0.10.0/24 (254 host)
├── Private Subnet (AZ-b): 10.0.20.0/24 (254 host)
└── Database Subnet (AZ-a): 10.0.100.0/24 (254 host)

AWS dự trữ 5 địa chỉ trong mỗi subnet:

  • .0 — Network address
  • .1 — AWS VPC router
  • .2 — AWS DNS server
  • .3 — Dự phòng tương lai
  • .255 — Broadcast (AWS không hỗ trợ)

→ Subnet /24 thực tế chỉ có 251 địa chỉ usable (256 - 5)

AWS dự trữ 5 địa chỉ — đừng tính theo công thức 2^n - 2 thuần túy

Khác với mạng vật lý thông thường (chỉ mất 2 địa chỉ: network + broadcast), AWS VPC mất thêm 3 địa chỉ (.1 router, .2 DNS, .3 dự phòng). Hệ quả: nếu bạn cần đúng 256 EC2 instance, một subnet /24 là không đủ — cần /23 (510 usable sau khi trừ 5). Lỗi này thường gặp khi ước tính capacity cho EKS node group hoặc RDS subnet group.

Terraform:

hcl
 1resource "aws_vpc" "main" {
 2  cidr_block = "10.0.0.0/16"
 3}
 4
 5resource "aws_subnet" "public_a" {
 6  vpc_id            = aws_vpc.main.id
 7  cidr_block        = "10.0.1.0/24"
 8  availability_zone = "ap-southeast-1a"
 9}
10
11resource "aws_subnet" "private_a" {
12  vpc_id            = aws_vpc.main.id
13  cidr_block        = "10.0.10.0/24"
14  availability_zone = "ap-southeast-1a"
15}

Kubernetes Pod CIDR

Kubernetes dùng CIDR riêng cho pod network, tách biệt với IP của node:

Kubernetes là gì? Container orchestration và Pod networking

Bash
1# Xem pod CIDR của cluster
2kubectl get nodes -o wide
3# Cột INTERNAL-IP là IP của node, cột POD-CIDR là CIDR của pod trên node đó
4
5# Kiểm tra pod CIDR trong kubeadm config
6kubectl get configmap kubeadm-config -n kube-system -o yaml | grep podSubnet
7
8# Xem pod IPs
9kubectl get pods -A -o wide

CNI Plugin mặc định và CIDR:

  • Flannel: 10.244.0.0/16 — mỗi node nhận /24 (256 pod per node)
  • Calico: 192.168.0.0/16 — flexible, hỗ trợ BGP routing
  • Cilium: 10.0.0.0/8 — dùng eBPF, flexible block size

VLSM: Variable Length Subnet Masking

VLSM cho phép dùng subnet kích thước khác nhau trong cùng một mạng:

Mạng gốc: 10.0.0.0/8

Văn phòng HCM (500 nhân viên): 10.1.0.0/23  → 510 host
Văn phòng HN  (200 nhân viên): 10.2.0.0/24  → 254 host
Văn phòng DN  (50 nhân viên):  10.3.0.0/26  → 62 host
Router-to-Router WAN link:     10.4.0.0/30  → 2 host
Loopback interface:             10.5.0.1/32  → 1 host

Không dùng VLSM: mỗi subnet phải đủ cho văn phòng lớn nhất → lãng phí hàng trăm địa chỉ IP.

CIDR block trong VLSM phải căn theo lũy thừa của 2

Khi chia VLSM, địa chỉ đầu của mỗi subnet phải là bội số của kích thước subnet đó. Ví dụ: subnet /26 (64 địa chỉ) chỉ được bắt đầu ở .0, .64, .128, .192. Nếu bạn đặt /26 bắt đầu ở .100, subnet đó invalid và router sẽ từ chối. Công cụ như ipcalc hoặc tính tay theo quy tắc "block size = 2^(32 - prefix)" giúp tránh lỗi này khi thiết kế địa chỉ cho nhiều site.

VPN là gì? Mạng riêng ảo, WireGuard và OpenVPN

NAT là gì? Network Address Translation trong mạng máy tính

Câu hỏi thường gặpQ&A
CIDR /24 nghĩa là gì?
CIDR /24 có nghĩa là 24 bit đầu tiên của địa chỉ IP là phần network (network prefix), còn 8 bit cuối là phần host. Với /24, bạn có 2^8 = 256 địa chỉ tổng, trừ đi 1 network address và 1 broadcast address còn lại 254 địa chỉ có thể gán cho host. Ví dụ: 192.168.1.0/24 có thể dùng 192.168.1.1 đến 192.168.1.254.
Subnet mask 255.255.255.0 và /24 có giống nhau không?
Có, đây là hai cách biểu diễn cùng một thứ. 255.255.255.0 viết theo dạng dotted-decimal: 255 = 11111111 (8 bit 1), ba octet đầu = 24 bit 1 → /24. CIDR (Classless Inter-Domain Routing) là ký hiệu ngắn gọn hơn và linh hoạt hơn vì có thể biểu diễn bất kỳ độ dài prefix nào, không cần chia theo class A/B/C.
Địa chỉ broadcast là gì?
Broadcast address là địa chỉ đặc biệt ở cuối mỗi subnet, dùng để gửi packet đến TẤT CẢ các host trong subnet đó. Router và switch không forward broadcast ra ngoài subnet. Trong 192.168.1.0/24: broadcast = 192.168.1.255. Trong 10.0.0.0/28: broadcast = 10.0.0.15. Đây là lý do mỗi subnet mất 2 địa chỉ (network + broadcast) không dùng được cho host.
Tại sao AWS VPC thường dùng /16 cho VPC và /24 cho subnet?
/16 cấp 65,536 địa chỉ — đủ không gian cho toàn bộ hạ tầng cloud của một tổ chức lớn, và còn dự phòng để mở rộng. /24 cấp 251 địa chỉ usable per subnet (AWS dự trữ 5 địa chỉ đầu/cuối) — vừa đủ cho một workload tier (web tier, app tier, database tier). Nếu dùng /24 cho cả VPC thì hết chỗ ngay khi có nhiều subnet.
Kubernetes pod CIDR là gì?
Pod CIDR là dải IP riêng được cấp phát cho các pod trong Kubernetes cluster, tách biệt hoàn toàn với IP của node. Mỗi node được cấp một /24 nhỏ hơn từ pod CIDR lớn. Ví dụ: Flannel dùng 10.244.0.0/16 (có thể có 256 node /24), Calico dùng 192.168.0.0/16 theo mặc định. CNI plugin (Flannel, Calico, Cilium) quản lý việc phân bổ này.
VLSM là gì và khi nào dùng?
VLSM (Variable Length Subnet Masking) là kỹ thuật dùng nhiều subnet mask có độ dài khác nhau trong cùng một mạng — thay vì tất cả subnet phải cùng kích thước. Ví dụ: văn phòng lớn dùng /22 (1022 host), phòng nhỏ dùng /26 (62 host), link point-to-point giữa router dùng /30 (2 host). VLSM giúp tiết kiệm địa chỉ IP và phản ánh đúng nhu cầu thực tế của từng phân đoạn mạng.