Configure Subdomain to Google Compute Engine with Google Cloud DNS + HAProxy + Docker
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.
Our previously Zone record should be exists. Click the record for see the detail.
Add new record for adding our subdomain data.
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.
After record successfully added, ping the subdomain from web browser and make sure can serve the response.
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 .
Login to the Docker Hub (make sure you already have an account):
Push to the image to Docker Hub:
docker push momo89/go-hostname:latest
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
Check the container service already running with:
Try to ping the app with curl command:
curl -s localhost:5000
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.
Next, find the container id from our HAProxy service with this command:
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
And Voila, it’s works! Now you can access the subdomain that successfully pointing to another service on port :
Hope you enjoy it, I’m happy if this article useful for you! Happy Pointing!