Skip to content

mw866/cloudflare-gke

Repository files navigation

Cloudflare x GKE - Proof of Concepts

Some proof-of-concepts that demonstrate how Cloudflare can work with GKE.

Getting started

Step 0: Prerequisites:

Step 1: Bootstrap the GKE environment with Terraform

1.1 Initialize Terraform terraform init

1.2 Plan Terraform and verify the plan terraform plan

(Optional) Set the variables as environment variables if you want want to entering them interactively every time you plan. For example,

# Read from the local user name
export TF_VAR_resource_prefix=$USER
# Read from gcloud default project id
export TF_VAR_gcp_project_id=$(gcloud config get-value project)

1.3 Apply the Terraform plan

terraform apply

It will takes > 10 minutes.

Step 2: Deploy your workloads to to the GKE environment with kubectl

2.1 Connect to the cluster Follow the instructions in GCP Console -> Kubernetes Engine -> Cluster -> Connect OR run

gcloud container clusters get-credentials <CLUSTER_NAME>

2.2 Run some kubectl to make sure it's configured correctly. kubectl config get-contexts

2.3 The foundation has been laid. The real fun starts from here...

Deployment Mode 1: Cloudflare Argo Tunnel in Sidecar Model with Cloudflare Load Balancer

cloudflared-sidecar.yaml

Architecture

cloudflared-sidecar

Integration with Cloudflare

  1. Login in to Cloudflare Argo Tunnel

cloudflared tunnel login

  1. Load the cert to the K8s secret store

kubectl create secret generic cloudflared-cert --from-file="$HOME/.cloudflared/cert.pem"

  1. Apply the deployment

kubectl apply -f cloudflared-sidecar.yaml

  1. Check Cloudflare dashboard > Traffic > Argo Tunnel

References

OOMKill Exit Code 137

Solution: remove resource limit My Container is terminated

Deployment Mode 2: Cloudflare with GKE Ingress with GKE External Load Balancer

gke-ingress.yaml

Architecture:

gke-ingress

Integration with Cloudflare

  1. Apply the deployment

kubectl apply -f gke-ingress.yaml

  1. Get the external IP address

kubectl get ingress

  1. Add it to Cloudflare DNS as an origin

References

Error 400 when creating ingress

Error during sync: error running load balancer syncing routine: loadbalancer default-cwang-httpbin-ingress--6029373544ea4799 does not exist: googleapi: Error 400: STANDARD network tier (the project's default network tier) is not supported: STANDARD network tier is not supported for global forwarding rule., badRequest

Just set the Network Service Tier to Premium. Using Network Service Tiers

Deployment Mode 3: Cloudflare Argo Tunnel in "Trailer" mode without Cloudflare Load Balancer or GCP Forwarding Rule

cloudflared-trailer.yaml

Architecture:

cloudflared-trailer

Integration with Cloudflare

  1. Login in to Cloudflare Argo Tunnel

cloudflared tunnel login

  1. Load the cert to the K8s secret store

kubectl create secret generic cloudflared-cert --from-file="$HOME/.cloudflared/cert.pem"

  1. Apply the deployment

kubectl apply -f cloudflared-trailer.yaml

  1. Check Cloudflare dashboard > Traffic > Argo Tunnel

Error - cloudflared listening to service

The service is not working.

kubectl run -it --rm --restart=Never alpine --image=alpine sh

If you don't see a command prompt, try pressing enter.
/ # wget -O- cwang-gke-int-lb-service
Connecting to cwang-gke-int-lb-service (10.112.9.183:80)
wget: can't connect to remote host (10.112.9.183): Connection refused

Service is not associated with the correct deployment?

kubectl describe  endpoints cwang-gke-int-lb-service
Name:         cwang-gke-int-lb-service
Namespace:    default
Labels:       app=cwang-gke-int-lb-app
Annotations:  <none>
Subsets:
Events:  <none>

References

Misc

Google Cloud

GKE and Kubernetes

Terraform

kubectl Cheatsheet

kubectl config current-context 
kubectl create -f FILE.yaml
kubectl apply -f FILE.yaml
kubectl delete -f FILE.yaml
kubectl get namespace
kubectl config get-contexts  
kubectl get po --output wide
kubectl describe pods
kubectl logs POD_NAME CONTAINER_NAME
kubectl top node
kubectl get ingress INGRESS_NAME --output yaml
kubectl exec POD_NAME   -- printenv | grep SERVICE   
kubectl exec -it POD_NAME -- /bin/bash
kubectl run -it --rm --restart=Never alpine --image=alpine sh
kubectl get endpoints
kubectl scale deploy tunnel --replicas=2

terraform Cheatsheet

terraform show gke.tf