← Math
Math

Network subnet planning

The capacity sheet says 6,834 of 8,192. Does the cluster fit its /19?

Plan address blocks that cover demand, growth and reservations.

Outcome

Size each subnet as a power of two, check the plan by its blocks rather than its demand, and place a new subnet where it is aligned and free.

Terminology (6)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Subnet
A contiguous range of addresses whose size is a power of two.
Prefix
The number of fixed leading bits, written /n; a /n subnet has 2^(32 − n) addresses.
Reserved addresses
Addresses the provider keeps in every subnet; standard AWS-provided IPv4 subnets reserve5, including network and broadcast addresses; address modes can differ.
Block
The power-of-two size a subnet takes, whatever it uses of it.
Alignment
A subnet starts at a multiple of its own size within its parent.
Parent range
The larger range the network team assigned, which the subnets are carved from.

Why this matters

Subnets are hard to change once a cluster runs in them: moving existing resources into new subnets can require migration. If a pod subnet runs out, new pods cannot start, usually at the busiest moment. Getting the sizes right is a little arithmetic with powers of two, done before the first node joins.

This lesson assumes powers of two and rounding up.

What a subnet allocates

An IPv4 address has 32 bits. A prefix /p fixes p of those bits, leaving 32 − p bits to vary within the subnet, so its block contains 2^(32 − p) addresses. A subnet occupies that whole block even when some addresses are unused.

Demand counts endpoints that need addresses; capacity counts the addresses a subnet can supply after reservations. Growth allowances change demand, while prefix lengths and alignment constrain which blocks can be allocated. The provider’s size and reservation rules are additional constraints.

01Size one subnet

Size one subnet

An online grocery company plans an IPv4 Kubernetes network on AWS, using pod addresses from VPC subnets. The network team assigned a /19. Each of three zones needs subnets for pods, nodes and load-balancer interfaces, with room to grow. The capacity sheet adds up the addresses and says it fits.

Allow 25% growth, round up to a whole address, and add the standard 5 reserved addresses for this AWS-provided IPv4 subnet model. AWS permits IPv4 subnet sizes from /28 (16 addresses) to /16 (65,536), so also apply those provider limits. The examples here already fall within that range; the lab below separately practices generic power-of-two blocks with caller-supplied reservations. For the arithmetic, grown=ceil(n(100+g)/100), needed=grown+reserved and block=2ᵏ for the least k with 2ᵏ≥needed. IPv4 prefix length is 32−k. On whole inputs, ceil division is floor((n(100+g)+99)/100), so no floating tolerance is needed.

One zone’s subnets
TierEndpointsNeededBlock
Pods1,7002,125 + 5 = 2,1304,096 (/20)
Nodes90113 + 5 = 118128 (/25)
Load balancers2025 + 5 = 3032 (/27)

Neither Node.js nor browsers parse CIDR prefixes; the arithmetic above is a few lines of integer code.

02Sum blocks, not demand

Sum blocks, not demand

Predict the two lines this program prints.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
function block(endpoints: number): number {
  const needed = Math.ceil(endpoints * 1.25) + 5; // 25% growth, 5 addresses reserved per subnet
  let size = 1;

  while (size < needed) size *= 2;

  return size;
}

const tiers = [1700, 90, 20]; // pods, nodes, load-balancer interfaces in one zone

console.log(tiers.map(block).join(" "));
console.log(3 * tiers.map(block).reduce((sum, size) => sum + size, 0), 2 ** (32 - 19)); // three zones vs a /19

Three zones need 6,834 addresses, which is what the capacity sheet added. They take 3 × 4,256 = 12,768 addresses of blocks, against the /19’s 8,192. The plan does not fit.

Which range should the team ask the network team for?

03Alignment

Alignment

Within an aligned parent, a subnet starts at a multiple of its own size. The same allocation problem now arises in a separate /22 extension range: 1,024 addresses already hold two subnets. Offsets below count from that parent’s base; they are not final-octet address strings.

A /22 with two existing subnets
OffsetsUse
0–191free (192)
192–255existing /26
256–639free (384)
640–767existing /25
768–1,023free (256)

