Configure Subdomain to Google Compute Engine with Google Cloud DNS + HAProxy + Docker

Photo by pixabay.com

Hi everyone! With me again and again to share some experience, though or opinion about technology related with software engineering field. On previous story, we already learn about configuring domain to Google Compute Engine with Google Cloud DNS. The details of the story can be found here: https://medium.com/@bismobaruno/2fb0a26832af

How about if we want to make subdomain? Maybe we want to separate our services behind HAProxy. The solution is using subdomain!

We will continue our previous work for this example. Or make sure you already have service on Google Compute Engine that running well with domain on Google Cloud DNS and HAProxy as proxy.

1. Setup Subdomain Cloud DNS

Choose Cloud DNS service for starting this part.

Picture 1 Choose Cloud DNS Service

Our previously Zone record should be exists. Click the record for see the detail.

Picture 2 Choose Zone

Add new record for adding our subdomain data.

Picture 3 Choose Add Record Set

Fill the information like DNS Name and IPv4 Address. IPv4 Address should have the same value with our domain IPv4 Address. Just copy the record and put into the form. Click Create button to proceed the process.

Picture 4 Add Record Set

After record successfully added, ping the subdomain from web browser and make sure can serve the response.

Picture 5 Ping Subdomain

2. Setup Additional Service

Cool, our subdomain already working properly. Next, we need another service for testing subdomain will be works as our expected.

Let’s create simple Go app for print the hostname on your local machine.

Don’t forget to Dockerize the application with this Dockerfile.

Build the image with this command:

docker build -t momo89/go-hostname .
Picture 6 Build Docker Image

Login to the Docker Hub (make sure you already have an account):

docker login

Push to the image to Docker Hub:

docker push momo89/go-hostname:latest
Picture 7 Push Docker Image

3. Running Additional Service

Connect to your instance, and just run the additional service by this command:

docker run -p 5000:5000 -d momo89/go-hostname
Picture 8 Pull Docker Image

Check the container service already running with:

docker ps
Picture 9 Check Additional Service Container

Try to ping the app with curl command:

curl -s localhost:5000
Picture 10 Ping Additional Go App

Nice, our additional service on port :5000 already running and can serve the response with hostname data.

4. Setup HAProxy Subdomain

Next, we will update HAProxy config and reset the service. Modify the HAProxy config like script below. We adding additional backend config and modify frontend config for handle subdomain request.

cd $HOME
cd haproxy
vi haproxy.cfg

Next, find the container id from our HAProxy service with this command:

docker ps
Picture 11 Check HAProxy Container ID

Copy the modified file to container:

docker cp haproxy.cfg 429405ee2091:/usr/local/etc/haproxy/haproxy.cfg

Reload HAProxy service:

docker kill -s HUP 429405ee2091
Picture 12 Successfully Reload HAProxy service
Picture 12 Successfully Setup Subdomain

And Voila, it’s works! Now you can access the subdomain that successfully pointing to another service on port :5000 !

Hope you enjoy it, I’m happy if this article useful for you! Happy Pointing!

Thank you!

Others story that still related

Software Engineer | Traveler | Guitarist | J-Lovers