Deploy Kubernetes Resources in Minikube cluster using Terraform

VAIBHAV HARIRAMANI
8 min readJun 3, 2024

--

In this blog, We will see how to automate a namespace and nginx application deployment creation in minikube using Terraform automation.

Kubernetes is an open-source orchestration system for Docker containers, initially developed by Google. It automates the deployment, scaling, and management of containerized applications. Kubernetes ensures the desired state of containers and manages their lifecycle across a cluster.

minikube quickly sets up a local Kubernetes cluster on macOS, Linux, and Windows. We proudly focus on helping application developers and new Kubernetes users.

Terraform is an open-source IaC (Infrastructure-as-Code) tool for configuring and deploying cloud infrastructure. It codifies infrastructure in configuration files that describe the desired state for your topology.

Introduction

Container based application workloads can be managed using Kubernetes(or k8s in short) and there are multiple methods available to create kubernetes clusters.
Cloud based k8s solutions like EKS, GKE and AKS can be used to deploy production grade clusters.

We can use minikube for Creating local k8s clusters for Simple POCs and learning purposes. For more details on minikube, refer docs.

K8s Concepts used in this demo

  • namespace is a logical grouping of different k8s workloads, secrets, etc. For example: we can group dev environment and prod environment configuration in different namespaces.
  • deployment is a kubernetes config declaration, in which we can declare different workloads and related configuration for the workload. Also we can create deployment under a namespace to logically differentiate the set of resources needed.
  • kube config file is created when we install the minikube and it has the information needed for creating local k8s clusters. Kube config contains the different clusters and contexts. minikube will be one of the context, which refers to the minikube cluster in kube config file
  • context refers to a set of parameters that contains information of Kubernetes cluster, a user, and a namespace

Installing & Configuring Terraform

Setting Up a Kubernetes Cluster with Terraform Here, we’ll demonstrate how to use Terraform to provision a Kubernetes cluster. We’ll cover the process of defining infrastructure resources, such as virtual machines or cloud instances and installing the necessary dependencies for a Kubernetes cluster. By the end of this section, you’ll have a fully operational Kubernetes cluster ready for deployment.

Step 1: Define Infrastructure as Code Create a Terraform configuration file (typically named main.tf) to define the infrastructure resources required for your Kubernetes cluster. These resources may include virtual machines, networks, load balancers, etc. Here's an example:

# main.tf
# Provider configuration
provider "your_provider" {
# Provider-specific configurations
}
# Other infrastructure resources as required

Step 2: Initialize the Terraform Configuration Run the terraform init command in your terminal or command prompt. This command initializes the Terraform working directory, downloads the necessary provider plugins, and configures the backend. Example:

$ terraform init

Step 3: Plan and Validate the Configuration Use the terraform plan command to generate an execution plan. This plan outlines the actions Terraform will take to create or modify resources. Verify that the plan matches your expectations. Example:

$ terraform plan

Step 4: Apply the Configuration Apply the Terraform configuration using the terraform apply command. This command creates or modifies the specified resources based on the configuration defined in your Terraform files. Example:

$ terraform apply

Configuring Kubernetes cluster

Deploying Kubernetes Resources with Terraform

In this section, we’ll explore how to leverage Terraform to deploy Kubernetes resources, including pods, services, deployments, and ingress rules.

You’ll learn how to define resource configurations using Terraform’s Kubernetes provider and manage their lifecycle through Terraform commands. Practical examples will illustrate the concepts, showcasing the deployment of a sample application onto the Kubernetes cluster.

Step 1 Start minikube

  • Use the command minikube start to start the local k8s minikube cluster
$ minikube start
😄 minikube v1.24.0 on Ubuntu 21.04
▪ KUBECONFIG=$USERHOME/.kube/config
🎉 minikube 1.26.0 is available! Download it: https://github.com/kubernetes/minikube/releases/tag/v1.26.0
💡 To disable this notice, run: 'minikube config set WantUpdateNotification false'
✨ Using the docker driver based on existing profile
👍 Starting control plane node minikube in cluster minikube
🚜 Pulling base image ...
🔄 Restarting existing docker container for "minikube" ...
🐳 Preparing Kubernetes v1.22.3 on Docker 20.10.8 ...
🔎 Verifying Kubernetes components...
▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
🌟 Enabled addons: default-storageclass, storage-provisioner
❗ /snap/bin/kubectl is version 1.24.2, which may have incompatibilites with Kubernetes 1.22.3.
▪ Want kubectl v1.22.3? Try 'minikube kubectl -- get pods -A'
🏄 Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

Step 2 Create Terraform code

Define Kubernetes Resources in Terraform In your Terraform configuration file (main.tf), define the Kubernetes resources you want to deploy. These resources can include pods, services, deployments, ingress rules, config maps, and more. Here's an example:

