ローカルストレージをKubernetesのPersistent Volumeとして追加する

AWXのように、Persistent Volumeを必要だったり、あったほうが都合が良いサービスを検証するために、Persistent Volumeをローカルストレージで提供する設定のメモ。

環境

  • HW: MacBook Pro(14"/2021)
  • CPU: Apple M1 Max
  • MEM: 32GB
  • OS: macOS Monterey 12.1
  • Docker Desktop 4.4.2 (73305)

ローカルストレージにPersistent Volumeを作成する

Persistent Volumeの配置先となるディレクトリを作成する

ここでは、ローカルストレージにPersistent Volume用のディレクトリを5つ(pv0[0-4])作成する。macOS/var//private/var/シンボリックリンクなので、以降のパス指定では、実体である/private/var/を指定する。

$ for i in $(seq 0 4); do sudo mkdir -m 777 -p /private/var/lib/kubernetes/pv0$i; done

/private/配下は、Docker Desktop設定で母艦から共有可能なホワイトリストに入っているので、KubernetesのPersistent Volumeのパスとして指定できる。このホワイトリストに入っていないと、Kubernetes側にPersistent Volumeの設定を投入しても、コンテナからは見えないので注意。

f:id:pyde:20220208131633p:plain

Persistent Volumeを作成するためのYAMLファイルを作成する

公式ドキュメントのガイドをベースとして、ここではpv00に20GBを設定するYAMLファイル(create_persistent_volume.yaml)を作成する。

Docker Desktop上のKubernetesなので、ローカルホストからのみPersistent Volumeにアクセスできれば良い。そのため、accessModesReadWriteOnceを設定しておく。

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv00
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/private/var/lib/kubernetes/pv00"

Persistent Volume設定をapplyする

create_persistent_volume.ymlをapplyする。

$ kubectl apply -f create_persistent_volume.yaml
persistentvolume/pv00 created

pv00が追加されていることを確認する。

$ kubectl get pv
NAME   CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM   STORAGECLASS   REASON   AGE
pv00   20Gi       RWO            Retain           Available           manual                  11m

$ kubectl describe pv
Name:            pv00
Labels:          type=local
Annotations:     <none>
Finalizers:      [kubernetes.io/pv-protection]
StorageClass:    manual
Status:          Available
Claim:
Reclaim Policy:  Retain
Access Modes:    RWO
VolumeMode:      Filesystem
Capacity:        20Gi
Node Affinity:   <none>
Message:
Source:
    Type:          HostPath (bare host directory volume)
    Path:          /private/var/lib/kubernetes/pv00
    HostPathType:
Events:            <none>

ダッシュボードからもpv00を確認できる。

f:id:pyde:20220208131809p:plain

create_persistent_volume.yamlをベースに、metadata.namespec.storageに、pv01からpv04を指定するようcreate_persistent_volume.ymlを修正することで、Persistent Volumeを必要に応じて順次追加できる。

Persistent Volumeのテスト

Persistent Volume Claimを作成する

テスト用にPersistent VolumeからPersistent Volume Claimを作成するYAMLファイル(create_persistent_volume_claim.yaml)してボリュームを払い出す。

ここでは、テスト目的でspec.volumeNameにPersistent Volume名を指定して、特定のPersistent Volumeから払い出しを行っている。

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv00-claim-0
spec:
  storageClassName: manual
  volumeName: pv00
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

create_persistent_volume_claim.yamlをapplyしてボリュームを払い出す。applyがエラーなく完了したことを確認する。

$ kubectl apply -f create_persistent_volume_claim.yaml
persistentvolumeclaim/pv00-claim-0 created

$ kubectl get pvc
NAME           STATUS    VOLUME   CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pv00-claim-0   Pending   pv00     0                         manual         25s

$ kubectl describe pvc pv00-claim-0
Name:          pv00-claim-0
Namespace:     default
StorageClass:  manual
Status:        Pending
Volume:        pv00
Labels:        <none>
Annotations:   <none>
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:      0
Access Modes:
VolumeMode:    Filesystem
Used By:       <none>
Events:        <none>
Events:        <none>

Podを作成する

defaultネームスペースにテスト用のPodを作成し、Nginxコンテナを起動してボリュームをマウントしてテストする。

---
apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  volumes:
    - name: test-volume
      persistentVolumeClaim:
        claimName: pv00-claim-0
  containers:
    - name: test-pod-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: test-volume

Pod(test-pod)が作成され、Nginxコンテナ(test-pod-container)が起動したことを確認する。

$ kubectl get pod test-pod
NAME       READY   STATUS    RESTARTS   AGE
test-pod   1/1     Running   0          113s

$ kubectl get pods -o='custom-columns=NameSpace:.metadata.namespace,NAME:.metadata.name,CONTAINERS:.spec.containers[*].name'
NameSpace   NAME       CONTAINERS
default     test-pod   test-pod-container

母艦側で、index.htmlを作成する。

$ echo 'Hello, World!' > /var/lib/kubernetes/pv00/index.html

test-pod-containerにアタッチして、母艦側で作成したindex.htmlがコンテナ側から参照できることを確認して終了。

$ kubectl exec -it test-pod -- /bin/bash
root@test-pod:/# curl http://127.0.0.1/index.html
Hello, World!

Persistent VolumeとPersistent Volume Claimが正しく動作するかどうかを確認するためには、おそらくこれで十分だと思う。