在GKE上将文件存储在Kubernetes持久卷中

39
我正在尝试在Kubernetes上(托管在GKE上)运行Factorio游戏服务器。
我已经设置了一个Stateful Set,带有一个Persistent Volume Claim,并将其挂载在游戏服务器的保存目录中。
我想上传本地计算机上的保存文件到这个Persistent Volume Claim,以便可以在游戏服务器上访问该保存文件。
最佳的上传文件方法是什么?
我想到了两种方法,但我不确定哪种更好,或者它们是否是好主意:
- 恢复包含我想要的文件的磁盘快照到支持此Persistent Volume Claim的GCP磁盘上。 - 在FTP容器上挂载Persistent Volume Claim,上传文件,然后在游戏容器上挂载它。

1
请查看 Kubernetes 1.12(2018年9月)中针对 CSI 的新快照/恢复功能:https://dev59.com/Uqjja4cB1Zd3GeqP7DJc#52570512 - VonC
3个回答

61

事实证明有一种更简单的方法:使用kubectl cp命令。

该命令允许您将数据从计算机复制到在群集上运行的容器中。

在我的情况下,我运行了:

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

这将k8s-test.zip文件从我的电脑复制到运行在群集上的容器中的/factorio/saves/k8s-test.zip位置。

有关更详细的使用信息和示例,请参见kubectl cp -h


3
我一直在寻找像这样的东西!为什么这不在 Kubernetes 命令行工具速查表上列出来?https://kubernetes.io/docs/reference/kubectl/cheatsheet/ - chringel21
这条命令正是我一直在寻找的。谢谢,你救了我的命! - Aldi Unanto
如果您正在复制的文件正在被容器应用程序使用,该怎么办?比如说您需要用另一个数据库文件替换当前的文件?您必须先停止应用程序,但是停止应用程序也会停止容器。因此... - majorgear
如果您的源Pod挂载了持久卷并且数据库保存在其中,那么您可以停止Pod,并创建一个新的Pod(比如只是一个永远睡眠的通用Alpine容器),该容器挂载此持久卷。然后您可以从该Pod复制。但是,如果您的Pod不将文件放入持久卷中,则会遇到麻烦。因为一旦该Pod关闭,您将会失去这些文件。要么找到一种停止向数据库写入并触发磁盘保存的方法,要么进行数据库转储并将其复制出来并恢复到新的数据库中。 - Noah Huppert
我还需要一种方法在我的工作站和pv之间传输文件。我将pv挂载到dperson/samba pod上,并使用cifs共享进行传输。 - majorgear

12

您可以在Google Cloud上创建data-folder:

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

然后创建 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"

创建持久卷声明:
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

然后将文件复制到 GCloud:

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

并且最后将这个 PersistentVolumeClaim 挂载到您的 Pod 上:
...
      volumeMounts:
       - name: hostpath-pvc
         mountPath: <your-path>
         subPath: hostpath-pvc  
  volumes:
    - name: hostpath-pvc
      persistentVolumeClaim:
        claimName: hostpath-pvc

将文件复制到 GGloud 的 data 文件夹中:

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

https://cloud.google.com/sdk/gcloud/reference/compute/ssh - Andrey

3

如果你只需要提供一些文件,可以使用Google Cloud Storage (https://cloud.google.com/storage/)。

另一个选择是使用PersistenVolumeClaims。如果你不经常更新文件,则这种方法效果更好,因为在执行此操作时,您需要将磁盘从Pods中分离出来(因此需要删除Pods)。

您可以创建GCE持久磁盘,将其附加到GCE VM上,将文件放在其中,然后删除VM并将PD带到Kubernetes作为PersistentVolumeClaim。有关如何执行此操作的文档,请参见:https://cloud.google.com/kubernetes-engine/docs/concepts/persistent-volumes#using_preexsiting_persistent_disks_as_persistentvolumes


嗨Ahmet,感谢您的回答。我该如何将Google Cloud Storage连接到Kubernetes pod?使用Persistent Volume Claim的方法正是我正在寻找的。感谢您提供指南的链接。 - Noah Huppert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接