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

Posted

技术标签:

【中文标题】将文件放置在 GKE 上的 Kubernetes 持久卷存储中【英文标题】:Placing Files In A Kubernetes Persistent Volume Store On GKE 【发布时间】:2018-11-15 03:43:36 【问题描述】:

我正在尝试在 Kubernetes(托管在 GKE 上)上运行 Factorio 游戏服务器。

我已经设置了一个带有持久卷声明的有状态集,并将其安装在游戏服务器的保存目录中。

我想从我的本地计算机将保存文件上传到此永久卷声明,以便我可以访问游戏服务器上的保存。

将文件上传到此 Persistent Volume Claim 的最佳方式是什么?

我想到了 2 种方法,但我不确定哪种方法最好,或者哪种方法好:

将包含我想要的文件的磁盘快照还原到支持此持久卷声明的 GCP 磁盘 在 FTP 容器上挂载 Persistent Volume Claim,通过 FTP 上传文件,然后将其挂载到游戏容器上

【问题讨论】:

查看带有 Kuberentes 1.12(2018 年 9 月)的 CSI 的新快照/恢复功能:***.com/a/52570512/6309 【参考方案1】:

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

另一个选项是使用 PersistenVolumeClaims。如果您不经常更新文件,这会更好,因为您需要在执行此操作时将磁盘与 Pod 分离(因此您需要删除 Pod)。

您可以创建 GCE 永久性磁盘,将其附加到 GCE 虚拟机,在其上放置文件,然后删除虚拟机并将 PD 作为 PersistentVolumeClaim 带到 Kubernetes。有关于如何做到这一点的文档:https://cloud.google.com/kubernetes-engine/docs/concepts/persistent-volumes#using_preexsiting_persistent_disks_as_persistentvolumes

【讨论】:

嗨,艾哈迈德,感谢您的回答。我该如何将 Google Cloud Storage 连接到 Kubernetes pod?使用 Persistent Volume Claim 的方法正是我想要的。感谢您提供该指南的链接。【参考方案2】:

原来有一个更简单的方法: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

【讨论】:

我一直在寻找这样的东西!为什么这没有在备忘单上列出? kubernetes.io/docs/reference/kubectl/cheatsheet 这正是我一直在寻找的正确命令。谢谢,你救了我的命!【参考方案3】:

您可以在 GoogleCloud 上创建数据文件夹:

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"

创建 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

然后将文件复制到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 中的数据文件夹:

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

【讨论】:

cloud.google.com/sdk/gcloud/reference/compute/ssh

以上是关于将文件放置在 GKE 上的 Kubernetes 持久卷存储中的主要内容,如果未能解决你的问题,请参考以下文章