0
0
GCPcloud~7 mins

Cloud CDN integration in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Cloud CDN helps deliver your website or app content faster by storing copies closer to users around the world. It solves the problem of slow loading times by caching content at locations near your users.
When you want to speed up your website for visitors in different countries.
When you have a web app that serves large files like images or videos and want to reduce load on your main server.
When you want to reduce bandwidth costs by caching content at Google’s edge locations.
When you want to improve reliability by serving cached content even if your main server is slow or down.
When you want to secure your content delivery with HTTPS and Google’s security features.
Config File - backend_service.yaml
backend_service.yaml
apiVersion: compute.googleapis.com/v1
kind: BackendService
metadata:
  name: example-backend-service
spec:
  backends:
  - group: projects/my-project/zones/us-central1-a/instanceGroups/example-instance-group
  cdnPolicy:
    cacheMode: CACHE_ALL_STATIC
    signedUrlCacheMaxAgeSec: 3600
  protocol: HTTP
  portName: http
  timeoutSec: 30
  enableCdn: true

This file defines a Backend Service in Google Cloud that uses Cloud CDN.

backends: Points to the instance group serving your content.

cdnPolicy: Configures caching behavior, here caching all static content for 1 hour.

enableCdn: Enables Cloud CDN for this backend service.

Commands
Creates a backend service named example-backend-service with HTTP protocol and enables Cloud CDN globally.
Terminal
gcloud compute backend-services create example-backend-service --protocol=HTTP --port-name=http --timeout=30 --enable-cdn --global
Expected OutputExpected
Created [https://www.googleapis.com/compute/v1/projects/my-project/global/backendServices/example-backend-service].
--enable-cdn - Turns on Cloud CDN for this backend service.
--global - Creates the backend service as a global resource.
Adds an instance group named example-instance-group in zone us-central1-a as a backend to the backend service.
Terminal
gcloud compute backend-services add-backend example-backend-service --instance-group=example-instance-group --instance-group-zone=us-central1-a --global
Expected OutputExpected
Updated [https://www.googleapis.com/compute/v1/projects/my-project/global/backendServices/example-backend-service].
--instance-group - Specifies the instance group to add.
--instance-group-zone - Specifies the zone of the instance group.
--global - Indicates the backend service is global.
Creates a URL map that routes all traffic to the backend service with Cloud CDN enabled.
Terminal
gcloud compute url-maps create example-url-map --default-service=example-backend-service
Expected OutputExpected
Created [https://www.googleapis.com/compute/v1/projects/my-project/global/urlMaps/example-url-map].
Creates an HTTP proxy that uses the URL map to route requests to the backend service.
Terminal
gcloud compute target-http-proxies create example-http-proxy --url-map=example-url-map
Expected OutputExpected
Created [https://www.googleapis.com/compute/v1/projects/my-project/global/targetHttpProxies/example-http-proxy].
Creates a global forwarding rule to send incoming HTTP traffic on port 80 to the HTTP proxy.
Terminal
gcloud compute forwarding-rules create example-http-forwarding-rule --global --target-http-proxy=example-http-proxy --ports=80
Expected OutputExpected
Creating forwarding rule...done. NAME REGION IP_ADDRESS IP_PROTOCOL PORT_RANGE TARGET example-http-forwarding-rule global 34.123.45.67 TCP 80 https://www.googleapis.com/compute/v1/projects/my-project/global/targetHttpProxies/example-http-proxy
--global - Creates a global forwarding rule.
--ports - Specifies the port to listen on.
Checks the backend service details to verify Cloud CDN is enabled and caching policy is set.
Terminal
gcloud compute backend-services describe example-backend-service --global
Expected OutputExpected
cdnPolicy: cacheMode: CACHE_ALL_STATIC signedUrlCacheMaxAgeSec: 3600 enableCdn: true name: example-backend-service protocol: HTTP portName: http timeoutSec: 30
--global - Specifies the backend service is global.
Key Concept

If you remember nothing else from this pattern, remember: enabling Cloud CDN on a backend service caches your content at Google’s edge locations to speed up delivery worldwide.

Common Mistakes
Not enabling CDN when creating the backend service.
Without enabling CDN, your content won't be cached and users won't get faster delivery.
Always use the --enable-cdn flag when creating or updating your backend service.
Adding backend instance groups without specifying the correct zone.
The backend service won't connect properly to your instance group, causing traffic failures.
Use the --instance-group-zone flag with the correct zone when adding backends.
Skipping verification of backend service settings after creation.
You might miss misconfigurations like CDN not being enabled or wrong cache policies.
Run gcloud compute backend-services describe to confirm settings.
Summary
Create a backend service with Cloud CDN enabled using gcloud commands.
Add your instance group as a backend to serve your content.
Set up URL maps, HTTP proxies, and forwarding rules to route traffic through Cloud CDN.
Verify your backend service to ensure CDN is active and caching policies are correct.