Here is a guide on building a Kubernetes (k8s) cluster on Google Cloud Platform (GCP). I chose Google because it is basically where the technology of Kubernetes started. It also has some nice automation around the creation of load balancers for ingress. This kind of functionality is ideal for developing web services and applications.
To get started, you need to create a Google Cloud Platform account and get started on the free tier. You'll get $300 of free credit and 365 days of free use. Quick note: running Kubernetes is not cheap, and it requires us to enable billing on our account. While we won't get charged as long as we have credit, running Kubernetes does eat through our credit if we run it for any significant amount of time. In fact, the primary motivation of this guide is reducing cost: you only want to be on the clock while you're actually developing.
Creating your project and service account
After you create your account, you'll need to go to the console and create a new project.
Next, you have the service account, which is required by Terraform to fire up your cluster. You need to create the service account with the proper permissions as described here.
- Go to the IAM and admin area of the dashboard and create a new service account.
- Under Project, give it Service Account Actor. Under Kubernetes Engine, give it Kubernetes Engine Admin. Under Compute Engine give it Compute Instance Admin (v1).
- Click the Furnish a new private key checkbox, and create the service account.
GCP will give you a JSON file with secret key material needed for Terraform, so store this in a safe place inside the Terraform project. (Make sure that the file is explicitly listed in your
.gitignore file.) If you lose the service account credentials file, you will have to delete and recreate it, as GCP will only give you the credentials once.
Defining your infrastructure
We use Terraform to describe our infrastructure. You can use my repository for a reference. Essentially we just create a simple cluster with three nodes in it. When you create the Kubernetes cluster, the following will happen:
- The API creates an instance template. This will define the VMs that will be part of the node pool for the cluster.
- The API creates an instance group. This manages a collection of VM instances.
- The API creates compute instances. We asked for three in our cluster, so we get three.
- The API creates disks. We asked for three VMs, so we much have three disks.
Scaling your infrastructure
As your organization grows, you will want to scale up. The way that you should do this of course is to edit your Terraform configuration. However, in a tricky situation, you can also go into the GCP dashboard, and edit the cluster itself. You can activate autoscaling and define minimum and maximum counts for your nodes. (But change this from the Kubernetes Engine, not the Instance Group area of the Compute Engine.)