We are following this original tutorial by Simos so we are just tweaking this for Google Cloud Platform to make sure HAProxy will work properly.

We assume that LXD has been already setup from that guide and we are now on step 3 where containers are already created.

The difference on this tutorial if you are using Google Cloud containers is that there’s no need to specify the IP address. GCP is using NAT on all Virtual Machines so the GCP container/main host does not actually own the IP address and we are just removing this.

-d your_server_ip/32

From the original tutorial command for IP tables:

sudo iptables -t nat -I PREROUTING -i eth0 -p TCP -d your_server_ip/32 --dport 80 -j DNAT --to-destination your_haproxy_ip:80

Command that we should be using for GCP (Google Cloud Platform) <– We we will this later after we flushed the IP tables.

sudo iptables -t nat -I PREROUTING -i ens4 -p TCP --dport 80 -j DNAT --to-destination 10.22.74.193:80

10.22.74.193 is the HAProxy IP from the LXC list (screenshot above).

Clearing the IP Tables using this bash script:

#!/bin/sh
echo "Flushing iptables rules..."
sleep 1
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

ens4 is the Network Infterace Card name of the GCP machine. You can get that using “ifconfig” command

Execute the IP Tables command

(10.22.74.193 is the IP of our HAProxy container)

sudo iptables -t nat -I PREROUTING -i ens4 -p TCP --dport 80 -j DNAT --to-destination 10.22.74.193:80

Your IP Address from GCP

Save the IP tables.

sudo /sbin/iptables-save

My DNS settings for that subdomain

Testing HAProxy

34.96.177.142 is the IP of your GCP machine

curl --verbose --header 'Host: lxd1.causingdesigns.net' http://34.96.177.142

Adding another site

edit the haproxy.cfg file then add the other domain

Test the haproxy config file and reload it.

Adding the domain to my DNS list

And see the beauty of HAProxy

I hope you enjoyed this article! 🙂

Subscribe To Our Newsletter

Join our mailing list to receive the latest website layout designs, news and updates from our team.

You have Successfully Subscribed!

Share This