Day 1/100daysofK8s
Switch — connect 2(or more) systems on the same network.
Router — connects 2(or more) systems on different networks. Router is just another system on one of the networks. If network1 has IP range 192.168.1.10
and network2 has 192.168.1.20
, router’s IP can be in the range of either of the networks, depending on what gateway one is creating.
Router is more like a house, switch is rooms and gateway is the door. So to for network1 to communicate to network2, it first needs to find the route to network2.
But how do the systems know where the router is? This is where Gateway comes into picture.
Gateway — create a route for systems on different networks to communicate with each other. Use route
to check the existing gateways on the system. To create a new gateway, run — ip route add <network1:system_ip> via <network2:router_ip>
. These routes must be added for every system, which means for any communication that happens on the internet, we need to first add the ip address of the internet site in our system. But we can add a default route instead with — ip add route default via <network2:router_ip>
.
Hence, in case of any troubles with the connection, on the internet or between different networks, looking at the gateway is a good place to start.
Example —
System A <-> [192.168.1.6]System B[192.168.2.6] <-> SystemC
[192.168.1.5]……………………………………………….[192.168.2.5]
System A and System C are 2 systems on different networks. System B is the router that can connect both the systems. It has IP address from both the networks. There is no direct way for System A to communicate to System C, nor is there a way for System C to communicate System A.
For System A to send a request to System C, we need to add a route that goes via System B — ip route add 192.168.2.0/24 via 192.168.1.6
.
For System C to send a response back to System A, we need to add a route that goes via System B — ip route add 192.168.1.0/24 via 192.168.2.6
System C will be able to ping System A, but no data from System C will be sent to System A . This is because, by default, linux does not allow packets to be transferred via interfaces/routes. This is due to security reasons, in case, System C is of private network and System A of public, we do not want private data to be received by public systems, unless explicitly done.
To know if the packet forwarding is allowed, check cat /proc/sys/net/ipv4/ip_forward
— by default, this is set to 0, which means no packet forwarding, setting it to 1, allows the forwarding. But this setting would only stay until the system reboots, to solidify these changes, set net.ipv4.ip_forward=1
in /etc/sysctl.conf
.
Useful commands when dealing with networking —
ip link
— to list and modify interfaces on the hostip addr
— see ip address assigned to the interfaces.ip addr add <system_ip> dev <interface_name>
— to set ip address on the interface
Any changes made by the above commands will only last until the system is rebooted. To solidify the changes, idk!