Lesson 103 · Section 9: Networking
Lesson 103: Switching, routing and gateways: the Linux networking a cluster stands on
How a Linux machine decides where to send a packet: interfaces and addresses, switches inside one network, routers and gateways between networks, the routing table, the default route, and the ip_forward setting every Kubernetes node needs. · 14 min read · 3 min video
Official competencies this lesson covers
Narrated by the local narrator voice. Keys: Space plays and pauses, J and L step, C captions, F full screen.
Why a Kubernetes course starts with switches
A cluster's network is not a new invention. A network plugin gives a Pod an address with the same ip addr
mechanism you would use by hand, and it sends traffic to another node's Pods with an ordinary route. When a Pod on
one node cannot reach a Pod on another, the answer is very often in the output of ip route on a node.
So this lesson has no Kubernetes objects in it. It teaches the three or four Linux ideas that everything else in this section reuses, and the handful of commands you need to inspect them. If you have managed Linux servers before, treat it as a refresher and pay attention to the last part, where these ideas meet Kubernetes.
Interfaces and addresses
A machine connects to a network through an interface: a physical card, or a virtual one. ip link lists them.
ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
link/ether 08:00:27:4a:9c:11 brd ff:ff:ff:ff:ff:ff
lo is the loopback interface, the machine talking to itself on 127.0.0.1. eth0 is the real one. state UP
means it is switched on, and link/ether is its hardware (MAC) address.
An interface needs an IP address before it is useful. The address comes with a prefix length that says how big its network is:
| Written as | Network | Addresses in it | Meaning |
|---|---|---|---|
192.168.1.10/24 | 192.168.1.0/24 | 192.168.1.0 to 192.168.1.255 | The first 24 bits (three numbers) name the network |
10.244.0.0/16 | 10.244.0.0/16 | 10.244.0.0 to 10.244.255.255 | The first two numbers name the network |
10.96.0.0/12 | 10.96.0.0/12 | 10.96.0.0 to 10.111.255.255 | The default Service range of kubeadm |
192.168.1.10/32 | one address | only itself | A single host, with no neighbours |
The prefix matters more than it looks. It is how the machine knows which addresses are its neighbours (reach them directly) and which are not (use a route).
Switching: one network
Parts:
- Host A (192.168.1.10/24)
- Switch (192.168.1.0/24)
- Host B (192.168.1.11/24)
Connections:
- Host A and Switch: eth0
- Switch and Host B: eth0
A switch connects machines that are in the same network. It forwards frames by hardware address and has no idea
about IP routes. To put two hosts in the network 192.168.1.0/24:
# on host A
ip addr add 192.168.1.10/24 dev eth0
ip link set dev eth0 up
# on host B
ip addr add 192.168.1.11/24 dev eth0
ip link set dev eth0 up
Adding the address also adds a route for the whole network, automatically:
ip route
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10
Read it as "everything in 192.168.1.0/24 is directly reachable through eth0". Host A can now ping host B. It
cannot reach anything else.
Under the hood · How does A find B on the switch?
IP addresses are for humans and routing tables. On the wire, a frame is addressed to a hardware (MAC) address. When
host A wants to send to 192.168.1.11 for the first time, it broadcasts an ARP request: "who has
192.168.1.11?". Host B answers with its MAC address, and A remembers it for a while.
ip neigh shows what a machine has learned:
192.168.1.11 dev eth0 lladdr 08:00:27:b1:22:f0 REACHABLE
192.168.1.1 dev eth0 lladdr 52:54:00:12:35:02 STALE
An entry marked FAILED or INCOMPLETE means nobody answered for that address: the target is down, or it is not in
this network at all. You rarely need this in the exam, but it explains one rule: ARP only works inside a
network, which is why traffic for other networks must be handed to a gateway.
Routing: between networks
Parts:
- Host A (192.168.1.10)
- Router (.1.1 and .2.1)
- Host C (192.168.2.10)
- Route on A (192.168.2.0/24 via .1.1)
- Internet (everything else)
- Route on C (192.168.1.0/24 via .2.1)
Connections:
- Host A and Router: net 1
- Router and Host C: net 2
- Router to Internet: default route
- Route on A to Host A: tells A
- Route on C to Host C: tells C
A second network, 192.168.2.0/24, has its own switch. Host A cannot reach host C in it:
ping -c 1 192.168.2.10
ping: connect: Network is unreachable
"Network is unreachable" is the kernel saying: I have no route for that address, so I did not even try.
A router is a device with an interface in each network, here 192.168.1.1 and 192.168.2.1. It can pass
packets from one to the other. But host A must be told to use it. From A's point of view, 192.168.1.1 is the
gateway to the other network: the door out of its own.
# on host A: to reach network 2, go through the router's address in MY network
ip route add 192.168.2.0/24 via 192.168.1.1
That is still only half of a conversation. Host C receives the packet, wants to answer 192.168.1.10, and has no
route for it. So C needs the mirror image:
# on host C
ip route add 192.168.1.0/24 via 192.168.2.1
The default gateway
You cannot add a route for every network on the internet. Instead, a machine has one default route: the gateway to use when no other line matches.
ip route add default via 192.168.1.1
default is the same as 0.0.0.0/0, the network that contains every address. After the three steps, host A's
table reads:
default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.10
192.168.2.0/24 via 192.168.1.1 dev eth0
How the kernel chooses
- Find every line whose network contains the destination
- Take the most specific one (longest prefix)
- If it has 'via', send to that gateway
- If not, send directly on that interface
- No line matches: Network is unreachable
"Most specific" is the rule to remember. For the destination 192.168.2.10, both default (/0) and
192.168.2.0/24 match, and the /24 wins because it is narrower. The order of the lines on screen means nothing.
| Part of a route | Meaning |
|---|---|
192.168.2.0/24 or default | The destinations this line is for |
via 192.168.1.1 | Hand the packet to this gateway. The gateway must be directly reachable |
dev eth0 | Send it out of this interface |
scope link | The destination is on the wire of this interface, with no gateway |
proto kernel | The kernel added the line itself when the address was configured |
src 192.168.1.10 | The source address to put on packets that use this route |
A Linux host as a router
Parts:
- Host A (192.168.1.5)
- Host B (eth0 .1.6, eth1 .2.6)
- Host C (192.168.2.5)
- ip_forward = 1 (/etc/sysctl.d/)
Connections:
- Host A and Host B: eth0
- Host B and Host C: eth1
- ip_forward = 1 to Host B: allows forwarding
A router does not have to be a special box. Any Linux host with two interfaces can do the job. Host B here has
eth0 in the first network and eth1 in the second. A and C get routes that point at B:
# on host A
ip route add 192.168.2.0/24 via 192.168.1.6
# on host C
ip route add 192.168.1.0/24 via 192.168.2.6
The ping from A to C still fails. This time there is no error, only silence. The routes are right, and the packets reach B. But B throws them away, because by default Linux does not forward packets that arrive on one interface and are meant for another machine. That is a safety default: a server connected to a private and a public network should not quietly become a bridge between them.
One kernel setting controls it:
cat /proc/sys/net/ipv4/ip_forward
0
sysctl -w net.ipv4.ip_forward=1
Now B forwards, and A reaches C.
Making it survive a reboot
Everything typed so far lives only in the running kernel. After a reboot, the addresses, the routes and the forwarding setting are gone.
| What | Temporary (until reboot) | Permanent |
|---|---|---|
| IP forwarding | sysctl -w net.ipv4.ip_forward=1 | A line net.ipv4.ip_forward = 1 in a file under /etc/sysctl.d/, then sysctl --system |
| Addresses and routes | ip addr add, ip route add | The distribution's network configuration: Netplan files in /etc/netplan/ on Ubuntu, NetworkManager (nmcli) on Red Hat family systems |
Where this meets Kubernetes
Everything above shows up on a cluster node, with different names.
Every node is a router. A node has its real interface in the node network, and a set of virtual interfaces that lead to its Pods. Passing a packet from a Pod to the outside, or from another node to a Pod, is forwarding. That is why the official installation steps tell you to enable IP forwarding, and why kubeadm refuses to continue without it:
[ERROR FileContent--proc-sys-net-ipv4-ip_forward]: /proc/sys/net/ipv4/ip_forward contents are not set to 1
Pod networks are routes. On a node of a cluster that uses a simple routed Pod network, the table looks like this:
ip route
default via 192.168.56.1 dev eth0 proto dhcp src 192.168.56.10 metric 100
10.244.0.0/24 dev cni0 proto kernel scope link src 10.244.0.1
10.244.1.0/24 via 192.168.56.11 dev eth0
192.168.56.0/24 dev eth0 proto kernel scope link src 192.168.56.10
You can now read every line:
10.244.0.0/24 dev cni0: this node's own Pods, directly reachable through a local bridge calledcni0.10.244.1.0/24 via 192.168.56.11: the Pods ofnode01. To reach them, hand the packet tonode01, which is the gateway to its own Pods.192.168.56.0/24 dev eth0: the node network.default via 192.168.56.1: everything else, including the internet.
Other plugins wrap packets in a tunnel instead (you will see interfaces such as flannel.1 or tunl0), but the
table still tells you where traffic for another node's Pods goes. The lesson Pod Networking builds this up step by
step.
Using it
Example · Look at a machine's network in ten seconds
The brief (-br) forms give one line per interface:
ip -br link
ip -br addr
ip route
lo UNKNOWN 00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP>
eth0 UP 08:00:27:4a:9c:11 <BROADCAST,MULTICAST,UP,LOWER_UP>
lo UNKNOWN 127.0.0.1/8 ::1/128
eth0 UP 192.168.56.10/24 fe80::a00:27ff:fe4a:9c11/64
default via 192.168.56.1 dev eth0 proto dhcp src 192.168.56.10 metric 100
192.168.56.0/24 dev eth0 proto kernel scope link src 192.168.56.10
Interfaces and their state, addresses with their network size, and the routes. These three commands are the first thing to run on a node with a network problem.
Example · Ask the kernel which route it would take
Do not work out the longest prefix in your head. ip route get runs the real decision for one destination:
ip route get 10.244.1.7
10.244.1.7 via 192.168.56.11 dev eth0 src 192.168.56.10 uid 0
cache
ip route get 8.8.8.8
8.8.8.8 via 192.168.56.1 dev eth0 src 192.168.56.10 uid 0
cache
The first goes to node01, the second to the default gateway. If the answer is
RTNETLINK answers: Network is unreachable, no route matches and you have found the problem.
Example · Find which interface a node uses for the cluster
A node can have several interfaces. The kubelet registers the node with one address, shown as INTERNAL-IP:
kubectl get node node01 -o wide
ip -br addr | grep 192.168.56.11
NAME STATUS ROLES AGE VERSION INTERNAL-IP ...
node01 Ready <none> 12d v1.34.2 192.168.56.11 ...
eth0 UP 192.168.56.11/24 fe80::a00:27ff:fe9d:7e02/64
eth0 carries the cluster traffic of this node. On the same line of ip -br link you find its MAC address, and
ip route shows the default gateway. Tasks that ask "which interface", "what is the MAC address" or "what is the
default gateway of the node" are answered with exactly these commands.
Example · Prepare a new node for Kubernetes
Before kubeadm init or kubeadm join, a node must forward IPv4 packets. Make the setting permanent and load it
without a reboot:
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
EOF
sudo sysctl --system
sysctl net.ipv4.ip_forward
net.ipv4.ip_forward = 1
sysctl --system reads every file in the sysctl directories, so the last command confirms that the value really
took effect.
Command summary
| Task | Command |
|---|---|
| List interfaces | ip link (short: ip -br link) |
| Switch an interface on | ip link set dev eth0 up |
| Show addresses | ip addr (short: ip -br addr) |
| Add an address | ip addr add 192.168.1.10/24 dev eth0 |
| Show the routing table | ip route |
| Add a route to a network | ip route add 192.168.2.0/24 via 192.168.1.1 |
| Add a default gateway | ip route add default via 192.168.1.1 |
| Delete a route | ip route del 192.168.2.0/24 |
| Which route for one address? | ip route get 192.168.2.10 |
| Show learned neighbours (ARP) | ip neigh |
| Check forwarding | sysctl net.ipv4.ip_forward |
Every ip word can be shortened as long as it stays unique: ip a, ip r, ip l, ip n.
Tricks
Common mistakes
- Adding a route on one side only. The request arrives, the reply has nowhere to go, and the ping simply times out.
- Forgetting IP forwarding on the machine in the middle. Routes on both ends are correct, and the router silently drops the packets.
- Naming a gateway that is not a neighbour. The address after
viamust be directly reachable. Otherwise the kernel refuses withError: Nexthop has invalid gateway. - Using the router's address from the wrong network. Host A must use
192.168.1.1, the router's address in A's own network, not192.168.2.1. - Trusting a change made with
ipto survive a reboot. It does not. Write permanent settings to/etc/sysctl.d/and to the distribution's network configuration. - Reading the routing table top to bottom. Order on screen is not priority. The most specific matching network
wins, and
defaultonly applies when nothing else matches.
Recap
- An interface with an address and prefix puts a machine in a network. A switch connects machines inside that one network.
- A router connects networks. The gateway is the router's address in your own network.
ip routeshows the routing table: the most specific match wins, and the default route catches the rest.- Traffic needs routes in both directions.
- A Linux host forwards between interfaces only with
net.ipv4.ip_forward = 1. Every Kubernetes node needs it, set permanently in/etc/sysctl.d/. - On a node,
ip -br addr,ip routeandip route gettell you where traffic for a Pod or a node really goes.
Official docs
Quiz: check what you learned
5 questions on this lesson · about 3 min · instant feedback. Missed questions come back in your review queue.
Sign in to take the quiz and keep your score.
Practise: try it on a real cluster
A hands-on lab for this lesson. You get goals, not steps: any solution that reaches them passes.
Two small networks hang off one Linux host that should route between them. Three things are wrong: a missing default route, a wrong gateway, and a router that does not forward.
Fix · Level 2 · standard · target 18 min
Discussion
Learners ask and answer questions here. Sign in to read and join the discussion.