0

I'm using azure aks to create a statefulset with volume using azure disk provisioner. I'm trying to find a way to write my statefulset YAML file in a way that when a pod restarts, it will get a new Volume and the old volume will be deleted.

I know I can delete volumes manually, but is there any ways to tell Kubernetes to do this via statefulset yaml?

Here is my Yaml:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: janusgraph
  labels:
    app: janusgraph
spec:
...
...
  template:
    metadata:
      labels:
        app: janusgraph
    spec:
      containers:
        - name: janusgraph
...
...
          volumeMounts:
            - name: data
              mountPath: /var/lib/janusgraph
          livenessProbe:
            httpGet:
              port: 8182
              path: ?gremlin=g.V(123).count()
            initialDelaySeconds: 120
            periodSeconds: 10
  volumeClaimTemplates:
    - metadata:
        name: data
      spec:
        accessModes: ["ReadWriteOnce"]
        storageClassName: "default"
        resources:
          requests:
            storage: 7Gi
toto
  • 1,197
  • 2
  • 15
  • 26
  • https://kubernetes.io/docs/concepts/storage/persistent-volumes/#delete this should help you delete the volume after use. It also deletes the disk. – Tarun Khosla Aug 10 '20 at 10:05
  • What do you mean by `restart`? – Matt Aug 11 '20 at 09:07
  • @HelloWorld one of kubernetes jobs is to bring up pod if it is down for some reason. this is all based of repliation value – toto Aug 11 '20 at 11:12
  • Sorry, I should have been more specific. Does deleting a pod also counts as restart in your case? Or do you mean only restart when container fails (no deleting a pod). @toto – Matt Aug 11 '20 at 11:17

1 Answers1

1

If you want your data to be deleted when the pod restarts, you can use an ephemeral volume like EmptyDir.

When a Pod is removed/restarted for any reason, the data in the emptyDir is deleted forever.

Sample:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: nginx # has to match .spec.template.metadata.labels
  serviceName: "nginx"
  replicas: 3 # by default is 1
  template:
    metadata:
      labels:
        app: nginx # has to match .spec.selector.matchLabels
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: nginx
        image: k8s.gcr.io/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
      volumes:
      - name: www
        emptyDir: {}

N.B.: By default, emptyDir volumes are stored on whatever medium is backing the node - that might be disk or SSD or network storage, depending on your environment. However, you can set the emptyDir.medium field to "Memory" to tell Kubernetes to mount a tmpfs (RAM-backed filesystem) for you instead.

Kamol Hasan
  • 12,218
  • 1
  • 37
  • 46
  • I can't use emptyDir because my storageClassName: "default" is actually Azure disk, which provide me with the 7Gi above. And also, seems like statefulset has this limitations: https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#limitations which means, I can't delete this Volume, only manually. or just use Deployment instead of statefulset – toto Aug 10 '20 at 15:20