kubernetes - 如何在 GKE 上克隆或预填充 PVC?

标签 kubernetes google-kubernetes-engine kubernetes-pvc

我正在尝试为我的拉取请求设置预览环境。每个环境都需要自己的预填充数据库。

我的种子数据库大约有 15GB。

我有一个引导 MySQL 镜像并将 /var/lib/mysql 内容复制到 PVC 卷的过程(我也在 tarball 中有这个)。

我需要找到一种方法来制作填充有这些数据的新 PVC。对我来说,我看到了几个选项:

  1. 为我的新部署克隆现有 PVC 并使用它
  2. 做一些备份/恢复过程以从旧的 PVC 制作新的 PVC
  3. 制作一个新的 PVC 并用 tarball 填充它

我正在努力让其中任何一个在 GKE 上运行。有没有人设法实现上述目标?我无法装载到 sql 文件中,因为从中创建数据库所花费的时间太长 - 我需要直接装载到数据库文件中。

我花了一些时间尝试让 CSI 驱动程序正常工作,但似乎找不到合理的操作指南。

最佳答案

使用来自@yvesonline 的建议 我能够实现上面的选项 1。

  1. 填充原始卷后,拍摄快照
gcloud compute disks snapshot [PD-name] --zone=[zone] --snapshot-names=mysql-seed-snapshot-21022020 --description="Snapshot of the /var/lib/mysql folder"
  1. 使用快照创建新磁盘
gcloud compute disks create pvc-example-1 --source-snapshot=mysql-seed-snapshot-21022020 --zone=europe-west2-a
  1. 在集群中新建pv和pvc:
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
  1. 然后使用上面创建的 pvc 启动新部署:
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        

一旦在 GKE 中建立了 K8s 中的卷克隆,这将变得更加容易,但同时此解决方案也可以!

关于kubernetes - 如何在 GKE 上克隆或预填充 PVC?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60334589/

相关文章:

deployment - 如何查看 k8s 部署历史?

kubernetes - ingress-nginx - 每个主机创建一个入口?或者将许多主机合并到一个入口并重新加载?

Kubernetes 多个 pvc 与每个 pod 的 statefulset 与所有 pod 的单个 pvc?

postgresql - 错误 : failed to prepare subPath for volumeMount "postgres-storage" of container "postgres"

kubernetes - 将PVC连接到具有多个副本的部署时,PVC实际如何工作?

kubernetes - 将 kubernetes Controller 管理器 "terminated-pod-gc-threshold"标志设置为较低值

kubernetes - 将 kubernetes 部署缩减至 0 并缩减至原始副本集数量

go - 如何使用 Go 从 kubernetes 获取日志?

google-cloud-platform - Kubernetes服务无法正常工作(计时)

docker - 谷歌云 - 从 GCR 部署为容器 - 端口未在 docker 容器中公开