There are a few different ways to set up Kubernetes deployments, pods, services, etc. Some of the key ways are with Kubernetes manifests or helm charts. Today we're going to see how to spin up Kubernetes resources using Terraform.

Prerequisites;

1. AKS (Azure Kubernetes) cluster, but you can use any Kubernetes cluster you like. You'll just need to change the provider and your authentication may be different. In either case, we simply need our .kubeconfig for Terraform to know where to deploy the resource.

2. AZ CLI downloaded and logged in.

3. An Azure account

4. An ACR (Azure Container Registry) or another container image registry.

The first thing you'll need to confirm is what Docker image you want to use for deployment and where that image is located. In my case, I have it in ACR (Azure Container Registry) so it can be accessed by my AKS cluster.

I'm going to use the latest Redis image that I pushed up to ACR. If you are not authenticated to your ACR, simply run az acr login --name name_of_your_registry to get access to the resources.

The next thing you need to do is authenticate to my AKS cluster. You'll want to run az aks get-credentials -n name_of_your_cluster -g _resource_group_name. That way the .kubeconfig is stored on localhost and you can interact with the Kubernetes cluster. Once authenticated you're able to see my Kubernetes nodes.

The big take-away here is that when you run the command to get your aks credentials, you're defaulting your kubernetes configuration to be your cluster in Azure. The authentication is happening because you're logged in with your AZ credentials to your subscription.

Now  take a look at the code.

Please Note: This code is found at - https://github.com/AdminTurnedDevOps/TheLifeOfAnEngineerBlog/tree/master/Terraform_Kubernetes_Resources

The first piece of code is a main configuration (main.tf).

provider "azurerm" {

version = "=1.38.0"

}


resource "kubernetes_deployment" "Redis" {

metadata {

name = var.name

labels = {

test = var.name

}

}


spec {

replicas = 3


selector {

match_labels = {

test = var.name

}

}


template {

metadata {

labels = {

test = var.name

}

}

spec {

container {

image = var.image_id

name  = var.name

}

}

}

}

}

Take a look at what's happening here.

The first thing is we're calling the azurerm provider. This allows us to communicate with Azure. It also defaults to using our credentials that we logged into AZ CLI with.

The second part is our kubernetes_deployment resource. This resource gives us the ability to create a Kubernetes deployment. There are a ton of other resources for Kubernetes found here: https://www.terraform.io/docs/providers/kubernetes/index.html

You'll also see some keywords var. These come from the variable configurations, which you'll take a look at now. The first configuration being variables.tf.

variable "name" {

type = string

description = "name of app for metadata"

}

variable "image_id" {

type = string

description = "image being used in your deployment"

}

The above shows what variables we are planning on passing in, the types, and descriptions. This is where our var.image_id and var.name variables come from in our main.tf.

Now take a look at the terraform.tfvars configuration. Think of any configuration ending with .tfvars as what is being passed in at runtime.

name = "redis-app"

image_id = "mjlreg.azurecr.io/r/redis:latest"

This is passing in the var.name and var.image_id values.

Now that you have the configurations, you're ready to start the terraform deployment. The first think you'll want to do is change directory (cd) to your directory that is holding all of your terraform configurations.

At the terminal run terraform init to initialize/stage the environment.

Now that you have initialized the environment, you're ready to run terraform plan to confirm everything is correct and ready to deploy.

Now that the plan has gone through, you're ready to run terraform apply to create the Kubernetes deployment.

Your Kubernetes deployment is now being created!