kubernetes 学習 PersistentVolume
PersistenceVolume 使うべからず?いままで避けていたが機会が巡ってきたのでやってみる。
persistent-volumes
https://kubernetes.io/docs/concepts/storage/persistent-volumes/
- StorageClass: ストレージの種類を示すオブジェクト
- PersistentVolume(PV): 永続化ボリュームについてのオブジェクト
- PersistentVolumeClaim(PVC): 永続化ボリュームの利用請求をするオブジェクト
PersistentVolume, PersistentVolumeClaim を作成
my-pv.yaml
apiVersion: v1 kind: PersistentVolume metadata: name: my-pv labels: type: local spec: storageClassName: manual capacity: storage: 5Gi accessModes: - ReadWriteOnce hostPath: path: "/mnt/data"
my-pvc.yaml
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: my-pvc spec: accessModes: - ReadWriteOnce volumeMode: Filesystem resources: requests: storage: 5Gi storageClassName: manual
apply し確認
$ kubectl apply -f my-pv.yaml persistentvolume/my-pv created $ kubectl apply -f my-pvc.yaml persistentvolumeclaim/my-pvc created $ kubectl get pv,pvc NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE persistentvolume/my-pv 5Gi RWO Retain Bound default/my-pvc manual 12s NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE persistentvolumeclaim/my-pvc Bound my-pv 5Gi RWO manual 9s
pod へマウントして試す
my-pod.yaml
apiVersion: v1 kind: Pod metadata: name: my-pod spec: containers: - image: alpine name: alpine command: ["tail", "-f", "/dev/null"] volumeMounts: - name: claim-volume mountPath: /data volumes: - name: claim-volume persistentVolumeClaim: claimName: my-pvc terminationGracePeriodSeconds: 0
apply して df。/data
がいる。touch でファイル作成
$ kubectl apply -f my-pod.yaml pod/my-pod created $ kubectl exec my-pod -- df /data Filesystem 1K-blocks Used Available Use% Mounted on /dev/vda9 17897500 2210592 14907276 13% /data $ kubectl exec my-pod -- touch /data/asdf
node22に /mnt/data/asdf
ができていた
$ ls -al /mnt/data/ total 20 drwxr-xr-x. 2 root root 4096 Aug 14 20:31 . drwxr-xr-x. 3 root root 4096 Aug 14 20:22 .. -rw-r--r--. 1 root root 0 Aug 14 20:31 asdf
お掃除
$ kubectl delete -f my-pod.yaml pod "my-pod" deleted $ kubectl delete -f my-pvc.yaml persistentvolumeclaim "my-pvc" deleted $ kubectl delete -f my-pv.yaml persistentvolume "my-pv" deleted