Ubuntu 24.04
Sponsored Link

MicroK8s : 動的プロビジョニング (NFS)2024/06/14

 

外部ストレージを使用している場合、ボリュームの動的プロビジョニング機能を利用することで、クラスター管理者が事前に PV (Persistent Volume) を作成することなく、ユーザーからの PVC (Persistent Volume Claim) 作成要求時に、動的に PV を作成することができます。

[1]
こちらを参考に、任意のサーバー上で NFS サーバーを稼働させておきます
なお、当例では [nfs.srv.world (10.0.0.35)] サーバー上の [/home/nfsshare] ディレクトリを共有ディレクトリとして設定します。
[2] Helm で NFS Client Provisioner をインストールします。
root@dlp:~#
microk8s helm3 repo add nfs-subdir-external-provisioner https://kubernetes-sigs.github.io/nfs-subdir-external-provisioner/
# nfs.server = (NFS サーバーのホスト名 または IP アドレス)
# nfs.path = (NFS 共有のパス)

root@dlp:~#
microk8s helm3 install nfs-client -n kube-system --set nfs.server=10.0.0.35 --set nfs.path=/home/nfsshare nfs-subdir-external-provisioner/nfs-subdir-external-provisioner
NAME: nfs-client
LAST DEPLOYED: Fri Jun 14 03:48:46 2024
NAMESPACE: kube-system
STATUS: deployed
REVISION: 1
TEST SUITE: None

root@dlp:~#
microk8s kubectl get pods -n kube-system

NAME                                                          READY   STATUS    RESTARTS       AGE
.....
.....
nfs-client-nfs-subdir-external-provisioner-864f5ff99b-rfqkz   1/1     Running   0              8m52s
[3] Pod からの使用例です。
root@dlp:~#
microk8s kubectl get storageclass

NAME                          PROVISIONER                                                RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
microk8s-hostpath (default)   microk8s.io/hostpath                                       Delete          WaitForFirstConsumer   false                  107m
nfs-client                    cluster.local/nfs-client-nfs-subdir-external-provisioner   Delete          Immediate              true                   9m29s

# PVC 作成

root@dlp:~#
vi my-pvc.yml
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: my-provisioner spec: accessModes: - ReadWriteOnce # StorageClass の名称を指定 storageClassName: nfs-client resources: requests: # リクエストするボリュームサイズ storage: 5Gi
root@dlp:~#
microk8s kubectl apply -f my-pvc.yml

persistentvolumeclaim/my-provisioner configured
root@dlp:~#
microk8s kubectl get pvc

NAME             STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
my-provisioner   Bound    pvc-74a17dbd-6743-4472-9519-4f0b765fa2b4   5Gi        RWO            nfs-client     <unset>                 3m32s

# PV は自動生成される

root@dlp:~#
microk8s kubectl get pv

NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                               STORAGECLASS        VOLUMEATTRIBUTESCLASS   REASON   AGE
pvc-74a17dbd-6743-4472-9519-4f0b765fa2b4   5Gi        RWO            Delete           Bound    default/my-provisioner              nfs-client          <unset>                          4m9s
pvc-fbf13c3e-0c3d-4ab0-bd26-0f139fdf1643   30Gi       RWX            Delete           Bound    container-registry/registry-claim   microk8s-hostpath   <unset>                          112m

root@dlp:~#
vi my-pod.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  selector:
    matchLabels:
      run: my-nginx
  replicas: 1
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: nginx-pvc
      volumes:
        - name: nginx-pvc
          persistentVolumeClaim:
            # 作成した PVC 名
            claimName: my-provisioner

root@dlp:~#
microk8s kubectl apply -f my-pod.yml

deployment.apps/my-nginx created
root@dlp:~#
microk8s kubectl get pods -o wide

NAME                        READY   STATUS    RESTARTS   AGE     IP            NODE            NOMINATED NODE   READINESS GATES
my-nginx-66fc75ffb8-lkbzz   1/1     Running   0          2m45s   10.1.142.88   dlp.srv.world   <none>           <none>

