How is network configured on Google Cloud linux VMs

I have a Ubuntu 16.04LTS VM with two NICs ( each has a public IP ) on Google Cloud.
I need to configure it in a way where all traffic on port 2000 goes though interface1 and all traffic for port 2001 through interface2.
I’ve already set up the firewall through gcloud and that work just fine.

I also have 2 ip rules and 2 ip routes to push traffic through the correct NICs.

echo 300 guest >> /etc/iproute2/rt_tables
ip route add 10.2.0.2/32 dev interface2 table guest
ip route add default via 10.2.0.1 dev interface2 table guest
ip rule add from 10.2.0.2/32 table guest
ip rule add to 10.2.0.2/32 table guest

What I’m struggling with is how to apply these changes on boot.
I thought I could just add the above to /etc/network/interfaces or any files referenced through this file located in /etc/network/interfaces.d with source. But this doesn’t seem to make any difference whatsoever.

Below is what I added to /etc/network/interfaces

auto interface2
iface interface2 inet dhcp
         post-up ip route add 10.2.0.2/32 dev interface2 table guest
         post-up ip route add default via 10.2.0.1 dev interface2 table guest
         post-up ip rule add from 10.2.0.2/32 table guest
         post-up ip rule add to 10.2.0.2/32 table guest

This only works if I do ifdown interface2 && ifup interface2.

I also followed the below

# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}

So with all the above when the VM starts it setts up the network with two NICs somehow and ignores the above. Can somebody please explain to me how this should be done.

Very much appreciated in advance

Answer

If you want to accomplish it with a script every time the VM instance starts, go with start up scripts.

Check their documentation at https://cloud.google.com/compute/docs/startupscript

Attribution
Source : Link , Question Author : Bart C , Answer Author : fbraga

Leave a Comment