title: "**기존 storage 재사용 in kind (w/ 데이터 유지)**"
description: "app, cluster가 재시작되어도 기존에 저장한 데이터를 그대로 사용하는 방법에 관한 설명이다."
cleanUrl: /sw-engineer/reuse-storage-in-kind
ogImage: "<https://anyflower.notion.site/image/https%3A%2F%2Fprod-files-secure.s3.us-west-2.amazonaws.com%2F7570d2fc-66b1-4e23-bb3c-ff7b56842b0d%2Fa5920f76-cea9-4738-aac3-d45b224241d1%2FUntitled.png?table=block&id=db88b3de-367a-48fc-80f2-a9f357691569&spaceId=7570d2fc-66b1-4e23-bb3c-ff7b56842b0d&width=2000&userId=&cache=v2>"
floatFirstTOC: right

Untitled

Motivation

storage에 저장된 데이터는 app 또는 cluster의 lifecycle과는 별개로 다룰 필요는 항시 발생하기 마련이다. 예컨데 Docker Registry app에서 많은 container image를 저장했을 때, (memory 부족 등 어떤 이유에서건) Docker Registry app 또는 cluster 자체를 재기동해야 하는 경우가 있다. 이를 해결하기 위한 구체적 요구사항은 다음이 될 것이다.

  1. app이 삭제되더라도 데이터는 삭제되지 않도록
  2. 기존 데이터를 신규 app과 cluster에서 사용 가능하도록

[kind](<https://kind.sigs.k8s.io/>)가 사용하는 기본 Storage class (standard)인 [local-path-provisioner](<https://github.com/rancher/local-path-provisioner>)는 hostPath로의 dynamic provisioning을 위함인데, 이 특성 상 사전에 PVC (Persistent Volume Claim)를 지정하지 않으면 새로운 PV (Persistent Volume)를 생성하기에 2번 요구사항과 상충한다. 또한 이 storage class의 default reclaim policy는 PVC가 삭제될 경우 해당 PV도 함께 삭제하는 Delete이므로, 1번 요구사항과도 충돌한다.

<aside> 💡 본 글은 My Cluster - Home k8s cluster 프로젝트의 일환으로, 아래는 여기서 사용한 코드를 포함한 이 프로젝트의 Github 링크이다.

GitHub - anyflow/my-cluster: Quickly install and remove various apps operated in Kubernetes in a local environment.

</aside>

Summary

기존 storage와 PV 간 binding, PV와 PVC 간 binding, 그리고 pod가 binding을 마친 PVC를 사용하도록 설정함으로 기존 storage 재사용이 가능하다.

설명

Summary에서 논한 두 가지 binding과 이를 통해 만들어진 PVC 사용 설정에 관한 상세 내용이다.

기존 storage와 PV 간 binding

1. spec.hostPath 설정

(pod 관점에서의 node에 해당하는) host내 저장 path를 지정한다. 이 path의 prefix는 [kind-config.yaml](<https://github.com/anyflow/my-cluster/blob/main/kind-config.yaml>)의 nodes.extraMounts.containerPath와 맞춰야 cluster 외부, 즉 kind cluster의 host에서 해당 directory를 조회할 수 있다. [kind-config.yaml](<https://github.com/anyflow/my-cluster/blob/main/kind-config.yaml>)의 nodes.extraMounts.hostPath는 외부 관점에서의 해당 directory path를 의미한다.

2. spec.nodeAffinity 설정

기존의 storage가 위치한 node가 아닌 타 node에 pod가 생성되는 것을 방지하기 위함으로, spec.nodeAffinity 없이 hostPath만 설정하면 기존 storage에 위치한 node가 아닌 타 node에 pod가 생성될 수 있어 기존 storage와의 binding에 실패한다.

PV와 PVC 간 binding