We'll use Terraform to provision a Kubernetes cluster with Google Kubernetes Engine (GKE).
Most of this code is lifted straight from the
hashicorp/google Terraform provider documentation. This tutorial shows more detailed steps like setting up
gloud to authenticate with Google Cloud Platform (GCP). I'll only highlight the interesting parts here.
First we set up the
hashicorp/google Terraform provider to interact with Google Cloud Platform. We also include some provider configuration to specify the default region and project to which resources will be deployed.
Then we specify a cluster and node pool.
The first thing you'll notice is that we're specifying the location
us-east4-a (Ashburn, Virginia) for the cluster and node pool. Specifying a specific zone instead of just the region
us-east4 makes this a "zonal cluster". This is important because free tier credits can be applied to the cluster management fee of zonal clusters.
Regional clusters should be used for production workloads as they offer higher availability. However, a cluster management fee of $0.10/hour (~$73/month) is applied to regional clusters. Since this is a personal project, I'll take the cost savings over high availability.
We set the
node_count to provision 2 nodes. We're using
e2-standard-2 VMs with 2 vCPUs and 8GB of memory. These typically cost $48.91/month.
Spot provisioning discounts that price to $14.67/month. For this personal project, I don't care if my workloads are interrupted. In fact it's a welcome opportunity to explore the "self-healing" capabilities of Kubernetes. If a node gets restarted, Kubernetes should ensure that all workloads return to a healthy state. Since the cluster management fee is covered by the free tier, these VMs are the only cost of running the cluster. So the total cost for the cluster is $29.34/month.
Finally we set the
auto_upgrade arguments in our node pool to allow GKE to bounce unhealthy nodes for us and ensure they are running the latest version of the Kubernetes control plane.
terraform apply with these two files gets us a Kubernetes cluster.