How to Create PV and PVC with Terraform

Selvam Raju
3 min readMar 30, 2023

Introduction:

Terraform is an open-source tool for building, changing, and versioning infrastructure safely and efficiently. Terraform modules provide a convenient way to encapsulate and reuse infrastructure code. In this blog post, we will focus on creating PV (Persistent Volume) and PVC (Persistent Volume Claim) using a Terraform module.

What are PV and PVC?

PV and PVC are Kubernetes resources used for persistent storage. PV (Persistent Volume) is a storage resource in Kubernetes that exists independently of any pod or node. PVC (Persistent Volume Claim) is a request for a specific amount of storage from a PV.

PV and PVC provide persistent storage to stateful applications like databases, messaging systems, and file servers.

Creating a Terraform module for PV and PVC:

To create a Terraform module for PV and PVC, we need to follow these steps:

Step 1: Create a new directory for the module and create two files, pv.tf, and pvc.tf.

mkdir pv-pvc-module
cd pv-pvc-module
touch pv.tf
touch pvc.tf

Step 2: Define the PV in the pv.tf file.

resource "kubernetes_persistent_volume" "pv_example" {
metadata {
name = "example-pv"
}
spec {
capacity {
storage = "1Gi"
}
access_modes = ["ReadWriteOnce"]
persistent_volume_reclaim_policy = "Retain"
storage_class_name = "standard"
host_path {
path = "/mnt/data"
}
}
}

In this example, we create a PV with a capacity of 1Gi, access mode of ReadWriteOnce, and a host path of /mnt/data. The PV will be retained even if the PVC is deleted.

Step 3: Define the PVC in the pvc.tf file.

resource "kubernetes_persistent_volume_claim" "pvc_example" {
metadata {
name = "example-pvc"
}
spec {
access_modes = ["ReadWriteOnce"]
resources {
requests {
storage = "1Gi"
}
}
storage_class_name = "standard"
}
}

In this example, we create a PVC with a capacity of 1Gi and an access mode of ReadWriteOnce. The PVC requests storage from the standard storage class.

Step 4: Define input variables in a variables.tf file.

variable "pv_name" {
description = "Name of the PV."
type = string
}

variable "pvc_name" {
description = "Name of the PVC."
type = string
}

variable "storage_size" {
description = "Storage size for the PV and PVC."
type = string
}

variable "access_mode" {
description = "Access mode for the PV and PVC."
type = string
}

variable "storage_class" {
description = "Storage class for the PV and PVC."
type = string
}

Step 5: Define default values for input variables in a defaults.tf file.

variable "pv_name" {
default = "example-pv"
}

variable "pvc_name" {
default = "example-pvc"
}

variable "storage_size" {
default = "1Gi"
}

variable "access_mode" {
default = "ReadWriteOnce"
}

variable "storage_class" {
default = "standard"
}

Step 6: Create a main.tf file to call the PV and PVC resources using input variables.

provider "kubernetes" {
config_path = "~/.kube/config"
}

module "pv_pvc" {
source = "./pv-pvc-module"

pv_name = var.pv_name
pvc_name = var.pvc_name
storage_size = var.storage_size
access_mode = var.access_mode
storage_class = var.storage_class
}

output "pv_name" {
value = module.pv_pvc.pv_name
}

output "pvc_name" {
value = module.pv_pvc.pvc_name
}

In this example, we use the Kubernetes provider to configure our connection to the Kubernetes cluster. We then call the pv-pvc-module and pass in input variables. Finally, we output the names of the created PV and PVC using the module output values.

Make sure to replace the config_path with the path to your Kubernetes configuration file. You can also customize the default values for input variables in a separate file or pass them in as command line arguments.

Thanks for reading the article, hope this helps !

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

--

--