Reputation: 1035
I have a GKE cluster and a google compute engine persistent disk created by terraform. This disk is used to create a kubernetes persistent volume, which is then claimed and mounted to a container in a pod.
What I want to do is to persist some files and folders in that persistent disk so that when it is mounted, my container is able to access those files and folders. I have tried to research and it seems like the way to do it is to mount the disk to a compute engine or even a container, then copy and paste from local to there.
Is there a better way? Preferably using terraform.
This is how those resources are defined.
resource "google_compute_disk" "app" {
name = "app-${var.project_id}"
type = "pd-standard"
zone = var.zone
size = var.volume_size_gb
labels = {
environment = var.environment
}
}
resource "kubernetes_persistent_volume" "app" {
metadata {
name = "app-${var.project_id}"
}
spec {
access_modes = ["ReadWriteOnce"]
capacity = {
storage = "${var.volume_size_gb}Gi"
}
persistent_volume_source {
gce_persistent_disk {
pd_name = google_compute_disk.app.name
fs_type = "ext4"
}
}
storage_class_name = "standard"
}
}
resource "kubernetes_persistent_volume_claim" "app" {
metadata {
name = "app-${var.project_id}"
}
spec {
access_modes = ["ReadWriteOnce"]
resources {
requests = {
storage = "${var.volume_size_gb}Gi"
}
}
volume_name = kubernetes_persistent_volume.app.metadata.0.name
storage_class_name = "standard"
}
}
resource "kubernetes_deployment" "core_app" {
metadata {
name = "core-app"
labels = {
app = "core"
}
}
spec {
replicas = 1
selector {
match_labels = {
app = "core"
}
}
template {
metadata {
labels = {
app = "core"
}
}
spec {
volume {
name = "app-volume"
persistent_volume_claim {
claim_name = kubernetes_persistent_volume_claim.app.metadata.0.name
}
}
container {
name = "core-app"
image = "core-image:latest"
port {
container_port = 8080
}
volume_mount {
mount_path = "/mnt/extra-addons"
name = "app-volume"
sub_path = "addons"
}
readiness_probe {
tcp_socket {
port = "8069"
}
initial_delay_seconds = 5
period_seconds = 10
}
image_pull_policy = "Always"
}
}
}
}
}
Upvotes: 1
Views: 935
Reputation: 76018
The way is correct. If you want to initialize a disk:
Now about Terraform, I have an opinion and now real answer for you. Terraform is a IaC tool: Infrastructure as Code. That means: dedicated to the infrastructure.
In your case, you want to perform "software deployment". The K8S resources that you deploy, but also the disk preparation and mounting and so on. IMO, Terraform isn't the right tool for this. You have other tools, like Ansible, which are more suitable for software/os management.
Note: I'm sure with Terraform 0.13 or 0.14 you can create script that you can execute and achieve what you want, but I think that it isn't the right way.
Upvotes: 2