Introduction to Cloud CDN

Cloud CDN (Content Delivery Network) is a service provided by Google Cloud Platform that accelerates content delivery by caching content at strategically located edge points of presence (PoPs) around the world. This reduces latency and improves the user experience by serving content from locations closer to the user.

Key Concepts

  1. Edge Caching: Storing copies of content at various edge locations to reduce latency.
  2. Origin Server: The server where the original content is hosted.
  3. Cache Hit and Miss: A cache hit occurs when the requested content is found in the cache, while a cache miss occurs when it is not, requiring retrieval from the origin server.
  4. TTL (Time to Live): The duration for which content is cached before it is refreshed from the origin server.

Setting Up Cloud CDN

Prerequisites

  • A Google Cloud Platform account.
  • A project with billing enabled.
  • A backend service or bucket to serve as the origin for the CDN.

Steps to Enable Cloud CDN

  1. Create a Backend Service:

    gcloud compute backend-services create my-backend-service 
    --protocol=HTTP
    --port-name=http
    --health-checks=my-health-check
    --global
  2. Add a Backend to the Service:

    gcloud compute backend-services add-backend my-backend-service 
    --instance-group=my-instance-group
    --instance-group-zone=us-central1-a
    --global
  3. Create a URL Map:

    gcloud compute url-maps create my-url-map 
    --default-service my-backend-service
  4. Create a Target HTTP Proxy:

    gcloud compute target-http-proxies create my-http-proxy 
    --url-map=my-url-map
  5. Create a Global Forwarding Rule:

    gcloud compute forwarding-rules create my-http-rule 
    --global
    --target-http-proxy=my-http-proxy
    --ports=80
  6. Enable Cloud CDN:

    gcloud compute backend-services update my-backend-service 
    --enable-cdn
    --global

Practical Example

Example: Serving Static Content with Cloud CDN

  1. Create a Cloud Storage Bucket:

    gsutil mb gs://my-static-content
    
  2. Upload Content to the Bucket:

    gsutil cp index.html gs://my-static-content
    
  3. Make the Bucket Public:

    gsutil iam ch allUsers:objectViewer gs://my-static-content
    
  4. Create a Backend Bucket:

    gcloud compute backend-buckets create my-backend-bucket 
    --gcs-bucket-name=my-static-content
    --enable-cdn
  5. Create a URL Map:

    gcloud compute url-maps create my-url-map 
    --default-backend-bucket=my-backend-bucket
  6. Create a Target HTTP Proxy:

    gcloud compute target-http-proxies create my-http-proxy 
    --url-map=my-url-map
  7. Create a Global Forwarding Rule:

    gcloud compute forwarding-rules create my-http-rule 
    --global
    --target-http-proxy=my-http-proxy
    --ports=80

Explanation

  • Backend Service: Defines the origin server or instance group.
  • URL Map: Routes incoming requests to the appropriate backend service or bucket.
  • HTTP Proxy: Handles HTTP requests and forwards them to the URL map.
  • Forwarding Rule: Directs traffic to the HTTP proxy.

Practical Exercise

Exercise: Enable Cloud CDN for a Static Website

  1. Create a Cloud Storage Bucket named my-website-bucket.
  2. Upload a sample HTML file to the bucket.
  3. Make the bucket public.
  4. Create a backend bucket and enable Cloud CDN.
  5. Create a URL map to route traffic to the backend bucket.
  6. Create an HTTP proxy and a global forwarding rule.

Solution

  1. Create the Bucket:

    gsutil mb gs://my-website-bucket
    
  2. Upload Content:

    gsutil cp index.html gs://my-website-bucket
    
  3. Make the Bucket Public:

    gsutil iam ch allUsers:objectViewer gs://my-website-bucket
    
  4. Create Backend Bucket:

    gcloud compute backend-buckets create my-backend-bucket 
    --gcs-bucket-name=my-website-bucket
    --enable-cdn
  5. Create URL Map:

    gcloud compute url-maps create my-url-map 
    --default-backend-bucket=my-backend-bucket
  6. Create HTTP Proxy:

    gcloud compute target-http-proxies create my-http-proxy 
    --url-map=my-url-map
  7. Create Forwarding Rule:

    gcloud compute forwarding-rules create my-http-rule 
    --global
    --target-http-proxy=my-http-proxy
    --ports=80

Summary

In this section, you learned about Cloud CDN and how it can be used to accelerate content delivery by caching content at edge locations. You also learned how to set up Cloud CDN for both backend services and static content hosted in Cloud Storage. By following the practical example and exercise, you should now be able to enable and configure Cloud CDN for your own projects, improving the performance and user experience of your applications.

© Copyright 2024. All rights reserved