A demo project to demonstrate how to create a Kubernetes private cluster in Google Kubernetes Engine (GKE) using terraform.io.
The scripts in this repository will create the following GCP resources (mostly) via Terraform:
- Create an Admin GCP project. Within it, create a service account
terraformand GCS bucketgs://<projectId>-terraform-state.- The Service Account to be used by Terraform to create resources across developmenet enviornments.
- The GCS bucket to be used for saving Terraform states.
- Create a development GCP project. Within it, create the following GCP resources
- A the VPC and a Subnet that will host the GKE cluster
- A GKE Cluster running Workload Identity with no public IPs on either the API or the worker nodes.
- Nodepools hosting the working nodes
- A Cloud NAT router to provide egress access to nodes and pods
- A dedicated GCE instance acting as jumphost to enable users (like admins and developers) to use SSH Tunneling to access the cluster.
- Services running within
podsaccess other GCP APIs via Workload Identity.
- Install Google Cloud SDK and Terraform using Homebrew:
brew update brew install Caskroom/cask/google-cloud-sdk brew install terraform - This project assumes that your account have enough permissions that allows you to create and destroy projects in the organization.
- This project assumes you already have a Billing account set up.
- You may need to accept terms under GCP Privacy & Security
Google Cloud Storage, Compute and Kubernetes Engine are billable components.
-
Run the
./scripts/init_admin_project.shto create a GCP admin project../scripts/init_admin_project.sh -P <admin-project-id> -B <your_billing_account_id> -S <path-to-save-service-account-key> [-h]Example:
./scripts/init_admin_project.sh -P b-demo-admin -B 016959-9E26A7-5FE28B -S "/Users/agni/tf-admin.json"Note: this script must be run by a human with admin rigths for the project_id.
You can find the value of <your_billing_account_id> using the following commands:gcloud alpha billing accounts listOnce the admin project is created:
- The script will create a GCS bucket
gs://<projectId>-terraform-statewithin the admin project -- to be used for saving Terraform states. - The script will create a Service Account
terraformwithin the admin project -- to be used by Terraform to create resources.
- The script will create a GCS bucket
-
Run
./scripts/init_app_project.shto create a GCP project to be used to host the GKE cluster../scripts/init_app_project.sh -P <project-id> -B <your_billing_account_id> -S <terraform service account email> [-h]Example:
./scripts/init_app_project.sh -P d-demo-dev -B 016959-9E26A7-5FE28B -S [email protected]Note: this script must be run by a human with admin rigths for the project_id.
Once the project is created:
- The script will provide the Terraform service account necessary permissions in the app project to create\update\delete resources on our behalf.
-
Run
./scripts/create_infra.shto provision the VPC, Subnet, GKE cluster, Jumphost and NAT using Terraform./scripts/create_infra.sh
Access to Kubernetes API is only possible via Jumphost by Identity Aware Proxy (IaP)
-
Run
./scripts/connect_cluster.shto establish connection. Once connected, run kubectl commands using the following syntax:HTTPS_PROXY=localhost:8888 <kubectl command>Example
❯ HTTPS_PROXY=localhost:8888 kubectl get nodes NAME STATUS ROLES AGE VERSION gke-private-cluster-nodepool-standard-5fab9583-dc4c Ready <none> 13m v1.24.8-gke.2000 gke-private-cluster-nodepool-standard-be3b76f5-j45z Ready <none> 13m v1.24.8-gke.2000 gke-private-cluster-nodepool-standard-fa4b0a5b-3qqc Ready <none> 13m v1.24.8-gke.2000
-
Run
./scripts/ssh_jumphost.shto SSH to the jumphost box:❯ ./scripts/ssh_jumphost.sh Linux private-cluster-jumphost 5.10.0-19-cloud-amd64 #1 SMP Debian 5.10.149-2 (2022-10-21) x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. ac7493@private-cluster-jumphost:~$ pwd /home/ac7493
To ensure the setup is correct, we will deploy a Wordpress app in the cluster. The Kubernetes yaml scripts are downloaded from here
-
Open
./k8s/secrets.yamland replace YOUR_PASSWORD with the password you want to use. Make sure the password is base64 encoded. -
Run
./scripts/deploy_wordpress.shto deploy WordPress app in the cluster./scripts/deploy_wordpress.sh Fetching cluster endpoint and auth data. kubeconfig entry generated for private-cluster. service/wordpress-mysql created persistentvolumeclaim/mysql-pv-claim created deployment.apps/wordpress-mysql created secret/mysql-pass created service/wordpress created persistentvolumeclaim/wp-pv-claim created deployment.apps/wordpress created NAME TYPE DATA AGE mysql-pass Opaque 1 12m NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE mysql-pv-claim Bound pvc-46d2af55-ef5e-4354-ab2a-e1dd8528f62e 20Gi RWO standard-rwo 12m wp-pv-claim Bound pvc-47fdc780-de6d-4260-bbc4-3183a15968eb 20Gi RWO standard-rwo 12m NAME READY STATUS RESTARTS AGE wordpress-55c9ff4b54-sc6l2 1/1 Running 0 12m wordpress-mysql-6946cdb7cd-7ljjb 1/1 Running 0 12m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE wordpress LoadBalancer 10.2.12.46 34.93.49.246 80:32398/TCP 12m Use the external IP in the browser to access the Wordpress site
-
Run
./scripts/destroy_wordpress.shto delete the Wordpress application from the cluster./scripts/destroy_wordpress.sh Fetching cluster endpoint and auth data. kubeconfig entry generated for private-cluster. service "wordpress-mysql" deleted persistentvolumeclaim "mysql-pv-claim" deleted deployment.apps "wordpress-mysql" deleted secret "mysql-pass" deleted service "wordpress" deleted persistentvolumeclaim "wp-pv-claim" deleted deployment.apps "wordpress" deleted -
Run
./scripts/destroy_infra.shto tear-down the VPC, Subnet, GKE cluster, Nodepools, Jumphost and NAT./scripts/destroy_infra.sh -
Run
./scripts/destroy_project.shto tear down the GCP projects./scripts/destroy_project.sh -P <project-id>Example:
./scripts/destroy_project.sh -P -a-demo-dev ./scripts/destroy_project.sh -P -a-demo-adminNote: this script must be run by a human with admin rigths for the project_id.
