Rancher Kubernetes Engine (RKE) on GCP using Terraform and Terragrunt

Source Code

The source codes relate to this article are kept publicly in the GitHub repository here — https://github.com/its-knowledge-sharing/gcp-rke-terraform. I would recommend to clone the codes and read them at the same time you read this article.


We assume that you already have a GCE instance that will be used as a workstation. The easiest way to have it is by following the previous article I wrote — GCP Infrastructure as Code with Terraform and Terragrunt and we will get one as shown in the picture below.

How to run the code?

To SSH to the workstation instance we can do by clicking the SSH button as shown in the picture below.

gcloud auth login
gcloud auth application-default login
./rke-cluster.bash init
./rke-cluster.bash apply
export KUBECONFIG=$(pwd)/kubeconfig

Wrapper script

For the simplicities, I created a wrapper script rke-cluster.bash that will internally creates the public/private key files (needed for provisioning the RKE cluster), invoking the Terragrunt command and finally export the kubeconfig file from Terraform variable.

Files Structure

  • 00–1-sa, the code to create service account and role for the GCE instances.
  • 00–2-firewall, the code to configure the firewall rules for the workstation GCE and the master nodes + worker nodes to make them connectable. In real life, we may limit the ports to only what we actually need.
  • 01–1-gce-nodes, the code to create the GCE instances that use the custom Ubuntu as OS image. Later these GCEs will be used to perform the master and worker nodes role in the RKE cluster.
  • 02–1-rke-cluster, the code to create the RKE cluster from the GCE instances created by 01–1-gce-nodes.
  • modules/gce, the internal module for creating GCE instance.
  • terragrunt.hcl, this is the shared configuration file uses for configuring Terraform codes in the folders mentioned above.

Program interface

The idea of the configuration is to put the configurable items in a single file here terragrunt.hcl. Later we will demonstrate the simple operations like adding new worker nodes and removing the worker nodes from the RKE cluster just by modifying terragrunt.hcl.

Remove a worker node

Assume that we want to remove a worker node rke-worker-05 from the cluster. We can do this by modifying an object in the worker_nodes array in the terragrunt.hcl.

./rke-cluster.bash apply
./rke-cluster.bash apply

Add a worker node

Assume that we want to add a worker node rke-worker-05 to the cluster (we previously removed it). We can do this by just adding the object below back, or change from “unregistered” to “registered” if it already exists. Yes, we need to call the command below for the changes to be applied.

./rke-cluster.bash apply


If everything is done and we want remove all we created earlier, we can simply do it by just by calling “./rke-cluster.bash destroy” command.


Congratulation!!! if you’ve read the entire article and it is able to help you solve your issues. You can support me by:

  • Follow me.
  • Share my articles.
  • Buy me a coffee via ADA address below if you want.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store