Procedure to enable Google Cloud CDN (Content Delivery Network) for your website.
In my previous post, I did a test and talked about how fast is CDN by Google. I covered briefly about enabling, and many of you asked to explain in details.
Cloud CDN by Google is a low-latency content delivery solution for small to enterprise business.
With more than 100 POP (point of presence) worldwide and accelerated content delivery using Google’s global premium network make the fastest SSL CDN globally.
The latest report by Cedexis.
Some of the features of Google Cloud CDN:
- HTTP/2 – it supports latest HTTP protocol for better performance
- Low latency – all contents are served through Anycast (single IP) worldwide
- Logging – it integrates with Stackdriver to provide detailed logging of a request, cache hit/miss
- Purge cache instantly
- Low cost
Wondering why pre-requisite?
Well, Google CDN only works with Google load balancer and cloud storage origins. This means if you are not hosting your application on GCP (Google Cloud Platform) then you can’t use their CDN.
In other words, it doesn’t support external origins yet. Simple.
So I assume you are hosting your application on GCP and ready to use a load balancer (LB) to use the cloud CDN.
You guessed it right; you can’t use VM as origin too. It can be enabled only on the load balancer.
Let’s get it started…
I’ve two VM running (one in Singapore and another in the US west). I want to load balance requests to both VM using HTTP load balancer and activate CDN on it.
I need to do the following.
- Create a health check
- Create an instance group
- Create a load balancer
- Enable CDN
Creating a health check
A health check is essential for the load balancer to determine if VM is healthy. When VM can’t respond to the query, then LB would mark that unhealthy and stop sending traffic.
- Login to GCP and go to Health Checks under Compute Engine
- Click “Create a health check.”
- Follow the wizard and enter the necessary information
- Protocol – select HTTP if your application hosted on VM will listen on port 80 and HTTPS for port 443
- Request path – a working context root.
- Health criteria – adjust as necessary
- Click create
It will take few seconds, and you will notice newly created health check on the list. We will use this shortly.
Creating an instance group
Instance group is used in the load balancing setup. It’s called backend services for LB.
- Go to Instance groups under Compute Engine
- Click “Create instance group.”
- Follow the wizard to provide necessary information
- Location – select single-zone as we are not going ahead with an auto-scaling option.
- Zone – choose the zone where VM is running
- Group type – tick unmanaged instance group
- VM instances – select the VM from the list
- Click Create
Repeat the steps for creating an instance group for another VM zone.
Now, we have two instance groups. Each mapped with the VM in the respective zone.
Creating a Load Balancer
- Go to Load balancing under Network services
- Click “Create load balancer.”
- Select the “HTTP(S) load balancing.”
- Enter the name of LB
- Click on “Backend configuration.”
- Click “Create a backend service.”
- Enter the name of the backend services
- Add both instances group which you created earlier as a backend
- Select health check which you created previously
- Session affinity – select if you want the session stickiness
- Click create
- Leave the default configuration in “Host and path rules”. However, if you need conditional forwarding then add as you need.
- Click frontend configuration and enter the name
- Select “create IP address” to get the static IP address. This is important as by default you will get ephemeral IP.
- Protocol, IP version, and port leave it to the default
- Click Done
- Click on review and finalize and if you feel something is wrong then change it else click “Create.”
- Once created, you will notice newly created LB on the list
This indicates LB is created. Let’s do some test to ensure LB works.
So, I try to access LB frontend IP, and it’s success!
You may also want to generate some loads to ensure traffic is routed to both the VMs. Google will forward requests to the nearest VM from user’s geolocation.
In my scenario, traffic from Asia will go to VM in Singapore and from America, it will go to US-West VM.
Activating Google Cloud CDN
And, finally, it’s time to enable the CDN.
- Go to Cloud CDN under network services
- Click “Add origin.”
- Select the load balancer from origin drop-down and click add
- You should see newly created CDN on the list
This concludes CDN is enabled on the load balancer.
Once you are satisfied with it, you can update your domain’s A record to load balancer global IP.
I hope this helps you. If you are looking to learn hands-on about Google Cloud Platform, then I would recommend exploring this course.