root@dlp:~#
microk8s kubectl exec my-nginx-66fc75ffb8-lkbzz -- df /usr/share/nginx/html

Filesystem                                                                               1K-blocks  Used Available Use% Mounted on
10.0.0.35:/home/nfsshare/default-my-provisioner-pvc-74a17dbd-6743-4472-9519-4f0b765fa2b4 164028416     0 155623424   0% /usr/share/nginx/html

# index ファイルを作成して動作確認

root@dlp:~#
echo "Nginx Index" > index.html

root@dlp:~#
microk8s kubectl cp index.html my-nginx-66fc75ffb8-lkbzz:/usr/share/nginx/html/index.html

root@dlp:~#
curl 10.1.142.88

Nginx Index
# 削除する場合は PVC を削除すると PV も削除される

root@dlp:~#
microk8s kubectl delete deployment my-nginx

deployment.apps "my-nginx" deleted
root@dlp:~#
microk8s kubectl delete pvc my-provisioner

persistentvolumeclaim "my-provisioner" deleted
root@dlp:~#
microk8s kubectl get pv

NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                               STORAGECLASS        VOLUMEATTRIBUTESCLASS   REASON   AGE
pvc-fbf13c3e-0c3d-4ab0-bd26-0f139fdf1643   30Gi       RWX            Delete           Bound    container-registry/registry-claim   microk8s-hostpath   <unset>                          120m
[4] StatefulSet を使用すると [volumeClaimTemplates] が指定可能なため、PVC の作成までまとめて記述できます。
root@dlp:~#
microk8s kubectl get storageclass

NAME                          PROVISIONER                                                RECLAIMPOLICY   VOLUMEBINDINGMODE      ALLOWVOLUMEEXPANSION   AGE
microk8s-hostpath (default)   microk8s.io/hostpath                                       Delete          WaitForFirstConsumer   false                  122m
nfs-client                    cluster.local/nfs-client-nfs-subdir-external-provisioner   Delete          Immediate              true                   24m

root@dlp:~#
vi statefulset.yml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: my-mginx
spec:
  serviceName: my-mginx
  replicas: 1
  selector:
    matchLabels:
      app: my-mginx
  template:
    metadata:
      labels:
        app: my-mginx
    spec:
      containers:
      - name: my-mginx
        image: nginx
        volumeMounts:
        - name: data
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      # StorageClass の名称を指定
      storageClassName: nfs-client
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 5Gi

root@dlp:~#
microk8s kubectl apply -f statefulset.yml

statefulset.apps/my-mginx created
root@dlp:~#
microk8s kubectl get statefulset

NAME       READY   AGE
my-mginx   1/1     10s

root@dlp:~#
microk8s kubectl get pods -o wide

NAME         READY   STATUS    RESTARTS   AGE   IP            NODE            NOMINATED NODE   READINESS GATES
my-mginx-0   1/1     Running   0          15s   10.1.142.89   dlp.srv.world   <none>           <none>

root@dlp:~#
microk8s kubectl get pvc

NAME              STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   VOLUMEATTRIBUTESCLASS   AGE
data-my-mginx-0   Bound    pvc-ea072275-904a-4529-9fd1-453b4c5ff007   5Gi        RWO            nfs-client     <unset>                 46s

root@dlp:~#
microk8s kubectl get pv

NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                               STORAGECLASS        VOLUMEATTRIBUTESCLASS   REASON   AGE
pvc-ea072275-904a-4529-9fd1-453b4c5ff007   5Gi        RWO            Delete           Bound    default/data-my-mginx-0             nfs-client          <unset>                          72s
pvc-fbf13c3e-0c3d-4ab0-bd26-0f139fdf1643   30Gi       RWX            Delete           Bound    container-registry/registry-claim   microk8s-hostpath   <unset>                          124m
関連コンテンツ