DEV Community

Cover image for  Terraform + GCP + MicroK8s
Anderson Gama
Anderson Gama

Posted on

Terraform + GCP + MicroK8s

MicroK8s allows you to specify the amount of join and the token that will be used, then it opens up the opportunity for some interesting things, just imagine easily provisioning in Terraform an infrastructure with a simple multi-node cluster?

I showed in a previous post on #Linkedin the configuration "#MicroK8s+#K9S+#Octant": https://www.linkedin.com/posts/andersongama_microk8sk9soctant-sudo-apt-y-install-activity-6697607973889425408-9Gm-

Based on that thought up there you would define the "cloud-init"/"user_data" of "control-ship" that during provisioning will have the IP "10.158.0.2" that it should have the following token "868f88ee477f893de65c99d906e767dcaf59ce10fe30795ef9b2d11af4faefa" that was generated with a:

$ date | sha256sum | cut -f 1 -d -
Enter fullscreen mode Exit fullscreen mode

Just out of curiosity if you were going to do it manually it would be:

$ sudo microk8s add-node --token-ttl 999 --token `date | sha256sum | cut -f 1 -d -`
Enter fullscreen mode Exit fullscreen mode

For the "cargo-ship" nodes to be created only after the "control-ship" in the provisioning of instances, just put "depends_on = [aws_instance.control_instance]" if it is AWS and "depends_on = [google_compute_instance.control_instance]" if it is GCP.

For the control-ship:

#cloud-config
#control-ship
runcmd:
 - sudo echo "DEBIAN_FRONTEND=noninteractive" >> /etc/environment
 - sudo source /etc/environment && source /etc/environment
 - sudo apt update --fix-missing
 - sudo apt -y snap snapd
 - sudo snap refresh
 - sudo snap install microk8s --classic
 - sudo microk8s status --wait-ready
 - sudo microk8s add-node --token-ttl 999 --token 868f88ee477f893de65c99d906e767dcaf59ce10fe30795ef9b2d11af4faefa
Enter fullscreen mode Exit fullscreen mode

For cargo-ship:

#cloud-config
#cargo-ship
runcmd:
 - sudo echo "DEBIAN_FRONTEND=noninteractive" >> /etc/environment
 - sudo source /etc/environment && source /etc/environment
 - sudo apt update --fix-missing
 - sudo apt -y snap snapd
 - sudo snap refresh
 - sudo snap install microk8s --classic
 - sudo microk8s status --wait-ready
 - sudo microk8s join 10.158.0.2:25000/868f88ee477f893de65c99d906e767dcaf59ce10fe30795ef9b2d11af4faefa
Enter fullscreen mode Exit fullscreen mode

Note: If you found it interesting, you can take the template for #GCP "https://github.com/smashse/iac-public/tree/master/IAC/GCP/template_terraform_gcp_microk8s" and execute it inside the folder:

$ sed -i "s/template/desiredname/g" *.*
Enter fullscreen mode Exit fullscreen mode

Instructions for setting up your environment with GCP on Linux: https://github.com/smashse/iac-public/tree/master/IAC/GCP

Top comments (0)