3 The purpose of this terraform template is to provision multi-node kubernetes cluster on AWS using microk8s. MicroK8s offers a light weight kubernetes environment for edge use cases.
7 1. Install terraform - https://www.terraform.io/downloads.html
9 (a)Downlaod the zip file based on the server type.
10 (b)Unzip the file to get the terraform binary.
11 (c)Currently supported ubuntu version is 18.04
13 2. IAM Access Keys - Permissions required for running the template - AmazonEC2FullAccess
14 3. PEM file for the AWS Key used in the terraform template
15 NOTE : Replace fields in the variable.tf file with your corresponding values
17 In order for Terraform to be able to create resources in your AWS account, you will need to configure the AWS credentials. One of the easiest ofwhich is to set the following environment variables:
19 export AWS_ACCESS_KEY_ID=(your access key id)
20 export AWS_SECRET_ACCESS_KEY=(your secret access key)
22 The credentials can also be set in the variable.tf file.
24 variable "access_key" {
25 description = "access_key"
29 variable "secret_key" {
30 description = "secret_key"
37 The template contains main.tf file, variable.tf file, pem file (add your pem file here) and worker_user_data.tmpl
38 You can move the pem file to the the directory where this template resides or you can change the location of the pem file in the main.tf file.
42 The first step to using Terraform is typically to configure the provider(s) you want to use.
43 This tells Terraform that you are going to be using the AWS provider and that you wish to deploy your infrastructure in the us-east-2 region.
46 region = var.aws_region
49 The user_data installs the microk8s inside the EC2 instance.
53 apt update -y >> microk8s_install.log
54 apt install snapd -y >> microk8s_install.log
55 snap install core >> microk8s_install.log
56 export PATH=$PATH:/snap/bin
57 snap install microk8s --classic >> microk8s_install.log
58 microk8s status --wait-ready
59 microk8s enable dns >> microk8s_install.log
60 microk8s add-node > microk8s.join_token
61 microk8s config > configFile
63 Since terraform does not wait until the user_data is executed, we exec into the instace by using the 'remote-exec' type provisioner and add the following script. This script will make terraform wait util microk8s.join-token file is created.
65 provisioner "remote-exec" {
66 inline = ["until [ -f /microk8s.join_token ]; do sleep 5; done; cat /microk8s.join_token"]
69 For testing purposes, we create an 'ALLOW ALL' ingress and egress rule security group.
73 The provider and the resource blocks in the main.tf file can be configured by changing the values in variables.tf file.
74 For example, if you want to change the aws_instace type from t2.small to t2.micro, replace the value here in this block.
75 variable "aws_instance" {
77 description = "instance_type"
80 Other resource specific values like aws_region, aws_ami, vpc and the subenet can also be changed the same way by editing the variable.tf file.
85 To create a master node with microk8s, run the following commands.
90 Once the worked nodes are created, it will be connected to the master. A multi-node k8s cluster will be provisioned with calico CNI.