Placing Files In A Kubernetes Persistent Volume Store On GKE

6/5/2018

I am trying to run a Factorio game server on Kubernetes (hosted on GKE).

I have setup a Stateful Set with a Persistent Volume Claim and mounted it in the game server's save directory.

I would like to upload a save file from my local computer to this Persistent Volume Claim so I can access the save on the game server.

What would be the best way to upload a file to this Persistent Volume Claim?

I have thought of 2 ways but I'm not sure which is best or if either are a good idea:

  • Restore a disk snapshot with the files I want to the GCP disk which backs this Persistent Volume Claim
  • Mount the Persistent Volume Claim on an FTP container, FTP the files up, and then mount it on the game container
-- Noah Huppert
docker
google-cloud-platform
google-kubernetes-engine
kubernetes

3 Answers

6/7/2018

It turns out there is a much simpler way: The kubectl cp command.

This command lets you copy data from your computer to a container running on your cluster.

In my case I ran:

kubectl cp ~/.factorio/saves/k8s-test.zip factorio/factorio-0:/factorio/saves/

This copied the k8s-test.zip file on my computer to /factorio/saves/k8s-test.zip in a container running on my cluster.

See kubectl cp -h for more more detail usage information and examples.

-- Noah Huppert
Source: StackOverflow

8/21/2018

You can create data-folder on your GoogleCloud:

gcloud compute ssh <your cloud> <your zone>
mdkir data

Then create PersistentVolume:

kubectl create -f hostpth-pv.yml

kind: PersistentVolume
apiVersion: v1
metadata:
  name: pv-local
  labels:
    type: local
spec:
  storageClassName: local
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/home/<user-name>/data"

Create PersistentVolumeClaim:

kubectl create -f hostpath-pvc.yml

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: hostpath-pvc
spec:
  storageClassName: local
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  selector:
    matchLabels:
      type: local

Then copy file to GCloud:

gcloud compute scp <your file> <your cloud> <your zone> 

And at last mount this PersistentVolumeClaim to your pod:

...
      volumeMounts:
       - name: hostpath-pvc
         mountPath: <your-path>
         subPath: hostpath-pvc  
  volumes:
    - name: hostpath-pvc
      persistentVolumeClaim:
        claimName: hostpath-pvc

And copy file to data-folder in GGloud:

  gcloud compute scp <your file> <your cloud>:/home/<user-name>/data/hostpath-pvc <your zone>
-- Andrey
Source: StackOverflow

6/5/2018

You can just use Google Cloud Storage (https://cloud.google.com/storage/) since you're looking at serving a few files.

The other option is to use PersistenVolumeClaims. This will work better if you're not updating the files frequently because you will need to detach the disk from the Pods (so you need to delete the Pods) while doing this.

You can create a GCE persistent disk, attach it to a GCE VM, put files on it, then delete the VM and bring the PD to Kubernetes as PersistentVolumeClaim. There's doc on how to do that: https://cloud.google.com/kubernetes-engine/docs/concepts/persistent-volumes#using_preexsiting_persistent_disks_as_persistentvolumes

-- AhmetB - Google
Source: StackOverflow