Tuesday, January 12, 2021

Using Google Cloud CDN for a Kubernetes Deployment


 


This post reviews the step to use Google Cloud CDN for a kubernetes deployment on Google Kubernetes Engine (GKE). The CDN configuration is base on the kubernetes Ingress on GKE, and greatly simplifies the process on the CDN setup.


First configure our ingress ad part of the kubernetes deployment:


ingress.yaml

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "false"
kubernetes.io/ingress.global-static-ip-name: "app-ip"
spec:
tls:
- secretName: ingress-secret
rules:
- host: app.com
http:
paths:
- path: /app/service1/*
backend:
serviceName: service1
servicePort: 80
- path: /app/service2/*
backend:
serviceName: service2
servicePort: 80


Once the ingress is deployed (it takes several minutes), it automatically creates backends for each service. In this example, as we have 2 services, it will creates two backends.


Now, let configure CDN for service2 using the gcloud CLI.



backend=$(kubectl get ingress ingress -o json | jq -j '.metadata.annotations."ingress.kubernetes.io/backends"' | jq '.' |grep service2 | cut -d\" -f2)
gcloud compute backend-services update --global ${backend} --enable-cdn --cache-mode=CACHE_ALL_STATIC



Notice that any CDN change take some time to apply, starting from minutes, and up to hours. 

The CDN starts serving our requests for service2. We will be able to view statistics for the served requests in GCP console, under Network Services, Cloud CDN. Note that 


A great method to check if a response is arriving from a CDN, is curl. We can run 

curl -v http://myapp.com

and then look for the age header which is added by the CDN, and indicates how many seconds had passed since the CDN first cached the response.

We can also check the performance of accessing the service from an end client using the ab tool:



sudo apt install apache2-utils
ab -c 2 -n 10 http://myapp.com/app/service2



One more item that requires our attention is the cache invalidation. As part of our CI/CD process we would like to invalidate the CDN cache, to ensure that a new version is downloaded to the clients. This can be done using the following CLI:



urlMap=$(gcloud compute url-maps list | grep app | awk '{print $1}')
gcloud compute url-maps invalidate-cdn-cache ${urlMap} --path "/*"



Additional information can be found at:



No comments:

Post a Comment