VLS2VLS-Routing

From CDOT Wiki
Revision as of 11:47, 18 March 2021 by Rchan (talk | contribs) (Manually Configure routes on each VM in each virtual network)
Jump to: navigation, search

Background Information

The following configuration may be used to set up the proper routes between private Virtual LAN Segments (VLS) in OPS535 Virtual Lab on CentOS 8.x virtual machines. The custom routes added to the gateway VM (VM1), and their corresponding VMs (VM2 to VM4) in their private VLS will enable VMs in each private VLS to communicate with VMs in the other private VLS.


Virtual LAN Segment setting

Consider three VLSs (one for each student) where there are 3 gateways, one for each VLS in the OPS535 virtual lab:

VLS 1

VLS network: 192.168.1.0/24

Gateway (Host) IPs: Internal (ens224): 192.168.1.1 External (ens192): 172.20.1.1

VMs IPs:

  • VM2: 192.168.1.2
  • VM3: 192.168.1.3
  • VM4: 192.168.1.4

Default gateway on each VM2: 172.20.255.1 Gateway to the 192.168.0.0/16 network: 192.168.1.1

VLS 2

VLS network: 192.168.2.0/24

Gateway (Host) IPs: Internal (ens224): 192.168.2.1 External (ens192): 172.20.2.1

VMs IPs:

  • VM2: 192.168.2.2
  • VM3: 192.168.2.3
  • VM4: 192.168.2.4

Default gateway on each VM2: 172.20.255.1 Gateway to the 192.168.0.0/16 network: 192.168.2.1

VLS 3

VLS network: 192.168.3.0/24

Gateway (Host) IPs: Internal (ens224): 192.168.3.1 External (ens192): 172.20.3.1

VMs IPs:

  • VM2: 192.168.3.2
  • VM3: 192.168.3.3
  • VM4: 192.168.3.4

Default gateway on each VM2: 172.20.255.1 Gateway to the 192.168.0.0/16 network: 192.168.3.1


Manually configure custom routes on the gateways to reach VMs in other VLSs

You only need to create the custom routes on your own VLS, but make sure that other student's VLS has the custom routes to your VLS.

On VLS 1

On VLS 1's gateway (192.168.1.1 or 172.20.1.1): Using nmcli:

  • nmcli con modify ens192 +ipv4.routes "192.168.2.0/24 172.20.2.1 100"
  • nmcli con modify ens192 +ipv4.routes "192.168.3.0/24 172.20.3.1 100"
  • nmcli con down ens192
  • nmcli con up ens192

Check to make sure that your kernel routing table has the proper entries with the following command:

  • ip route show

On VLS 2

On VLS 2's gateway (192.168.2.1 or 172.20.2.1): Using nmcli:

  • nmcli con modify ens192 +ipv4.routes "192.168.1.0/24 172.20.1.1 100"
  • nmcli con modify ens192 +ipv4.routes "192.168.3.0/24 172.20.3.1 100"
  • nmcli con down ens192
  • nmcli con up ens192

Check to make sure that your kernel routing table has the proper entries with the following command:

  • ip route show

On VLS 3

On VLS 3's gateway (192.168.3.1 or 172.20.3.1): Using nmcli:

  • nmcli con modify ens192 +ipv4.routes "192.168.1.0/24 172.20.1.1 100"
  • nmcli con modify ens192 +ipv4.routes "192.168.2.0/24 172.20.2.1 100"
  • nmcli con down ens192
  • nmcli con up ens192

Check to make sure that your kernel routing table has the proper entries with the following command:

  • ip route show

Manually Configure routes on each VM in each VLS

You only need to create the custom routes on the VM in your VLS, but make sure that other students have proper routes on their VMs as well.

VLS 1 VM2 to VM4

On VLS 1's VM (192.168.1.2 .. 192.168.1.4)

  • nmcli con modify ens224 +ipv4.routes "192.168.0.0/16 192.168.1.1 100"
  • nmcli con down ens224
  • nmcli con up ens224
  • ip route show

VLS 2 VM2 to VM4

On VLS 2's VM (192.168.2.2 .. 192.168.2.4)

  • nmcli con modify ens224 +ipv4.routes "192.168.0.0/16 192.168.2.1 100"
  • nmcli con down ens224
  • nmcli con up ens224
  • ip route show

VLS 3 VM2 to VM4

On VLS 3's VM (192.168.3.2 .. 192.168.3.4)

  • nmcli con modify ens224 +ipv4.routes "192.168.0.0/16 192.168.3.1 100"
  • nmcli con down ens224
  • nmcli con up ens224
  • ip route show

Automate the process using BASH script

