digitalocean / terraform-provider-digitalocean

Terraform DigitalOcean provider
https://registry.terraform.io/providers/digitalocean/digitalocean/latest/docs
Mozilla Public License 2.0
508 stars 278 forks source link

Kubernetes persistent volumes #180

Open aboeglin opened 5 years ago

aboeglin commented 5 years ago

Hi,

since Digital Ocean now got kubernetes clusters, I wondered if it would be possible to somehow get a digitalocean_volume resource to work with a kubernetes_persistent_volume_claim resource ? Or directly being ablee to create a volume by adding a persistent_volume_source ( https://www.terraform.io/docs/providers/kubernetes/r/persistent_volume.html ) to kubernetes_persistent_volumes.

At the moment I must use do something of the following shape to get a DO volume to be created by terraform provider :

# In a statefulset spec
volume_claim_template {
  metadata {
    name = "xyz"
  }

  spec {
    access_modes       = ["ReadWriteOnce"]
    storage_class_name = "do-block-storage"

    resources {
      requests {
        storage = "2Gi"
      }
    }
  }
}

The problem is that updating the storage size doesn't work at all, but I'm able to resize to volume from DO's dashboard. Maybe having a separate resource, or finding a way to connect the DO resource to a kubernetes resource would help ?

andrewsomething commented 5 years ago

Unfortunately, the DigitalOcean CSI plugin does not currently support resizing. You can follow that issue at:

https://github.com/digitalocean/csi-digitalocean/issues/106

I believe that your current approach should "just work" once resizing support is added to the CSI plugin.

danielsreichenbach commented 5 years ago

This would be extremely helpful. E.g. if this would be possible, I'd bake you a cake:

resource "digitalocean_volume" "assets" {
  region                  = "${var.digitalocean_region}"
  name                    = "${var.project}-storage"
  size                    = 100
  initial_filesystem_type = "ext4"
  description             = "Kubernetes storage volume"
}

resource "kubernetes_persistent_volume_claim" "assets" {
  metadata {
    name      = "${digitalocean_volume.assets.name}"
    namespace = "${var.namespace}"
  }

  spec {
    access_modes = ["ReadWriteOnce"]

    resources {
      requests {
        storage = "${digitalocean_volume.assets.size}Gi"
      }
    }

    storage_class_name = "do-block-storage"
  }
}

Even if resizing does not work, having a named, designated volume instead of a hashed name would be awesome.

Use cases e.g. would be volume claims in stateful sets, since you might want to keep these at all the time.

Side benefit of linking PVs to block storage volumes: one can then also use volume snapshots for PVs.

andrewsomething commented 5 years ago

@danielsreichenbach The DigitalOcean CSI driver does support creating and restoring from volume snapshots:

https://github.com/digitalocean/csi-digitalocean/tree/master/examples/kubernetes/snapshot

Unfortunately it doesn't look like support for management of Kubernetes native volume snapshots has landed in the Kubernetes Terraform provider yet.

More details on using Kubernetes volume snapshots:

https://kubernetes.io/blog/2018/10/09/introducing-volume-snapshot-alpha-for-kubernetes/

thorerik commented 4 years ago

Any status on this, as of DOKS 1.16.2-do.3, resizing is supported by CSI

zer0blockchain commented 4 years ago

This works for me, it will create a persistent volume in k8 using the existing volume identified by volumeHandle. Then I can create a persistent volume claim normally using the PV name.

https://github.com/digitalocean/csi-digitalocean/tree/master/examples/kubernetes/pod-single-existing-volume

kind: PersistentVolume
apiVersion: v1
metadata:
  name: volume-nyc1-01
  annotations:
    # fake it by indicating this is provisioned dynamically, so the system
    # works properly
    pv.kubernetes.io/provisioned-by: dobs.csi.digitalocean.com
spec:
  storageClassName: do-block-storage
  # by default, the volume will be not deleted if you delete the PVC, change to
  # "Delete" if you wish the volume to be deleted automatically with the PVC
  persistentVolumeReclaimPolicy: Delete
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  csi:
    driver: dobs.csi.digitalocean.com
    fsType: ext4
    volumeHandle: 1952d58a-c714-11e8-bc0c-0a58ac14421e
    volumeAttributes:
      com.digitalocean.csi/noformat: "true"

hoping this helps someone, it took me a day or so to figure this out :)