<p>I’m trying to setup preview environments for my pull requests. Each environment needs its own prepopulated database.</p>

<p>My seed database is about 15GB.</p>

<p>I have a process to bootstrap a MySQL image and copy the <code>/var/lib/mysql</code> contents to a PVC volume (I also have this in a tarball).</p>

<p>I need to find a way to make new PVC which are populated with this data. To me I see a few options:</p>

<ol>
<li>Clone an existing PVC for my new deployment and use that</li>
<li>do some backup/restore process to make a new PVC from the old</li>
<li>Make a new PVC and populate it with a tarball</li>
</ol>

<p>I'm struggling to get any of these to work on GKE. Has anyone managed to achieve the above? I can't mount in the sql file as it simply takes too long to create the database from it - I need to mount in the database files directly.</p>

<p>I spent some time trying to get the CSI drivers working, but it seems I can't find a reasonable how-to guide.</p>

<p>Using advice from @yvesonline
I was able to achieve option 1 above.</p>

<ol>
<li>After my original volume has been populated, take a snapshot</li>
</ol>

<pre><code>gcloud compute disks snapshot [PD-name] --zone=[zone] --snapshot-names=mysql-seed-snapshot-21022020 --description="Snapshot of the /var/lib/mysql folder"
</code></pre>

<ol start="2">
<li>Create a new disk using the snapshot</li>
</ol>

<pre><code>gcloud compute disks create pvc-example-1 --source-snapshot=mysql-seed-snapshot-21022020 --zone=europe-west2-a
</code></pre>

<ol start="3">
<li>In the cluster create a new pv and pvc:</li>
</ol>

<pre><code>apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-demo
spec:
  persistentVolumeReclaimPolicy: Delete
  storageClassName: ""
  capacity:
    storage: 30Gi
  accessModes:
    - ReadWriteOnce
  gcePersistentDisk:
    pdName: pvc-example-1
    fsType: ext4
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-claim-demo
spec:
  # It's necessary to specify "" as the storageClassName
  # so that the default storage class won't be used, see
  # https://kubernetes.io/docs/concepts/storage/persistent-volumes/#class-1
  storageClassName: ""
  volumeName: pv-demo
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 30Gi
</code></pre>

<ol start="4">
<li>Then launch a new deployment using the pvc created above:</li>
</ol>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.7
        name: mysql
        env:
          # Use secret in real usage
        - name: MYSQL_ROOT_PASSWORD
          value: root
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: task-pv-storage
          mountPath: /var/lib/mysql
      volumes:
        - name: task-pv-storage
          persistentVolumeClaim:
           claimName: pv-claim-demo        
</code></pre>

<p>Once the volume cloning in K8s is more established in GKE this will be easier, but this solution will do for the mean time!</p>
