<p>I have created a new disk in Google Compute Engine.</p>

<pre><code>gcloud compute disks create --size=10GB --zone=us-central1-a dane-disk
</code></pre>

<p>It says I need to format it. But I have no idea how could I mount/access the disk?</p>

<pre><code>gcloud compute disks list
NAME                                               LOCATION       LOCATION_SCOPE  SIZE_GB  TYPE         STATUS
notowania-disk                                     us-central1-a  zone            10       pd-standard  READY
</code></pre>

<blockquote>
  <p>New disks are unformatted. You must format and mount a disk before it
  can be used. You can find instructions on how to do this at:</p>
  
  <p><a href="https://cloud.google.com/compute/docs/disks/add-persistent-disk#formatting" rel="noreferrer">https://cloud.google.com/compute/docs/disks/add-persistent-disk#formatting</a></p>
</blockquote>

<p>I tried instruction above but <code>lsblk</code> is not showing the disk at all</p>

<p>Do I need to create a VM and somehow attach it to it in order to use it? My goal was to mount the disk as a <a href="https://kubernetes.io/docs/concepts/storage/#gcepersistentdisk" rel="noreferrer">persistent GKE volume</a> independent of the VM (last time GKE upgrade caused recreation of VM and data loss)</p>

Wojtas.Zet
<p>Thanks for the clarification of what you are trying to do in the comments.</p>

<p>I have 2 different answers here.</p>

<hr>

<p>The first is that my testing shows that the <a href="https://kubernetes.io/docs/concepts/storage/#gcepersistentdisk" rel="nofollow noreferrer">Kubernetes GCE PD</a> documentation is exactly right, and the warning about formatting seems like it can be safely ignored.</p>

<p>If you just issue:</p>

<pre><code>gcloud compute disks create --size=10GB --zone=us-central1-a my-test-data-disk
</code></pre>

<p>And then use it in a pod:</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: nginx
    name: nginx-container
    volumeMounts:
    - mountPath: /test-pd
      name: test-volume
  volumes:
  - name: test-volume
    # This GCE PD must already exist.
    gcePersistentDisk:
      pdName: my-test-data-disk
      fsType: ext4
</code></pre>

<p>It will be formatted when it is mounted.  This is likely because the <code>fsType</code> parameter <a href="https://kubernetes.io/docs/concepts/storage/volumes/#csi" rel="nofollow noreferrer">instructs the system how to format the disk</a>.  You don't need to do anything with a separate GCE instance.  The disk is retained even if you delete the pod or even the entire cluster.  It is not reformatted on uses after the first and the data is kept around.</p>

<p>So, the warning message from gcloud is confusing, but can be safely ignored in this case.</p>

<hr>

<p>Now, in order to <em>dynamically</em> create a persistent volume based on GCE PD that isn't automatically deleted, you will need to create a new <code>StorageClass</code> that sets the Reclaim Policy to <code>Retain</code>, and then create a <code>PersistentVolumeClaim</code> based on that <code>StorageClass</code>.  This also keeps basically the entire operation inside of Kubernetes, without needing to do anything with gcloud.  Likewise, a similar approach is what you would want to use with a <code>StatefulSet</code> as opposed to a single pod, as described <a href="https://cloud.google.com/kubernetes-engine/docs/how-to/stateful-apps" rel="nofollow noreferrer">here</a>.</p>

<p>Most of what you are looking to do is described in <a href="https://cloud.google.com/kubernetes-engine/docs/concepts/persistent-volumes#dynamic_provisioning" rel="nofollow noreferrer">this GKE documentation about dynamically allocating PVCs</a> as well as the <a href="https://kubernetes.io/docs/concepts/storage/storage-classes/#the-storageclass-resource" rel="nofollow noreferrer">Kubernetes StorageClass documentation</a>.  Here's an example:</p>

<p>gce-pd-retain-storageclass.yaml:</p>

<pre><code>apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: gce-pd-retained
reclaimPolicy: Retain
provisioner: kubernetes.io/gce-pd
parameters:
  type: pd-standard
  replication-type: none
</code></pre>

<p>The above storage class is basically the same as the 'standard' GKE storage class, except with the <code>reclaimPolicy</code> set to Retain.</p>

<p>pvc-demo.yaml:</p>

<pre><code>apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-demo-disk
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: gce-pd-retained
  resources:
    requests:
      storage: 10Gi
</code></pre>

<p>Applying the above will dynamically create a disk that will be retained when you delete the claim.</p>

<p>And finally a demo-pod.yaml that mounts the PVC as a volume (this is really a nonsense example using nginx, but it demonstrates the syntax):</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: nginx
    name: nginx-container
    volumeMounts:
    - mountPath: /test-pd
      name: test-volume
  volumes:
  - name: test-volume
    persistentVolumeClaim:
      claimName: pvc-demo-disk
</code></pre>

<p>Now, if you apply these three in order, you'll get a container running using the PersistentVolumeClaim which has automatically created (and formatted) a disk for you.  When you delete the pod, the claim keeps the disk around.  If you delete the <em>claim</em> the StorageClass still keeps the disk from being deleted.</p>

<p>Note that the PV that is left around after this won't be automatically reused, as the data is still on the disk.  See <a href="https://kubernetes.io/docs/concepts/storage/persistent-volumes/#reclaiming" rel="nofollow noreferrer">the Kubernetes documentation</a> about what you can do to reclaim it in this case.  Really, this mostly says that you shouldn't delete the PVC unless you're ready to do work to move the data off the old volume.</p>

<p>Note that these disks will even continue to exist when the entire GKE cluster is deleted as well (and you will continue to be billed for them until you delete them).</p>