In the provider section, we need to declare the kube config file path and (minikube) context we will be using for our automation.

# main.tf
terraform {
required_providers {
kubernetes = {
source = "hashicorp/kubernetes"
version = "2.11.0"
}
}
}
# Provider configuration
provider "kubernetes" {
config_path = "~/.kube/config" # Path to your kubeconfig file
config_context = "minikube"
}


Kubernetes provider module in Terraform supports the same kubernetes configuration declaration arguments and parameters like metadata, spec etc.

  • So if you’re aware of creating kube config yaml, it will be easy for you to create terraform code using the config yaml experience. Otherwise, there will be a little bit learning curve is there, to create the kubernetes configuration
  • namespace will be defined as below,
resource "kubernetes_namespace" "demo" {
metadata {
name = "k8s-tf"
}
}
  • deployment will be defined as below, and it is getting created under the new namespace we are creating
resource "kubernetes_deployment" "demo" {
metadata {
name = "terraform-demo"
labels = {
test = "MydemoApp"
}
namespace = "k8s-tf"
}
  • In the deployment we are declaring a spec and it contains the replica definition and deployment template definition.
  • In this automation example, we are creating deployment for nginx application
  • Here’s the sample config snippet showing the spec definition,
spec {
replicas = 2
selector {
match_labels = {
test = "MydemoApp"
}
}
template {
metadata {
labels = {
test = "MydemoApp"
}
}

spec {
container {
image = "nginx:1.21.6"
name = "demo"
}

Step 2: Initialize the Terraform Configuration Run the terraform init command to initialize the Terraform configuration, ensuring that the required provider plugins are installed. Example:

$ terraform init

Step 3: Plan and Validate the Configuration Use the terraform plan command to generate a plan that outlines the changes Terraform will make to the Kubernetes cluster. Verify that the plan matches your expectations. Example:

$ terraform plan

Step 4: Apply the Configuration Apply the Terraform configuration using the terraform apply command. Terraform will create or modify the Kubernetes resources specified in your configuration file. Example

$ terraform apply

Step 5: Verify the Deployment After applying the configuration, you should verify that the Kubernetes resources have been deployed successfully. You can use the kubectl command-line tool to interact with the cluster and check the status of your resources. Example:

  • List namespace using — kubectl get ns
$ kubectl get ns k8s-tf  
NAME STATUS AGE
k8s-tf Active 52s
  • View deployments using — kubectl get deployment -n k8s-tf
$ kubectl get deployment -n k8s-tf
NAME READY UP-TO-DATE AVAILABLE AGE
terraform-demo 2/2 2 2 100s
  • Get the deployed pods using — kubectl get pods -n k8s-tf
  • Since we mentioned 2 replicas, we can see two pods deployed in the namespace
$ kubectl get pods -n k8s-tf
NAME READY STATUS RESTARTS AGE
terraform-demo-867678cb96-7ts9k 1/1 Running 0 2m22s
terraform-demo-867678cb96-hcht4 1/1 Running 0 2m22s

Clear down demo resources

  • Once we are done with the demo, we can clear down the deployed resources using terraform destroy command,
$ terraform destroy

Conclusion

In this blog, we gone through some basics k8s concepts and creating k8s resource automation in minikube.

Github Repo

But wait, there’s more to explore! In upcoming articles, I’ll delve into exciting topics like Ansible, Helm charts, and beyond. Your feedback and questions are invaluable, so feel free to share as I continue this learning adventure. Stay curious, and let’s keep building amazing things! 🚀

Thank You for reading

Please give 👏🏻 Claps if you like the blog.

GEEKY BAWA

just a silly Geek who love to seek out new technologies and experience cool stuff.

Do Checkout My other Blogs

Do Checkout My Youtube channel

If you want to get in touch and by the way, you know a good joke you can connect with me on Twitter or LinkedIn.

Thanks for reading!😄 🙌

Connect with me on Twitter and LinkedIn

Do find time check out my other articles and further readings in the reference section. Kindly remember to follow me so as to get notified of my publications.

Made with ❤️by Vaibhav Hariramani

Don’t forget to tag us

if you find this blog beneficial for you don’t forget to share it with your friends and mention us as well. And Don’t forget to share us on Linkedin, instagram, facebook , twitter, Github

More Resources

To learn more about these Resources you can Refer to some of these articles written by Me:-

Follow me

on Linkedin, instagram, facebook , twitter, Github

Happy coding ❤️ .

--

--

VAIBHAV HARIRAMANI

Hi there! I am Vaibhav Hariramani a Travel & Tech blogger who love to seek out new technologies and experience cool stuff.