Learning Cloud: Caching Cloud Storage content with Cloud CDN usinf Load Balancer

Опубликовано: 29 Сентябрь 2024
на канале: Cyethack Solutions
347
3

Objectives
In this lab, you learn how to perform the following tasks:

Create and populate a Cloud Storage bucket
Create an HTTP load balancer with Cloud CDN
Verify the caching of your bucket's content
Invalidate the cached content

Task 1. Create and populate a Cloud Storage bucket
Cloud CDN content can originate from different types of backends:

Compute Engine virtual machine (VM) instance groups
Zonal network endpoint groups (NEGs)
Internet network endpoint groups (NEGs), for endpoints that are outside of Google Cloud (also known as custom origins)
Google Cloud Storage buckets
In this lab, you configure a Cloud Storage bucket as the backend.

Create a unique Cloud Storage bucket
In the Cloud Console, on the Navigation menu (Navigation menu), click Cloud Storage -- Browser.

Click Create bucket.

Specify the following, and leave the remaining settings as their defaults:

Property Value (type value or select option as specified)
Name Enter a globally unique name
Location type Region
Location Choose a location that is very far from you
Prevent public access Deselect option to Enforce public access prevention to the bucket
Try to choose a location that is either halfway around the world from you or at least in a different continent. This provides a greater time difference between accessing the image with and without Cloud CDN enabled. See the current list of regions here.
Click Create.

Note the name of your storage bucket for the next subtask. It will be referred to as [your-storage-bucket].

Copy image files into your bucket
Copy images from a public Cloud Storage bucket to your own bucket.

In the Cloud Console, click Activate Cloud Shell (Cloud Shell).

If prompted, click Continue.

Store [your-storage-bucket] in an environment variable in Cloud Shell:

export YOUR_BUCKET=--Enter the name of [your-storage-bucket]--
To copy two images from a public bucket to your bucket, run the following commands:

gsutil cp gs://cloud-training/gcpnet/cdn/cdn.png gs://$YOUR_BUCKET
gsutil cp gs://cloud-training/gcpnet/cdn/regions.png gs://$YOUR_BUCKET
In the Cloud Console, click Refresh to verify that the images were copied.

In Cloud Shell, to publish the image files to the web, run the following command:

gsutil iam ch allUsers:objectViewer gs://$YOUR_BUCKET
In the Cloud Console, click Refresh to verify that both images have Public access set to Public to internet.
Task 2. Create the HTTP load balancer with Cloud CDN
HTTP(S) load balancing provides global load balancing for HTTP(S) requests of static content to a Cloud Storage bucket (backend). When you enable Cloud CDN on your backend, your content is cached at a location at the edge of Google's network, which is usually far closer to the user than your backend is.

Start the HTTP load balancer configuration
In the Cloud Console, on the Navigation menu (Navigation menu), click Network Services -- Load balancing.

Click Create load balancer.

Under HTTP(S) Load Balancing, click Start configuration.

Select From Internet to my VMs, and click Continue.

For Name, type cdn-lb

Configure the backend
Click Backend configuration.
For Backend services & backend buckets, click Create or select backend services & backend buckets -- Backend buckets -- Create a backend bucket.
For Name, type cdn-bucket
Click Browse under Cloud Storage bucket.
Select your bucket, and click Select.
Select Enable Cloud CDN.
Click Create.
Yes, enabling Cloud CDN is as simple as selecting Enable Cloud CDN!
Configure the frontend
The host and path rules determine how your traffic will be directed. For example, you could direct video traffic to one backend and image traffic to another backend. However, you are not configuring the host and path rules in this lab.

Click Frontend configuration.

Specify the following, leaving all other values with their defaults:

Property Value (type value or select option as specified)
Protocol HTTP
IP version IPv4
IP address Ephemeral
Port 80
Click Done.

Review and create the HTTP load balancer
Click Review and finalize.
Ensure that Cloud CDN is enabled for the Backend buckets. If it is not, return to the backend configuration to enable Cloud CDN.
Click Create. Wait for the load balancer to be created.
Click on the name of the load balancer (cdn-lb).
Note the IP address of the load balancer. It will be referred to as [LB_IP_ADDRESS].
Open a new browser tab and navigate to http://[LB_IP_ADDRESS]/regions.png, replacing [LB_IP_ADDRESS] with the IP address of the load balancer.
You should see an error because the load balancer needs time to be available from all edge locations. Wait a couple of minutes and keep refreshing the browser tab until you can see the image. In the meantime, you can read about caching details, including the cache mode.
When you can see the regions.png image (shown below), continue to the next task.