Kubernetes/EKS Study

EKS에서 볼륨 snapscheduler 설치하기

백곰곰 2023. 5. 9. 23:34
728x90

VolumeSnapshot을 주기적으로 생성할 수 있는 방법을 검색하던 도중 snapscheduler라는 프로젝트를 발견했습니다.

helm으로 간단하게 설치가 가능해서 실제로 잘 동작하는지 실습을 통해 알아봤습니다.

 

해당 실습 전에 Volume Snapshots Controller가 클러스터에 설치가 되어 있어야 합니다.

설치 과정은 이전 포스팅에 정리해 두었습니다.

설치

$ helm repo add backube https://backube.github.io/helm-charts/
$ kubectl create namespace backube-snapscheduler
$ helm install -n backube-snapscheduler snapscheduler backube/snapscheduler

예제 

먼저 복구 테스트용 pod와 pvc를 생성하겠습니다.

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: ebs-claim
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 4Gi
  storageClassName: gp3
---
apiVersion: v1
kind: Pod
metadata:
  name: app
spec:
  terminationGracePeriodSeconds: 3
  containers:
  - name: app
    image: centos
    command: ["/bin/sh"]
    args: ["-c", "while true; do echo $(date -u) >> /data/out.txt; sleep 5; done"]
    volumeMounts:
    - name: persistent-storage
      mountPath: /data
  volumes:
  - name: persistent-storage
    persistentVolumeClaim:
      claimName: ebs-claim

참고로 pod에서는 /data/out.txt 파일에 시간을 남기고 있습니다.

 kubectl exec app -- tail -f /data/out.txt
Tue May 9 14:18:34 UTC 2023
Tue May 9 14:18:39 UTC 2023
Tue May 9 14:18:44 UTC 2023
Tue May 9 14:18:49 UTC 2023

이제 default namespace에 있는 pv에 대해서 5분마다 snapshot을 생성하는 scheduler를 생성해보겠습니다. 

snapshotClassName은 실제 클러스터 내 vsclass 명과 일치시키면 됩니다.

apiVersion: snapscheduler.backube/v1
kind: SnapshotSchedule
metadata:
  # The name for this schedule. It is also used as a part
  # of the template for naming the snapshots.
  name: every-5min
  # Schedules are namespaced objects
  namespace: default
spec:
  # A LabelSelector to control which PVCs should be snapshotted
  claimSelector:  # optional
  # Set to true to make the schedule inactive
  disabled: false  # optional
  retention:
    # The length of time a given snapshot should be
    # retained, specified in hours. (168h = 1 week)
    expires: "168h"  # optional
    # The maximum number of snapshots per PVC to keep
    maxCount: 10  # optional
  # The cronspec (https://en.wikipedia.org/wiki/Cron#Overview)
  # that defines the schedule. It is interpreted with
  # respect to the UTC timezone. The following pre-defined
  # shortcuts are also supported: @hourly, @daily, @weekly,
  # @monthly, and @yearly
  schedule: "*/5 * * * *"
  snapshotTemplate:
    # A set of labels can be added to each
    # VolumeSnapshot object
    labels:  # optional
      mylabel: myvalue
    # The SnapshotClassName to use when creating the
    # snapshots. If omitted, the cluster default will
    # be used.
    snapshotClassName: csi-aws-vsc  # optional

snapshotschedules이 정상적으로 생성된 것을 볼 수 있습니다.

 

$ kubectl get snapshotschedules
NAME         SCHEDULE      MAX AGE   MAX NUM   DISABLED   NEXT SNAPSHOT
every-5min   */5 * * * *   168h      10        false      2023-05-09T14:25:00Z

설정한 시간이 되면 volumesnapshot이 생성된 것을 볼 수 있습니다.

$ k get volumesnapshot
NAME                                READYTOUSE   SOURCEPVC   SOURCESNAPSHOTCONTENT   RESTORESIZE   SNAPSHOTCLASS   SNAPSHOTCONTENT                                    CREATIONTIME   AGE
ebs-claim-every-5min-202305091425   true         ebs-claim                           4Gi           csi-aws-vsc     snapcontent-712c0559-a2f5-4513-8141-29f5519fd313   43s            43s

이제 pod와 pvc를 삭제하겠습니다.

$ k get po,pvc,pv
No resources found

그리고 snapshot을 사용해서 pvc를 만듭니다.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: ebs-snapshot-restored-claim
spec:
  storageClassName: gp3
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 4Gi
  dataSource:
    name: ebs-claim-every-5min-202305091425
    kind: VolumeSnapshot
    apiGroup: snapshot.storage.k8s.io

pvc가 연결된 pod도 새로 만듭니다.

apiVersion: v1
kind: Pod
metadata:
  name: app
spec:
  containers:
  - name: app
    image: centos
    command: ["/bin/sh"]
    args: ["-c", "while true; do echo $(date -u) >> /data/out.txt; sleep 5; done"]
    volumeMounts:
    - name: persistent-storage
      mountPath: /data
  volumes:
  - name: persistent-storage
    persistentVolumeClaim:
      claimName: ebs-snapshot-restored-claim
$ k get po,pvc,pv
NAME      READY   STATUS    RESTARTS   AGE
pod/app   1/1     Running   0          21s

NAME                                                STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
persistentvolumeclaim/ebs-snapshot-restored-claim   Bound    pvc-39872e70-b384-4821-a7f8-ee2297c97117   4Gi        RWO            gp3            39s

NAME                                                        CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                                 STORAGECLASS   REASON   AGE
persistentvolume/pvc-39872e70-b384-4821-a7f8-ee2297c97117   4Gi        RWO            Delete           Bound    default/ebs-snapshot-restored-claim   gp3                     17s

이제 생성된 pod에 이전 데이터가 남아있는지 확인해보겠습니다.

25분 이전 데이터가 있는 것을 확인 했고 복구 이후인 29분 이후 새로운 로그가 남은 것을 볼 수 있습니다.

$ kubectl exec app -- cat /data/out.txt
...
Tue May 9 14:23:54 UTC 2023
Tue May 9 14:23:59 UTC 2023
Tue May 9 14:24:04 UTC 2023
Tue May 9 14:24:09 UTC 2023
Tue May 9 14:24:14 UTC 2023
Tue May 9 14:24:19 UTC 2023
Tue May 9 14:24:24 UTC 2023
Tue May 9 14:24:29 UTC 2023
Tue May 9 14:24:34 UTC 2023
Tue May 9 14:24:39 UTC 2023
Tue May 9 14:24:44 UTC 2023
Tue May 9 14:24:49 UTC 2023
Tue May 9 14:24:54 UTC 2023
Tue May 9 14:29:36 UTC 2023
Tue May 9 14:29:41 UTC 2023
Tue May 9 14:29:46 UTC 2023
...

이상으로 snapshot scheduler 실습을 마치겠습니다.

참고 문서

728x90
반응형

'Kubernetes > EKS Study' 카테고리의 다른 글

[4주차] EKS Observability  (0) 2023.05.21
[3주차] EKS Storage  (0) 2023.05.09
EKS에서 pvc로 생성한 ebs에 태그 추가하기  (0) 2023.05.09
[2주차] EKS Networking  (0) 2023.05.01
[1주차] EKS 설치 및 기본 사용  (2) 2023.04.24