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
- Edge Caching: Storing copies of content at various edge locations to reduce latency.
- Origin Server: The server where the original content is hosted.
- 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.
- 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
-
Create a Backend Service:
gcloud compute backend-services create my-backend-service
--protocol=HTTP
--port-name=http
--health-checks=my-health-check
--global -
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 -
Create a URL Map:
gcloud compute url-maps create my-url-map
--default-service my-backend-service -
Create a Target HTTP Proxy:
gcloud compute target-http-proxies create my-http-proxy
--url-map=my-url-map -
Create a Global Forwarding Rule:
gcloud compute forwarding-rules create my-http-rule
--global
--target-http-proxy=my-http-proxy
--ports=80 -
Enable Cloud CDN:
gcloud compute backend-services update my-backend-service
--enable-cdn
--global
Practical Example
Example: Serving Static Content with Cloud CDN
-
Create a Cloud Storage Bucket:
gsutil mb gs://my-static-content
-
Upload Content to the Bucket:
gsutil cp index.html gs://my-static-content
-
Make the Bucket Public:
gsutil iam ch allUsers:objectViewer gs://my-static-content
-
Create a Backend Bucket:
gcloud compute backend-buckets create my-backend-bucket
--gcs-bucket-name=my-static-content
--enable-cdn -
Create a URL Map:
gcloud compute url-maps create my-url-map
--default-backend-bucket=my-backend-bucket -
Create a Target HTTP Proxy:
gcloud compute target-http-proxies create my-http-proxy
--url-map=my-url-map -
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
- Create a Cloud Storage Bucket named
my-website-bucket
. - Upload a sample HTML file to the bucket.
- Make the bucket public.
- Create a backend bucket and enable Cloud CDN.
- Create a URL map to route traffic to the backend bucket.
- Create an HTTP proxy and a global forwarding rule.
Solution
-
Create the Bucket:
gsutil mb gs://my-website-bucket
-
Upload Content:
gsutil cp index.html gs://my-website-bucket
-
Make the Bucket Public:
gsutil iam ch allUsers:objectViewer gs://my-website-bucket
-
Create Backend Bucket:
gcloud compute backend-buckets create my-backend-bucket
--gcs-bucket-name=my-website-bucket
--enable-cdn -
Create URL Map:
gcloud compute url-maps create my-url-map
--default-backend-bucket=my-backend-bucket -
Create HTTP Proxy:
gcloud compute target-http-proxies create my-http-proxy
--url-map=my-url-map -
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.
Google Cloud Platform (GCP) Course
Module 1: Introduction to Google Cloud Platform
- What is Google Cloud Platform?
- Setting Up Your GCP Account
- GCP Console Overview
- Understanding Projects and Billing
Module 2: Core GCP Services
Module 3: Networking and Security
Module 4: Data and Analytics
Module 5: Machine Learning and AI
Module 6: DevOps and Monitoring
- Cloud Build
- Cloud Source Repositories
- Cloud Functions
- Stackdriver Monitoring
- Cloud Deployment Manager
Module 7: Advanced GCP Topics
- Hybrid and Multi-Cloud with Anthos
- Serverless Computing with Cloud Run
- Advanced Networking
- Security Best Practices
- Cost Management and Optimization