<p>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 <strong>statefulset</strong> YAML file in a way that when a pod restarts, it will get a new <em>Volume</em> and the old volume will be deleted.</p>
<p>I know I can delete volumes manually, but is there any ways to tell Kubernetes to do this via statefulset yaml?</p>
<p>Here is my Yaml:</p>
<pre><code>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: [&quot;ReadWriteOnce&quot;]
        storageClassName: &quot;default&quot;
        resources:
          requests:
            storage: 7Gi
</code></pre>

toto
<p>If you want your data to be deleted when the pod restarts, you can use an ephemeral volume like <a href="https://kubernetes.io/docs/concepts/storage/volumes/#emptydir" rel="nofollow noreferrer">EmptyDir</a>.</p>
<blockquote>
<p>When a Pod is removed/restarted for any reason, the data in the emptyDir is deleted forever.</p>
</blockquote>
<p>Sample:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: nginx # has to match .spec.template.metadata.labels
  serviceName: &quot;nginx&quot;
  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: {}

</code></pre>
<p><strong>N.B.:</strong>
By default, <code>emptyDir</code> 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 <code>emptyDir.medium</code> field to &quot;Memory&quot; to tell Kubernetes to mount a <code>tmpfs</code> (RAM-backed filesystem) for you instead.</p>
