Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Archives
Today
Total
관리 메뉴

전공공부

Volumes & Persistent Volumes & Persistent Volumes Claim 본문

Study/K8S

Volumes & Persistent Volumes & Persistent Volumes Claim

monitor 2023. 4. 2. 19:34
Volume

Volume은 기본적으로 Pod에서 생성된 데이터를 Pod와 따로 두어 Pod가 삭제되어도 데이터 값은 유지 하게 하기 위해 만들어진 것이다.

 

apiVersion: v1
kind: Pod
metadata:
  name: random-number-generator
spec:
  containers:
  - image: alpine
    name: alpine
    command: ["/bin/sh","-c"]
    args: ["shuf -i 0-100 -n 1 >> /opt/number.out"]
    volumeMounts: #opt로 마운트 된 값만 노드의 /data로 저장된다.
    - mountPath: /opt
      name: data-volume

  volumes:
  - name: data-volume
    hostPath:
      path: /data #이러면 node의 root/data에 데이터가 저장된다.
      type: Diretory


      #다중 노드 상황이라면? 오픈 소스 솔루션 사용을 하자

이것의 문제점은 Pod가 늘어나면 사용자는 Volume을 생성을 꾸준히 해주어야 한다는 것이다.

 

이것을 해결하기 위해서 Persistent Volume이 사용된다. (허나 이것은 PVC가 세팅 되어야 알 맞은 PV로 저장되어진다.)

 

Persistent Volumes (PV)

pv-definition.yaml

 

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-vol1
spec:
  accessModes:
    - ReadWriteOnce #읽기 쓰기 한 번
  capacity:
    storage: 1Gi # 1Gi를 준다.
  hostPath:
    path: /tmp/data #데이터가 저장될 로컬 디렉토리 파일 위치 (노드)
  awsElasticBlockStore: #오픈 소스 스토리지 솔루션 사용 할 거면 이렇게 쓸 수 있긴 함
    volumeID: <volume-id>
    fsType: ext4

 

Persistent Volumes Claim (PVC)

PV와 PVC를 바인딩해야 작동이 될 수 있다.

 

바인딩은 access 모드만 맞아도 바인딩 될 수 있고 특정 PVC만 등록하고 싶으면 PV Label을 사용하자.

 

 

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mypvc
spec:
  accessModes:
    - ReadWriteOnce #읽기 쓰기 한 번
  resources:
    requests: 
      storage: 500Mi

 

- 그럼 PVC가 삭제되면 PV는 어떻게 할까?

persistentVolumeReclaimPolicy: Delete (PVC 삭제시 삭제)

persistentVolumeReclaimPolicy: Retain (PVC 삭제해도 PV 유지)

persistentVolumeReclaimPolicy: Recycle (PVC 삭제하면 다른 PVC가 사용 할 수 있음)

위 옵션을 사용하면 된다.

 

저장될 Pod

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
    - name: myfrontend
      image: nginx
      volumeMounts:
      - mountPath: "/var/www/html" #내가 보내고 싶은 데이터들
        name: mypd
  volumes:
    - name: mypd
      persistentVolumeClaim:
        claimName: mypvc #연결할 pvc

 

 

 

 

 

'Study > K8S' 카테고리의 다른 글

Authentication  (0) 2023.04.05
Docker로 이미지 만들기  (0) 2023.04.02
Traffic  (0) 2023.04.02
Ingress  (0) 2023.04.02
Services  (0) 2023.04.01