Terraform- Kubernetes Deployment

Selvam Raju
3 min readMar 3, 2023

In this blog, we are going to learn how to do Kubernetes Deployment using the Terraform Module.

Kubernetes Deployment:

A Deployment provides declarative updates for Pods and ReplicaSets.

A deployment is an object in Kubernetes that helps you to manage a group of identical pods.

Kubernetes Deployment using Terraform Module:

First, let’s define the requirements for the module. For this example, we will assume that we want to deploy a simple Kubernetes deployment consisting of a single container.

Here’s the basic structure of the Terraform module:

.
├── main.tf
├── variables.tf
├── outputs.tf
└── README.md

Now, let’s start with the main.tf file.

main.tf file:

# Provider definition
provider "kubernetes" {
config_context_cluster = var.cluster_name
}

# Deployment
resource "kubernetes_deployment" "example_deployment" {
metadata {
name = var.deployment_name
}

spec {
replicas = var.replicas
selector {
match_labels = {
app = var.deployment_name
}
}

template {
metadata {
labels = {
app = var.deployment_name
}
}

spec {
container {
name = var.container_name
image = var.container_image
}
}
}
}
}

In the above code, we define the Kubernetes provider and create a Kubernetes deployment resource. The deployment resource specifies the number of replicas, the selector, and the template.

Next, let’s define the variables for the module in variables.tf file.

variables.tf file:

variable "cluster_name" {
description = "The name of the Kubernetes cluster"
}

variable "deployment_name" {
description = "The name of the deployment"
}

variable "replicas" {
description = "The number of replicas to run"
default = 1
}

variable "container_name" {
description = "The name of the container"
default = "example-container"
}

variable "container_image" {
description = "The container image to run"
}

In the above code, we define the input variables for the module. These variables will be used to configure the Kubernetes deployment.

Now, let’s define the outputs for the module in outputs.tf file.

outputs.tf file:

output "deployment_name" {
value = kubernetes_deployment.example_deployment.metadata[0].name
}

output "container_name" {
value = kubernetes_deployment.example_deployment.spec[0].template[0].spec[0].container[0].name
}

output "container_image" {
value = kubernetes_deployment.example_deployment.spec[0].template[0].spec[0].container[0].image
}

In the above code, we define the output values for the module. These outputs can be used by other Terraform modules or scripts.

Finally, let’s add a README.md file to the module to document how to use it.

README.md file:

# Kubernetes Deployment Terraform Module

This module deploys a simple Kubernetes deployment consisting of a single container.

## Usage

```hcl
module "example_deployment" {
source = "./path/to/module"

cluster_name = "example-cluster"
deployment_name = "example-deployment"
container_image = "nginx:latest"
}

Inputs

Outputs

| Name | Description |

| — — — |

👉 Follow me Selvam R and Cloudnloud Tech Community on LinkedIn for more insightful knowledge & resources

--

--