Google’s Cloud NAT gateway

Thanks to Yuvraaj at crave.io for the following

Google launched something called Cloud NAT Gateway that charges for bandwidth to do NAT. It makes it more expensive to run than going with external IP (at least in the low volumes that I do). I found this blog entry by Yuvraaj at Crave.io that allows you to enable NAT in a few steps. I’ve documented the commands below so I can refer to them in the future if I need them and in case the crave.io blog entry is no longer available or I can’t find it for some reason.

Create or edit this file/script that runs everytime the machine is booted

/etc/rc.local
#!/bin/bash

set -x

# Turn on IP forwarding
sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"
# Turn on the route
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

Setup Route in GCP Routing table

gcloud compute routes create no-ip-internet-route --network default --destination-range 0.0.0.0/0 --next-hop-instance nat-gateway --next-hop-instance-zone us-west1-b --tags no-ip --priority 800

Setup networking tags for VMs that need this

gcloud compute instances add-tags <existing-instance> --tags no-ip

Bash script to check if a process is running and if failed, restart it

Thanks to Karson for this one.

Create a file:

sudo nano ./run.sh

#!/bin/sh
if ps -ef | grep -v grep | grep boinc ; then
exit 1
else
sudo systemctl restart boinc-client &
exit 0
fi

Change permissions

sudo chmod +x ./run.sh

Make it run automatically

crontab -e

Every second

* * * * * /home/user/run.sh