The above example shows that 12 route commands will be needed to create the necessary routes just for 3 virtual networks, each with a gateway and one VM. It will be difficult to manage all the routes if the number of virtual networks is much greater. The following script will be more practical if your need to set up all the necessary routes for a large number of virtual networks. Both script use the shell environment variables "MY_NETWORK_NO" and "OTHER_NETWORKS" to allow customization for different values of private network address.

Shell Script for adding the proper routes on your gateway to other's private network

  • set the shell variable MY_NETWORK_NO to your assigned network ID (we use 99 here is an example, please change 99 to your assigned network ID)
export MY_NETWORK_NO=99
  • set the shell variable OTHER_NETWORKS to the list of private network you want to create a route. The following example assign the list of network numbers 32, 33, 34, 35, 40, 41, and 99 that you want to create a route on your gateway:
export OTHER_NETWORKS="32 33 34 35 40 41 99"

The following Bash script to add 7 new route to your gateway host's routing table:

#! /bin/bash
if [ -z "$MY_NETWORK_NO" ]
then
	echo "Please run the following command to assign your network number" >&2
	echo "to the shell varible MY_NETWORK_NO and run this script again." >&2
	echo "Please replace xx with your actual network number." >&2
	echo "  export MY_NETWORK_NO=xx" >&2
	exit 1
fi
X=${MY_NETWORK_NO}
echo "Your Network Number is $X"
if [ -z "$OTHER_NETWORKS" ]
then
	echo "Please run the following command to assign the list of " >&2
	echo "private networks you want to add a route on your gateway " >&2
	echo "to the shell variable OTHER_NETWORKS and run this script again." >&2
	echo "The following command assign 32, 33, 34, 35, 40, 41, and 99" >&2
	echo "to the variable OTHER_NETWORKS. Please replace those number" >&2
	echo "with the ones you actually wanted." >&2
	echo "  export OTHER_NETWORKS=\"32 33 34 35 40 41 99\"" >&2
	exit 2
fi
Y=${OTHER_NETWORKS}
echo "Route to be add for the network number(s) $Y"
echo -n "Press ENTER to continue ... "
read dummy

# Add new route
for y in $Y
do
	if [ "$X" -ne "$y" ]
	then
	    route add -net 192.168.${y}.0 netmask 255.255.255.0 gw 172.16.${y}.1
	    echo "Adding route to 192.168.${y}.0 network." 
	fi
done

# enable IP forwarding on the gateway
echo "1" > /proc/sys/net/ipv4/ip_forward

Shell Script for adding the proper routes on your VM (all Linux VMs) to other's private network

  • set the shell variable MY_NETWORK_NO to your assigned network ID (we use 99 here is an example, please change 99 to your assigned network ID)
export MY_NETWORK_NO=99
  • set the shell variable OTHER_NETWORKS to the list of private network you want to create a route. The following example assign the list of network numbers 32, 33, 34, 35, 40, 41, and 99 that you want to create a route on your gateway:
export OTHER_NETWORKS="32 33 34 35 40 41 99"

The following Bash script to add 7 new route to your VM's routing table:

#! /bin/bash
if [ -z "$MY_NETWORK_NO" ]
then
	echo "Please run the following command to assign your network number" >&2
	echo "to the shell varible MY_NETWORK_NO and run this script again." >&2
	echo "Please replace xx with your actual network number." >&2
	echo "  export MY_NETWORK_NO=xx" >&2
	exit 1
fi
X=${MY_NETWORK_NO}
echo "Your Network Number is $X"
if [ -z "$OTHER_NETWORKS" ]
then
	echo "Please run the following command to assign the list of " >&2
	echo "private networks you want to add a route on your gateway " >&2
	echo "to the shell variable OTHER_NETWORKS and run this script again." >&2
	echo "The following command assign 32, 33, 34, 35, 40, 41, and 99" >&2
	echo "to the variable OTHER_NETWORKS. Please replace those number" >&2
	echo "with the ones you actually wanted." >&2
	echo "  export OTHER_NETWORKS=\"32 33 34 35 40 41 99\"" >&2
	exit 2
fi
Y=${OTHER_NETWORKS}
echo "Route to be add for the network number(s) $Y"
echo -n "Press ENTER to continue ... "
read dummy

# Add new route
for y in $Y
do
	if [ "$X" -ne "$y" ]
	then
	    route add -net 192.168.${y}.0 netmask 255.255.255.0 gw 192.168.${X}.1
	    echo "Adding route to 192.168.${y}.0 network via 192.168.${X}.1" 
	fi

done

# Add route to the 172.16.0.0 network
route add -net 172.16.0.0 netmask 255.255.0.0 gw 192.168.${X}.1