Persistent VolumeとPersistent Volume ClaimとPod、どれがどう紐づくか調べた
調べた背景
業務でPod使うときは永続ボリュームを使っておらず、いつもPodでクラウドに置いてあるデータをDLしていた。よく考えると永続ボリューム全然使っていないな、、どう使うんだっけというので使い方の再勉強
環境
kindで調べました
$ kind version
kind v0.23.0 go1.22.3 darwin/amd64
kind create cluster --config kind_config.yaml
kind_config.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
# add a mount from /path/to/my/files on the host to /files on the node
extraMounts:
- hostPath: /Users/user/my_mnt
containerPath: /my_mnt
kindでPVを使えるように、extraMountsを指定
PVCとPVを調べる
PVCとPVをapplyします。
k apply -f mypv.yaml
mypv.yaml
apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: null
name: namespace1
spec: {}
status: {}
---
apiVersion: v1
kind: Namespace
metadata:
creationTimestamp: null
name: namespace2
spec: {}
status: {}
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
storageClassName: ""
capacity:
storage: 50Mi
accessModes:
- ReadWriteMany
hostPath:
path: "/my_mnt/"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
namespace: namespace1
spec:
storageClassName: ""
accessModes:
- ReadWriteMany
resources:
requests:
storage: 20Mi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv2
namespace: namespace1
spec:
storageClassName: ""
capacity:
storage: 50Mi
accessModes:
- ReadWriteMany
hostPath:
path: "/my_mnt/"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc2
namespace: namespace1
spec:
storageClassName: ""
accessModes:
- ReadWriteMany
resources:
requests:
storage: 20Mi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv3
namespace: namespace1
spec:
storageClassName: ""
capacity:
storage: 50Mi
accessModes:
- ReadWriteMany
hostPath:
path: "/my_mnt/"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc3
spec:
storageClassName: ""
accessModes:
- ReadWriteMany
resources:
requests:
storage: 20Mi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv4
namespace: namespace2
spec:
storageClassName: ""
capacity:
storage: 50Mi
accessModes:
- ReadWriteMany
hostPath:
path: "/my_mnt/"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc4
namespace: namespace1
spec:
storageClassName: ""
accessModes:
- ReadWriteMany
resources:
requests:
storage: 20Mi
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv5
spec:
storageClassName: foo
capacity:
storage: 50Mi
accessModes:
- ReadWriteMany
hostPath:
path: "/my_mnt/"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc5
spec:
storageClassName: bar
accessModes:
- ReadWriteMany
resources:
requests:
storage: 20Mi
結果
$ k get pv -A
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS VOLUMEATTRIBUTESCLASS REASON AGE
my-pv 50Mi RWX Retain Bound namespace1/my-pvc <unset> 3s
my-pv2 50Mi RWX Retain Bound namespace1/my-pvc2 <unset> 3s
my-pv3 50Mi RWX Retain Bound default/my-pvc3 <unset> 3s
my-pv4 50Mi RWX Retain Bound namespace1/my-pvc4 <unset> 3s
my-pv5 50Mi RWX Retain Available foo <unset> 3s
$ k get pvc -A
NAMESPACE NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE
default my-pvc3 Bound my-pv3 50Mi RWX <unset> 8s
default my-pvc5 Pending bar <unset> 8s
namespace1 my-pvc Bound my-pv 50Mi RWX <unset> 8s
namespace1 my-pvc2 Bound my-pv2 50Mi RWX <unset> 8s
namespace1 my-pvc4 Bound my-pv4 50Mi RWX <unset> 8s
my-pv(c)4までは全てBoundとなっている。my-pv5はPending状態なので、Bindされなかった。
Persistent Volume Claimの要求する容量を満たすPersistent Volumeがあれば、名前空間が違ってもBindされる。Bindされないようにするには、名前空間ではなくて、StorageClassを変える必要があった。
PVCとPodを調べる
k apply -f mypod.yaml
mypod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: mypod
name: mypod
namespace: namespace1
spec:
containers:
- image: debian:bullseye
name: mypod
resources: {}
command: ["/bin/bash"]
args: ["-c", "sleep 1d"]
volumeMounts:
- name: myvol
mountPath: /tmp/
dnsPolicy: ClusterFirst
restartPolicy: Always
volumes:
- name: myvol
persistentVolumeClaim:
claimName: my-pvc
status: {}
---
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: mypod
name: mypod2
namespace: namespace1
spec:
containers:
- image: debian:bullseye
name: mypod
resources: {}
command: ["/bin/bash"]
args: ["-c", "sleep 1d"]
volumeMounts:
- name: myvol
mountPath: /tmp/
dnsPolicy: ClusterFirst
restartPolicy: Always
volumes:
- name: myvol
persistentVolumeClaim:
claimName: my-pvc
status: {}
---
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: mypod
name: mypod3
spec:
containers:
- image: debian:bullseye
name: mypod
resources: {}
command: ["/bin/bash"]
args: ["-c", "sleep 1d"]
volumeMounts:
- name: myvol
mountPath: /tmp/
dnsPolicy: ClusterFirst
restartPolicy: Always
volumes:
- name: myvol
persistentVolumeClaim:
claimName: my-pvc
status: {}
結果
$ k get pod -A
NAMESPACE NAME READY STATUS RESTARTS AGE
default mypod3 0/1 Pending 0 30m
...
namespace1 mypod 1/1 Running 0 30m
namespace1 mypod2 1/1 Running 0 30m
$ k describe pod mypod3
Warning FailedScheduling 33s default-scheduler 0/1 nodes are available: persistentvolumeclaim "my-pvc" not found. preemption: 0/1 nodes are available: 1 Preemption is not helpful for scheduling.
PodとPVCは同じでないとダメそう。名前空間の異なるmypod3は起動できなかった。
調べた結果
PVとPVCのbindはStorageClassで制御する必要がある。
PVCとPodは同じnamespaceにいないといけない。
あと、PVにつけられるPVCは一つだけ。
調べたと偉そうに書いたが、公式に乗っていることの確認しただけ
この投稿へのトラックバック
トラックバックはありません。
- トラックバック URL
この投稿へのコメント