<p>I have a volume <code>PersistentVolumeClaim</code> that I want to run snapshots for. I know there is <code>VolumeSnapshot</code> <a href="https://kubernetes.io/docs/concepts/storage/volume-snapshots/" rel="nofollow noreferrer">docs</a>. I think the best way to run periodic snapshots is to create a <code>CronJob</code> for that.</p>

<p>So I've created a docker image with <a href="https://github.com/kubernetes-client/python" rel="nofollow noreferrer">python k8s client</a> and my custom script. This way I'm able to run it whenever I want and I can access kube config and all resources directly from the pod.</p>

<pre class="lang-sh prettyprint-override"><code>FROM python:3.8-slim-buster
RUN apt-get -qq update &amp;&amp; apt-get -qq install -y git
COPY . .
RUN pip install --upgrade pip
RUN pip install git+https://github.com/kubernetes-client/python.git

</code></pre>

<p>The first problem I encountered, was that I was trying to use this <code>VolumeSnapshot</code> template using <code>apiVersion: snapshot.storage.k8s.io/v1beta1</code> </p>

<p>doing </p>

<pre class="lang-py prettyprint-override"><code>client = kubernetes.client.ApiClient()
utils.create_from_yaml(
        k8s_client=client,
        yaml_file='snapshot.yaml',
        verbose=True
    )
</code></pre>

<p>but it fails</p>

<pre><code>AttributeError: module 'kubernetes.client' has no attribute 'SnapshotStorageV1beta1Api'
</code></pre>

<p>And indeed I can't find it in <a href="https://github.com/kubernetes-client/python/tree/7763e37b7d8ca6e966716ad82d54f82f3e341cd4/kubernetes/client/api" rel="nofollow noreferrer">python client</a>, <a href="https://github.com/kubernetes-client/javascript/tree/master/src/gen/api" rel="nofollow noreferrer">js client</a> nor in the <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.18/" rel="nofollow noreferrer">official docs v1.18.</a> Maybe it's because it's in beta?</p>

<p>Then I tried writing some custom code. So I have</p>

<pre class="lang-py prettyprint-override"><code>def main():
    _configuration = kubernetes.client.Configuration()
    _client = kubernetes.client.ApiClient(_configuration)
    _storage_api = kubernetes.client.StorageV1beta1Api(_client)
    storages = _storage_api.get_api_resources()
</code></pre>

<p>But it returns a list of <a href="https://github.com/kubernetes-client/python/blob/v11.0.0/kubernetes/docs/V1StorageClass.md" rel="nofollow noreferrer">V1 storage class</a> and I can't find any way to create a snapshot from the response.</p>

<p>In my terminology, a snapshot is a copy-paste of a current state to anything else. Do you know how to achieve that?</p>

<p>I feel like I'm overengineering this but I don't want to use 3rd parties such as <a href="https://stash.run/" rel="nofollow noreferrer">stash</a>.</p>

<p>I'm running on GKE.</p>

Tom Wojcik
<p>That persistentVolumeClaim is mapped to a PersistentVolume, if you log into your gcloud console and on the Sidebar</p>

<p>Compute Engine -> Disks </p>

<p>You will get a list of all the GCE Disks that are being used on your project.</p>

<p>You will need to run </p>

<pre><code>kubectl get pvc --namespace YOUR_NAMESPACE
</code></pre>

<p>this will get you a list of all pvc, you need to figure out which one is the PVC you want, in order to get the disk name so you can view in the console you need to do something like this </p>

<pre><code>╰─ kubectl describe pvc NAME_OF_PVC  --namespace YOUR_NAMESPACE
Name:          NAME_OF_PVC
Namespace:     YOUR_NAMESPACE
StorageClass:  standard
Status:        Bound
Volume:        pvc-61e864b6-6fbf-4a36-80af-8a65e1588b58
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:      10Gi
Access Modes:  RWO
VolumeMode:    Filesystem
Mounted By:    &lt;none&gt;
Events:        &lt;none&gt;
</code></pre>

<p>Where your volume name would be <code>pvc-61e864b6-6fbf-4a36-80af-8a65e1588b58</code></p>

<p>Go back to the console and filter by this name and then click on it, you should be able to create a snapshot from there. </p>

<p>Now the correct approach would be to create a snapshot schedule and bind it to your disk as shown here (<a href="https://cloud.google.com/blog/products/compute/introducing-scheduled-snapshots-for-compute-engine-persistent-disk" rel="nofollow noreferrer">https://cloud.google.com/blog/products/compute/introducing-scheduled-snapshots-for-compute-engine-persistent-disk</a>).</p>

<p>When you are done creating the snapshot schedule you can edit the disk in the console and assign it whatever shanpshot schedule you created to your disk.</p>
