Prerequisite Switching Routing Gateways CNI in kubernetes
Networking fundamentals for Linux and Kubernetes covering switching routing gateways CNIs CoreDNS and network namespaces with practical commands and troubleshooting
In this lesson we refresh the networking fundamentals you’ll need when working with Linux hosts and Kubernetes: switching, routing, gateways, CNIs, plus a short introduction to DNS (CoreDNS) and Linux network namespaces. This is a hands-on, sysadmin- and developer-oriented guide — lots of commands, examples, and troubleshooting pointers to help you inspect and debug networking on hosts and in container environments.
What is a network?At its simplest, a network connects two or more hosts so they can exchange packets. Consider two machines, A and B (physical machines, VMs, or laptops). To let A reach B you typically plug both into the same switch and give each host a network interface and an IP address on the same subnet.On Linux:
Inspect interfaces with ip link.
Assign IP addresses with ip addr.
Example — bring up two hosts on a single subnet 192.168.1.0/24 and verify connectivity:
$ ip link1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 10002: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000$ sudo ip addr add 192.168.1.10/24 dev eth0$ sudo ip addr add 192.168.1.11/24 dev eth0$ ping 192.168.1.11PING 192.168.1.11 (192.168.1.11) 56(84) bytes of data.64 bytes from 192.168.1.11: icmp_seq=1 ttl=64 time=4.00 ms64 bytes from 192.168.1.11: icmp_seq=2 ttl=64 time=3.98 ms
Switches and subnetsA switch forwards Ethernet frames within a single Layer‑2 broadcast domain — typically one IP subnet. If you place hosts A and B on 192.168.1.0/24 they can reach each other directly via the switch. If hosts C and D are on another subnet (for example 192.168.2.0/24), a router is required to forward packets between the two subnets.A router has interfaces in both subnets (for example 192.168.1.1 and 192.168.2.1). Hosts use a gateway (a route) to send packets destined for other subnets to that router.
Routing — view and configureThe kernel routing table determines where packets are sent. View it with ip route or route -n. Initially you may see no explicit route for an off-subnet network:
$ route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface# (no route for 192.168.2.0/24 shown)
To allow a host on 192.168.1.0/24 to reach 192.168.2.0/24 via a router at 192.168.1.1:
$ sudo ip route add 192.168.2.0/24 via 192.168.1.1
Verify the new route:
$ route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.2.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth0
Default route (internet access)To reach arbitrary Internet destinations you add a default route that points to the router connected to the Internet. This is a 0.0.0.0/0 route:
$ sudo ip route add default via 192.168.2.1
Example route -n output with a default route:
$ route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.2.1 0.0.0.0 UG 0 0 0 eth0192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
Multiple routersIf you have multiple routers (for example one for internal networks and one for the Internet), add specific routes for the internal networks and a default route for everything else:
$ sudo ip route add 192.168.1.0/24 via 192.168.2.2$ sudo ip route add default via 192.168.2.1
Troubleshooting internet connectivity often begins by checking your routing table and confirming a valid default gateway.Using a Linux host as a routerSuppose you have three hosts:
A: 192.168.1.5 (on 192.168.1.0/24)
B: 192.168.1.6 and 192.168.2.6 (B has two interfaces: eth0 on 192.168.1.0 and eth1 on 192.168.2.0)
C: 192.168.2.5 (on 192.168.2.0/24)
To enable A ↔ C communication, configure routes:On host A (route to 192.168.2.0 via B on 192.168.1.6):
$ sudo ip route add 192.168.2.0/24 via 192.168.1.6
On host C (route to 192.168.1.0 via B on 192.168.2.6):
$ sudo ip route add 192.168.1.0/24 via 192.168.2.6
If you attempt to ping before adding routes you will likely see:
$ ping 192.168.2.5ping: connect: Network is unreachable
After adding routes, packets reach B, but Linux will not forward packets between interfaces by default. Kernel packet forwarding is controlled by net.ipv4.ip_forward.Enable forwarding temporarily:
# Using procfs$ sudo sh -c 'echo 1 > /proc/sys/net/ipv4/ip_forward'# Or using sysctl$ sudo sysctl -w net.ipv4.ip_forward=1
Persist forwarding across reboots by adding to /etc/sysctl.conf:
net.ipv4.ip_forward=1
Then reload:
$ sudo sysctl -p
Remember: changes made with ip addr and ip route are not persistent across reboots. To make IPs and routes persistent, update your distribution’s network configuration (for Debian/Ubuntu, /etc/network/interfaces or Netplan files; for RHEL/CentOS, use NetworkManager or /etc/sysconfig/network-scripts). For IP forwarding, update /etc/sysctl.conf as shown above.
Quick reference — useful commands
Command
Description
ip link
List and modify network interfaces on the host.
ip addr
View IP addresses assigned to interfaces.
ip addr add <addr>/<prefix>dev <iface>
Assign an IP address to an interface (wrap <...> in backticks as shown).
ip route or route -n
View the kernel routing table.
ip route add <network>/<prefix>via <gateway>
Add a route to the routing table.
ip route add default via <gateway>
Add a default route (0.0.0.0/0).
cat /proc/sys/net/ipv4/ip_forward
Check IP forwarding state (0 = disabled, 1 = enabled).
sudo sysctl -w net.ipv4.ip_forward=1
Enable IP forwarding temporarily.
Edit /etc/sysctl.conf and run sudo sysctl -p
Make IP forwarding persistent.
DNS and CoreDNS (brief introduction)On Linux, DNS resolution is typically configured via /etc/resolv.conf. System resolvers (systemd-resolved, NetworkManager) may manage this file — inspect the active configuration to understand where DNS queries are sent.In Kubernetes, CoreDNS is the default cluster DNS service. It provides DNS resolution for service discovery inside the cluster (pods resolving my-service.default.svc.cluster.local, etc.). When troubleshooting DNS in Kubernetes:
Check the CoreDNS pods (kubectl get pods -n kube-system).
Verify pod /etc/resolv.conf and that DNS policies are correct for the pod.
Network namespaces (short overview)Linux network namespaces isolate network resources — each namespace has its own interfaces, routing table, and firewall rules. Containers use network namespaces to provide isolation between workloads. Useful debugging commands:
ip netns — list network namespaces.
ip netns exec <ns> <command> — run a command inside a namespace.
ip link, ip addr, ip route — run inside a namespace to inspect interfaces, addresses, and routes for that namespace.
That’s it for this lesson — these basics are practical building blocks you’ll rely on when working with container networking, CNIs, and Kubernetes networking concepts.