832 addresses are free. A /23 of 512 can start only at 0 or 512, and each of those ranges contains an existing subnet, so it does not fit. A /24 of 256 can start at 0, 256, 512 or 768; 256 is the first that is clear.

Could the /23 go at offset 256, which starts a free run of 384?

04What is left

What is left

Return to the three-zone plan in a fresh /18. For an empty aligned power-of-two parent, largest-first placement works whenever total blocks fit: every earlier block size is a multiple of each smaller size, so each next offset remains aligned. The 12,768 addresses of blocks placed that way leave 3,616. Placed that way, the leftover splits into aligned subnets by its binary digits: 2,048 + 1,024 + 512 + 32, a /21, a /22, a /23 and a /27. A fourth zone’s pod subnet, a /20, would not fit in any of them.

05What the arithmetic leaves out

What the arithmetic leaves out

06Carve the cluster’s range

Carve the cluster’s range

The cluster request first needs fixed reservations included after rounded growth. The whole plan then needs block totals, not endpoint demand. Once a parent has allocations, total free space is insufficient: the new subnet needs a complete aligned interval, including its far edge, without overlapping any existing block.

A cluster’s address plan

Size one subnet, check the plan, then place a new subnet.

  1. easy · M-SUBNET-V4-1 Size one subnet
  2. medium · M-SUBNET-V4-2 Check the plan
  3. hard · M-SUBNET-V4-3 Place a new subnet
In the frontend A subnet request form in Next.js and Svelte Optional

An internal request form can show the block and prefix as the user types, and say when reservations push beyond the otherwise required block. This AWS-specific form enforces the minimum /28 and maximum /16, validates whole inputs including empty fields, and reports an oversized request rather than producing a negative or unsupported prefix. The generic lab deliberately takes its reservation count from the caller.

Estimate.tsx
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
"use client";

import { useState } from "react";

// AWS-specific form: standard IPv4 reservations and /28.. /16 provider limits.
function describe(endpoints: number | undefined, growth: number | undefined) {
  if (
    !Number.isSafeInteger(endpoints) ||
    !Number.isSafeInteger(growth) ||
    endpoints! < 0 ||
    endpoints! > 65531 ||
    growth! < 0 ||
    growth! > 1000
  )
    return { error: "Enter whole endpoints0..65531 and growth0..1000%." };

  const grown = Math.floor((endpoints! * (100 + growth!) + 99) / 100);
  const needed = grown + 5;

  if (needed > 65536)
    return {
      error: "This demand exceeds one AWS IPv4 /16 subnet; revise the allocation."
    };

  let block = 16,
    withoutReserved = 16,
    prefix = 28;

  while (block < needed) {
    block *= 2;
    prefix--;
  }
  while (withoutReserved < grown) withoutReserved *= 2;

  return { needed, block, prefix, pushed: block > withoutReserved };
}

export function SubnetRequest() {
  const [endpoints, setEndpoints] = useState<number | undefined>(100);
  const [growth, setGrowth] = useState<number | undefined>(25);
  const plan = describe(endpoints, growth);

  return (
    <form>
      <input
        type="number"
        min={0}
        max={65531}
        step={1}
        value={endpoints ?? ""}
        onChange={(e) =>
          setEndpoints(e.target.value === "" ? undefined : Number(e.target.value))
        }
        aria-label="Endpoints"
      />
      <input
        type="number"
        min={0}
        max={1000}
        step={1}
        value={growth ?? ""}
        onChange={(e) =>
          setGrowth(e.target.value === "" ? undefined : Number(e.target.value))
        }
        aria-label="Growth %"
      />
      {"error" in plan ? (
        <p>{plan.error}</p>
      ) : (
        <>
          <p>
            {plan.needed} addresses: a /{plan.prefix} ({plan.block}).
          </p>
          {plan.pushed && (
            <p>The five reserved addresses push this to the next block.</p>
          )}
        </>
      )}
    </form>
  );
}

Check this idea later
A tier needs exactly 128 addresses after growth and reserved ones. Which block?
Why does a capacity sheet that adds needed addresses overstate the room?
Sources

Values above were computed in Node 22 and run in Go 1.27, Rust 1.99 and Apple clang 17. The cluster plan is a worked model.

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.