There is no fog, only Clouds

24 March 2023

Terraforming Linode LKE!

by Stefan Krantz

Terraform is an open-source infrastructure-as-code (IaC) tool that allows you to define, provision, and manage infrastructure resources such as servers, databases, networks, and cloud services in a declarative manner.

With Terraform, you can create a description of your desired infrastructure configuration in a simple and human-readable syntax, known as HashiCorp Configuration Language (HCL), or JSON. Then, Terraform will apply the configuration to create, modify, or delete resources based on the desired state.

Terraform is particularly useful in modern cloud computing environments where infrastructure is becoming increasingly dynamic and complex, and resources are provisioned and de-provisioned on-demand. It enables teams to manage infrastructure as code, apply version control, and automate the provisioning process, making it easier to maintain infrastructure and reduce manual errors. Terraform supports multiple cloud providers, including Amazon Web Services (AWS), Google Cloud Platform (GCP) and Linode.

This guide will walk you through step-by-step launching a Linode Kubernetes Engine (LKE) with Terraform.

 
 

Tools needed for this guide

1. Download and Install Terraform:

2. Generate Linode token and save it in a secure place

 
 

3. Download and Install kubectl

Terraform definition files

Create the following files in the folder

terraform {
  required_providers {
    linode = {
      source  = "linode/linode"
      version = "1.29.4"
    }
  }
}
//Use the Linode Provider
provider "linode" {
  token = var.token
}

 

variable "token" {
  description = "Your Linode API Personal Access Token. (required)"
}

variable "k8s_version" {
  description = "Kubernetes version"
}

variable "region" {
  description = "Linode Region"
}

 

k8s_version = "1.27"
region = "se-sto"

(note: List of available kubernetes versions can be found here) (list of regions can be found through API or CLI)

 

resource "linode_lke_cluster" "my-cluster" {
  label       = "lke-cluster"
  k8s_version = var.k8s_version
  region      = var.region
 
  pool {
    type  = "g6-standard-2"
    count = 1
  }

  # Prevent the count field from overriding autoscaler-created nodes
  lifecycle {
    ignore_changes = [
      pool.0.count
    ]
  }
}

//Export this cluster's attributes
output "kubeconfig" {
  value     = linode_lke_cluster.my-cluster.kubeconfig
  sensitive = true
}

output "api_endpoints" {
  value = linode_lke_cluster.my-cluster.api_endpoints
}

output "status" {
  value = linode_lke_cluster.my-cluster.status
}

output "id" {
  value = linode_lke_cluster.my-cluster.id
}

output "pool" {
  value = linode_lke_cluster.my-cluster.pool
}

(note: we are using the g6-standard-2 instance type, but the list of instance types are available through the API or CLI)

 
 

Lets run some terraform!

 
 

Test connection to kubernetes:

If successful, output you would see will be similar to this, showing 3 active nodes:

$ kubectl get nodes
NAME                            STATUS   ROLES    AGE     VERSION
lke103403-154758-643e56400ab1   Ready    <none>   5m26s   v1.25.4
lke103403-154758-643e56406fd7   Ready    <none>   5m28s   v1.25.4
lke103403-154758-643e5640d15b   Ready    <none>   5m46s   v1.25.4

 
 

Whats next?

Now that you have a cluster running, you can explore what to use it for! For example, you might explore the world of Helm here on packaged software ready to run in LKE!

 
 

You might want to wrap your cluster in a cloud firewall?

create file

(note: Keep in mind that recycling nodes in an LKE cluster will cause the nodes to be deleted and replaced. Node recycle is required during K8s version upgrades, which will be necessary at least once per year. If you recycle the nodes in your LKE cluster, the list of Linodes in the Firewall will need to be updated either manually or by re-running terraform apply.)

 
 

Stay tuned as I will add links to follow-up posts on specific usecases for LKE!

All The best! Stefan

tags: