Caching GCS using Cloud CDN & Cloud Load Balancer
Create and populate a Cloud Storage bucket
- In the Cloud Console, from the Navigation menu click Cloud Storage > Browser.
- Click Create bucket.
- Specify values for the following fields, leave all others at their defaults:
Name: Enter a globally unique name. If the name you enter is not unique, you’ll see a message to try another name.
Location type: Multi-regional
Location: Choose a location that is very far from you. - Click Create.
- Run the following command in Cloud Shell, replacing
[your-storage-bucket]
with your bucket’s name:gsutil cp gs://cloud-training/gcpnet/cdn/cdn.png gs://[your-storage-bucket]
- On the Bucket details page, click Refresh to verify that the image was copied into your bucket.
- On the Bucket details page click the Permissions tab then Click Add
- In New principals, start typing “allUsers” then select the choice from the dropdown menu.
- For the role, select Cloud Storage > Storage Object Viewer > Save > Allow public access
- Click on the Objects tab then Click Copy URL under Public access, then open a new browser tab and paste it in to verify that you can access the image.
Create an HTTP Load Balancer with Cloud CDN
- In the Cloud Console, from the Navigation menu select Network Services > Load balancing, and click Create load balancer.
- Under HTTP(S) Load Balancing, click Start configuration.
- Choose From Internet to my VMs or serverless services and click Continue. Set the Name to
cdn-lb
. - Click on Backend configuration.
- For Backend services & backend buckets, click backend services & backend buckets dropdown menu, then click Create a backend bucket.
- Set the Name to
cdn-bucket
. Under Cloud Storage bucket click Browse . - Click your bucket and click Select. Check Enable Cloud CDN.
- Cash Static Content is selected by default, for this lab, set the Client time to live, Default time to live, and Maximum time to live to 1 minute. Click Create.
- Click Frontend configuration. Specify the following, leave all other values with their defaults:
Protocol: HTTP
IP version: IPv4
IP address: Ephemeral
Port: 80
Click Done. - Click Review and finalize.
Verify the caching of your bucket’s content
- In Cloud Shell, store the IP address of the Load Balancer in an environment variable:
export LB_IP_ADDRESS=<Enter the IP address of the Load Balancer>
- Run the following to time 3 consecutive HTTP requests:
for i in {1..3};do curl -s -w "%{time_total}\n" -o /dev/null http://$LB_IP_ADDRESS/cdn.png; done
- In the Cloud Console, from the Navigation menu select Logging > Logs Explorer.
- Under the Resources filter, select Cloud HTTP Load Balancer > cdn-lb-forwarding-rule > cdn-lb. Then click Apply.
- Click Run Query.
- Expand the first log entry (on top).
- Within the entry, expand the httpRequest and notice that the cacheLookup is
true
but there is no cacheHit field. This illustrates that the cache did not contain the image on this first request. - Expand the jsonPayload and notice that the statusDetails field contains
response_sent_by_backend
. This also illustrates that the image came from the backend bucket on this first request. - Close the current log entry and expand a log entry towrds the bottom of the query results.
- Within the entry, expand the httpRequest and notice that the cacheLookup and CacheHit is
true
. This illustrates that the cache contained the image on this request. - Expand the jsonPayload and notice that the statusDetails field contains
response_from_cache
. This also illustrates that the cache provided the image on this request, instead of the backend.
Tag:Google Cloud