이 페이지에서는 볼륨 스냅샷을 사용하여 Persistent Disk 스토리지를 백업 및 복원하는 방법을 보여줍니다.
자세한 내용은 Kubernetes 볼륨 스냅샷 정보를 참조하세요.
요구사항
GKE에서 볼륨 스냅샷을 사용하려면 다음 요구사항을 충족해야 합니다.
스냅샷을 지원하는 CSI 드라이버를 사용합니다. 트리 내 Persistent Disk 드라이버에서는 스냅샷을 지원하지 않습니다. 스냅샷을 만들고 관리하려면 기본
PersistentVolumeClaim(PVC)와 동일한 CSI 드라이버를 사용해야 합니다.Persistent Disk(PD) 볼륨 스냅샷의 경우 Compute Engine Persistent Disk CSI 드라이버를 사용합니다. Compute Engine Persistent Disk CSI 드라이버는 GKE 버전 1.18.10-gke.2100 이상 또는 버전 1.19.3-gke.2100 이상을 실행하는 새 Linux 클러스터에 기본적으로 설치됩니다. 기존 클러스터에서 Compute Engine Persistent Disk CSI 드라이버를 사용 설정할 수도 있습니다.
스냅샷을 지원하는 모든 CSI 드라이버 목록은 Kubernetes 문서에서 드라이버의 기타 기능 열을 참조하세요.
컨트롤 플레인 버전 1.17 이상을 사용합니다.
VolumeSnapshot에서 Compute Engine Persistent Disk CSI 드라이버를 사용하려면 GKE 버전 1.17.6-gke.4 이상을 사용합니다.
- 스냅샷에 사용할 기존
PersistentVolumeClaim이 있어야 합니다. 스냅샷 소스에 사용하는PersistentVolume은 CSI 드라이버로 관리되어야 합니다.PersistentVolume사양의driver: pd.csi.storage.gke.io또는filestore.csi.storage.gke.io에csi섹션이 있는지 확인하여 CSI 드라이버를 사용 중인지 확인할 수 있습니다. 다음 섹션에 설명된 대로 CSI 드라이버로PersistentVolume이 동적으로 프로비저닝된 경우 CSI 드라이버로 관리됩니다.
제한사항
Compute Engine의 모든 디스크 스냅샷 생성 제한사항도 GKE에 적용됩니다.
권장사항
GKE에서 Persistent Disk Volume 스냅샷을 사용할 때는 Compute Engine 디스크 스냅샷 권장사항을 따라야 합니다.
시작하기 전에
시작하기 전에 다음 태스크를 수행했는지 확인합니다.
- Google Kubernetes Engine API를 사용 설정합니다. Google Kubernetes Engine API 사용 설정
- 이 태스크에 Google Cloud CLI를 사용하려면 gcloud CLI를 설치한 후 초기화합니다. 이전에 gcloud CLI를 설치한 경우
gcloud components update명령어를 실행하여 최신 버전을 가져옵니다. 이전 gcloud CLI 버전에서는 이 문서의 명령어를 실행하지 못할 수 있습니다.
볼륨 스냅샷 만들기 및 사용
이 문서의 예시에서는 다음 태스크를 수행하는 방법을 보여줍니다.
PersistentVolumeClaim및Deployment를 만듭니다.Deployment에 사용되는PersistentVolume에 파일을 추가합니다.- 스냅샷을 구성할
VolumeSnapshotClass를 만듭니다. PersistentVolume의 볼륨 스냅샷을 만듭니다.- 테스트 파일을 삭제합니다.
PersistentVolume을 만든 스냅샷으로 복원합니다.- 복원이 완료되었는지 확인합니다.
볼륨 스냅샷을 사용하려면 다음 단계를 완료해야 합니다.
VolumeSnapshotClass객체를 만들어 스냅샷의 CSI 드라이버 및 삭제 정책을 지정합니다.VolumeSnapshot객체를 만들어 기존PersistentVolumeClaim의 스냅샷을 요청합니다.PersistentVolumeClaim에서VolumeSnapshot을 참조하여 볼륨을 해당 스냅샷으로 복원하거나 스냅샷을 사용하여 새 볼륨을 만듭니다.
PersistentVolumeClaim 및 Deployment 만들기
PersistentVolumeClaim객체를 만들려면 다음 매니페스트를my-pvc.yaml로 저장합니다.Persistent Disk
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: my-pvc spec: storageClassName: standard-rwo accessModes: - ReadWriteOnce resources: requests: storage: 1Gi이 예시에서는 Compute Engine Persistent Disk CSI 드라이버와 함께 기본적으로 설치된
standard-rwo스토리지 클래스를 사용합니다. 자세한 내용은 Compute Engine Persistent Disk CSI 드라이버 사용을 참조하세요.spec.storageClassName에는 지원되는 CSI 드라이버를 사용하는 모든 스토리지 클래스를 지정할 수 있습니다.매니페스트를 적용합니다.
kubectl apply -f my-pvc.yamlDeployment를 만들려면 다음 매니페스트를my-deployment.yaml로 저장합니다.apiVersion: apps/v1 kind: Deployment metadata: name: hello-app spec: selector: matchLabels: app: hello-app template: metadata: labels: app: hello-app spec: containers: - name: hello-app image: google/cloud-sdk:slim args: [ "sleep", "3600" ] volumeMounts: - name: sdk-volume mountPath: /usr/share/hello/ volumes: - name: sdk-volume persistentVolumeClaim: claimName: my-pvc매니페스트를 적용합니다.
kubectl apply -f my-deployment.yamlDeployment의 상태를 확인합니다.kubectl get deployment hello-appDeployment를 준비하는 데 다소 시간이 걸릴 수 있습니다. 다음과 비슷한 출력이 표시될 때까지 위의 명령어를 실행하면 됩니다.NAME READY UP-TO-DATE AVAILABLE AGE hello-app 1/1 1 1 2m55s
볼륨에 테스트 파일 추가
Deployment의Pods를 나열합니다.kubectl get pods -l app=hello-app출력은 다음과 비슷합니다.
NAME READY STATUS RESTARTS AGE hello-app-6d7b457c7d-vl4jr 1/1 Running 0 2m56sPod에 테스트 파일을 만듭니다.kubectl exec POD_NAME \ -- sh -c 'echo "Hello World!" > /usr/share/hello/hello.txt'POD_NAME을Pod이름으로 바꿉니다.파일이 있는지 확인합니다.
kubectl exec POD_NAME \ -- sh -c 'cat /usr/share/hello/hello.txt'출력은 다음과 비슷합니다.
Hello World!
VolumeSnapshotClass 객체를 만듭니다.
볼륨 스냅샷의 CSI 드라이버와 deletionPolicy를 지정하는 VolumeSnapshotClass 객체를 만듭니다. VolumeSnapshot 객체를 만들 때 VolumeSnapshotClass 객체를 참조할 수 있습니다.
다음 매니페스트를
volumesnapshotclass.yaml로 저장합니다.Persistent Disk
버전 1.21 이상을 실행하는 클러스터에는
v1API 버전을 사용합니다.apiVersion: snapshot.storage.k8s.io/v1 kind: VolumeSnapshotClass metadata: name: my-snapshotclass driver: pd.csi.storage.gke.io deletionPolicy: Delete예를 들면 다음과 같습니다.
driver필드는 CSI 드라이버에서 스냅샷을 프로비저닝하는 데 사용됩니다. 이 예시에서pd.csi.storage.gke.io에는 Compute Engine Persistent Disk CSI 드라이버가 사용됩니다.deletionPolicy필드는 바인딩된VolumeSnapshot객체가 삭제될 때VolumeSnapshotContent객체와 기본 스냅샷으로 수행할 작업을 GKE에 알려줍니다.VolumeSnapshotContent객체 및 기본 스냅샷을 삭제하려면Delete를 지정합니다.VolumeSnapshotContent및 기본 스냅샷을 보존하려면Retain을 지정합니다.커스텀 스토리지 위치를 사용하려면
storage-locations매개변수를 스냅샷 클래스에 추가합니다. 이 매개변수를 사용하려면 클러스터에서 버전 1.21 이상을 사용해야 합니다.apiVersion: snapshot.storage.k8s.io/v1 kind: VolumeSnapshotClass metadata: name: my-snapshotclass parameters: storage-locations: us-east2 driver: pd.csi.storage.gke.io deletionPolicy: Delete디스크 이미지를 만들려면
parameters필드에 다음을 추가합니다.parameters: snapshot-type: images image-family: IMAGE_FAMILYIMAGE_FAMILY를 원하는 이미지 계열 이름(예:preloaded-data)으로 바꿉니다.
매니페스트를 적용합니다.
kubectl apply -f volumesnapshotclass.yaml
VolumeSnapshot 만들기
VolumeSnapshot 객체는 기존 PersistentVolumeClaim 객체의 스냅샷에 대한 요청입니다. VolumeSnapshot 객체를 만드는 경우 GKE는 PersistentVolume 객체와 같은 클러스터의 리소스인 VolumeSnapshotContent 객체를 사용하여 자동으로 만들고 바인딩합니다.
다음 매니페스트를
volumesnapshot.yaml로 저장합니다.apiVersion: snapshot.storage.k8s.io/v1 kind: VolumeSnapshot metadata: name: my-snapshot spec: volumeSnapshotClassName: my-snapshotclass source: persistentVolumeClaimName: my-pvc매니페스트를 적용합니다.
kubectl apply -f volumesnapshot.yamlVolume스냅샷을 만들면 GKE가 클러스터에 해당VolumeSnapshotContent객체를 만듭니다. 이 객체는VolumeSnapshot객체의 스냅샷과 바인딩을 저장합니다.VolumeSnapshotContents객체와는 직접 상호작용하지 않습니다.GKE에서
VolumeSnapshotContents객체를 만들었는지 확인합니다.kubectl get volumesnapshotcontents출력은 다음과 비슷합니다.
NAME AGE snapcontent-cee5fb1f-5427-11ea-a53c-42010a1000da 55s
Volume 스냅샷 콘텐츠가 생성되면 VolumeSnapshotClass가 지정한 CSI 드라이버가 해당 스토리지 시스템에 스냅샷을 만듭니다. GKE가 스토리지 시스템에서 스냅샷을 만들어 클러스터의 VolumeSnapshot 객체에 결합하면 스냅샷을 사용할 준비가 된 것입니다. 다음 명령어를 실행하여 상태를 확인할 수 있습니다.
kubectl get volumesnapshot \
-o custom-columns='NAME:.metadata.name,READY:.status.readyToUse'
스냅샷을 사용할 수 있으면 다음과 비슷한 출력이 표시됩니다.
NAME READY
my-snapshot true
테스트 파일 삭제
만든 테스트 파일을 삭제합니다.
kubectl exec POD_NAME \ -- sh -c 'rm /usr/share/hello/hello.txt'파일이 더 이상 존재하지 않는지 확인합니다.
kubectl exec POD_NAME \ -- sh -c 'cat /usr/share/hello/hello.txt'출력은 다음과 비슷합니다.
cat: /usr/share/hello/hello.txt: No such file or directory
볼륨 스냅샷 복원
PersistentVolumeClaim의 VolumeSnapshot을 참조하여 기존 볼륨의 데이터가 포함된 새 볼륨을 프로비저닝하거나 스냅샷으로 캡처한 상태로 볼륨을 복원할 수 있습니다.
PersistentVolumeClaim의 VolumeSnapshot을 참조하려면 dataSource 필드를 PersistentVolumeClaim에 추가합니다. VolumeSnapshotContents가 디스크 이미지 또는 스냅샷을 참조하는지 여부에 관계없이 동일한 프로세스가 사용됩니다.
이 예시에서는 새 PersistentVolumeClaim에서 만든 VolumeSnapshot을 참조하고 새 클레임을 사용하도록 Deployment를 업데이트합니다.
다음과 같이 디스크 또는 이미지 스냅샷을 사용하고 있는지 확인합니다.
- 디스크 스냅샷: 스냅샷을 자주 생성하고 가끔 복원합니다.
- 이미지 스냅샷: 스냅샷을 자주 생성하고 자주 복원합니다. 또한 이미지 스냅샷은 디스크 스냅샷보다 생성 속도가 느릴 수 있습니다.
자세한 내용은 스냅샷 빈도 제한을 참조하세요. 스냅샷 유형을 알면 문제 해결이 필요할 때 도움이 됩니다.
VolumeSnapshot을 검사합니다.kubectl describe volumesnapshot SNAPSHOT_NAMEvolumeSnapshotClassName필드는 스냅샷 클래스를 지정합니다.kubectl describe volumesnapshotclass SNAPSHOT_CLASS_NAMEsnapshot-type매개변수는snapshots또는images를 지정합니다. 제공되지 않은 경우 기본값은snapshots입니다.스냅샷 클래스가 없는 경우(예: 스냅샷이 정적으로 생성된 경우)
VolumeSnapshotContents을 검사합니다.sh kubectl describe volumesnapshotcontents SNAPSHOT_CONTENTS_NAME출력의 스냅샷 핸들 형식은 다음과 같이 스냅샷 유형을 알려줍니다. *projects/PROJECT_NAME/global/snapshots/SNAPSHOT_NAME: 디스크 스냅샷projects/PROJECT_NAME/global/images/IMAGE_NAME: 이미지 스냅샷
다음 매니페스트를
pvc-restore.yaml로 저장합니다.Persistent Disk
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: pvc-restore spec: dataSource: name: my-snapshot kind: VolumeSnapshot apiGroup: snapshot.storage.k8s.io storageClassName: standard-rwo accessModes: - ReadWriteOnce resources: requests: storage: 1Gi매니페스트를 적용합니다.
kubectl apply -f pvc-restore.yaml새
PersistentVolumeClaim을 사용하도록my-deployment.yaml파일을 업데이트합니다.... volumes: - name: my-volume persistentVolumeClaim: claimName: pvc-restore업데이트된 매니페스트를 적용합니다.
kubectl apply -f my-deployment.yaml
스냅샷 복원 여부 확인
GKE가 업데이트된
Deployment에 대해 만드는 새Pod의 이름을 가져옵니다.kubectl get pods -l app=hello-app
테스트 파일이 있는지 확인합니다.
kubectl exec NEW_POD_NAME \
-- sh -c 'cat /usr/share/hello/hello.txt'
NEW_POD_NAME을 GKE가 만든 새 Pod의 이름으로 바꿉니다.
출력은 다음과 비슷합니다.
Hello World!
기존 스냅샷 가져오기
현재 클러스터 외부에서 만든 기존 볼륨 스냅샷을 사용하여 VolumeSnapshotContents 객체를 수동으로 프로비저닝할 수 있습니다. 예를 들어 다른 클러스터에서 만든 다른Google Cloud 리소스의 스냅샷으로 GKE의 볼륨을 채울 수 있습니다.
스냅샷 이름을 찾습니다.
Google Cloud 콘솔
Google Cloud CLI
다음 명령어를 실행합니다.
gcloud compute snapshots list출력은 다음과 비슷합니다.
NAME DISK_SIZE_GB SRC_DISK STATUS snapshot-5e6af474-cbcc-49ed-b53f-32262959a0a0 1 us-central1-b/disks/pvc-69f80fca-bb06-4519-9e7d-b26f45c1f4aa READY다음
VolumeSnapshot매니페스트를restored-snapshot.yaml로 저장합니다.apiVersion: snapshot.storage.k8s.io/v1 kind: VolumeSnapshot metadata: name: restored-snapshot spec: volumeSnapshotClassName: my-snapshotclass source: volumeSnapshotContentName: restored-snapshot-content매니페스트를 적용합니다.
kubectl apply -f restored-snapshot.yaml다음
VolumeSnapshotContent매니페스트를restored-snapshot-content.yaml로 저장합니다.snapshotHandle필드를 프로젝트 ID 및 스냅샷 이름으로 바꿉니다. 양방향 바인딩이 유효하려면volumeSnapshotRef.name과volumeSnapshotRef.namespace가 모두 이전에 만든VolumeSnapshot를 가리켜야 합니다.apiVersion: snapshot.storage.k8s.io/v1 kind: VolumeSnapshotContent metadata: name: restored-snapshot-content spec: deletionPolicy: Retain driver: pd.csi.storage.gke.io source: snapshotHandle: projects/PROJECT_ID/global/snapshots/SNAPSHOT_NAME volumeSnapshotRef: kind: VolumeSnapshot name: restored-snapshot namespace: default매니페스트를 적용합니다.
kubectl apply -f restored-snapshot-content.yaml다음
PersistentVolumeClaim매니페스트를restored-pvc.yaml로 저장합니다. Kubernetes 스토리지 컨트롤러는restored-snapshot이라는VolumeSnapshot을 찾은 다음PersistentVolume을 데이터 소스로 찾거나 동적으로 만들려고 시도합니다. 그런 다음 이 PVC를 포드에서 활용하여 복원된 데이터에 액세스할 수 있습니다.apiVersion: v1 kind: PersistentVolumeClaim metadata: name: restored-pvc spec: dataSource: name: restored-snapshot kind: VolumeSnapshot apiGroup: snapshot.storage.k8s.io storageClassName: standard-rwo accessModes: - ReadWriteOnce resources: requests: storage: 1Gi매니페스트를 적용합니다.
kubectl apply -f restored-pvc.yamlPersistentVolumeClaim을 참조하여 다음Pod매니페스트를restored-pod.yaml로 저장합니다. CSI 드라이버는PersistentVolume을 프로비저닝하고 스냅샷에서 이를 채웁니다.apiVersion: v1 kind: Pod metadata: name: restored-pod spec: containers: - name: busybox image: busybox args: - sleep - "3600" volumeMounts: - name: source-data mountPath: /demo/data volumes: - name: source-data persistentVolumeClaim: claimName: restored-pvc readOnly: false매니페스트를 적용합니다.
kubectl apply -f restored-pod.yaml파일이 복원되었는지 확인합니다.
kubectl exec restored-pod -- sh -c 'cat /demo/data/hello.txt'
삭제
이 페이지에서 사용한 리소스 비용이 Google Cloud 계정에 청구되지 않도록 하려면 다음 단계를 수행합니다.
VolumeSnapshot을 삭제합니다.kubectl delete volumesnapshot my-snapshotVolumeSnapshotClass를 삭제합니다.kubectl delete volumesnapshotclass my-snapshotclassDeployment를 삭제합니다.kubectl delete deployments hello-appPersistentVolumeClaim객체를 삭제합니다.kubectl delete pvc my-pvc pvc-restore
다음 단계
- Kubernetes 볼륨 스냅샷 문서 읽어보기
- 볼륨 확장에 대해 알아보기
- CSI 드라이버 수동 설치 방법 알아보기
- GKE용 블록 스토리지(Persistent Disk) 알아보기