大网数据广告活动
服务器运维问答

Is there any way to attach an existing volume (one I just restored from a snapshot) to a PVC?

· 52CE 我爱测

I have a mongodb-replicaset created with the helm chart. The chart creates PVCs based on the StorageClass I provide. I annotate my volumes with a tag picked up by a cron job that snapshots the volumes. In the event I need to rest…

问题原文

原帖链接 · monty0 · CC BY-SA 4.0

I have a mongodb-replicaset created with the helm chart. The chart creates PVCs based on the StorageClass I provide. I annotate my volumes with a tag picked up by a cron job that snapshots the volumes.

In the event I need to restore from backup snapshots, say in another cluster, I know I can create a volume from the snapshot, but I don't know how to then turn that volume into the PVC that the StatefulSet is expecting and can restart from.

已采纳解答

原帖链接 · monty0 · CC BY-SA 4.0

I learned that a StatefulSet will look for PVCs with specific names. I figured this out from the documentation at https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/#limitations which says:

The storage for a given Pod must either be provisioned by a PersistentVolume Provisioner based on the requested storage class, or pre-provisioned by an admin.

And through experimentation I discovered that pre-provisioning just meant creating PersistentVolumeClaims with the expected names.

I was able to restore an EBS snapshot to a volume, create a PersistentVolume directly referencing the restored volume id, then create a PersistentVolumeClaim with the correct name. So for instance this mongo installation expects volumes named like datadir-pii-mongodb-replicaset-[0-2] and after restoring the EBS snapshot to a volume I use the following yaml:

apiVersion: v1
kind: PersistentVolume
metadata:
  labels:
    failure-domain.beta.kubernetes.io/region: us-west-2
    failure-domain.beta.kubernetes.io/zone: us-west-2a
  name: pv-a
  namespace: larksettings-pii
spec:
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 320Gi
  awsElasticBlockStore:
    fsType: xfs
    volumeID: aws://us-west-2a/vol-xxxxxxxxxxxxx
  storageClassName: mongo-xfs

---

apiVersion: v1
kind: PersistentVolumeClaim

metadata:
  labels:
    app: mongodb-replicaset
    release: pii
  name: datadir-pii-mongodb-replicaset-0
  namespace: larksettings-pii
spec:
    accessModes:
    - ReadWriteOnce
    resources:
      requests:
        storage: 320Gi
    storageClassName: mongo-xfs
    volumeName: pv-a

Be careful about availability zones. Since I spanned 3 zones I needed to restore the three snapshots into separate zones and make sure the PersistentVolume spec reflected that.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-05-24)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。