Debian 11 Bullseye
Sponsored Link

MicroK8s : Storage を有効にする
2021/08/30
 
MicroK8s ビルトイン の Storage を有効にする場合は以下のように設定します。
[1] プライマリーノードで、ビルトイン Storage を有効にします。
root@dlp:~#
microk8s enable storage

Enabling default storage class
deployment.apps/hostpath-provisioner created
storageclass.storage.k8s.io/microk8s-hostpath created
serviceaccount/microk8s-hostpath created
clusterrole.rbac.authorization.k8s.io/microk8s-hostpath created
clusterrolebinding.rbac.authorization.k8s.io/microk8s-hostpath created
Storage will be available soon

# [hostpath-provisioner] pod が起動する

root@dlp:~#
microk8s kubectl get pods -A

NAMESPACE     NAME                                         READY   STATUS    RESTARTS   AGE
kube-system   metrics-server-8bbfb4bdb-26477               1/1     Running   0          10m
kube-system   calico-node-swq7c                            1/1     Running   0          18m
kube-system   calico-kube-controllers-f7868dd95-sznzp      1/1     Running   0          43m
kube-system   dashboard-metrics-scraper-78d7698477-ts455   1/1     Running   0          8m53s
kube-system   kubernetes-dashboard-85fd7f45cb-vg68g        1/1     Running   0          8m53s
kube-system   coredns-7f9c69c78c-wvhn6                     1/1     Running   0          8m53s
kube-system   hostpath-provisioner-5c65fbdb4f-lbqc5        1/1     Running   0          28s
[2] PVC (Persistent Volume Claim) オブジェクトを定義しての、Pod からのストレージ利用例です。
root@dlp:~#
vi my-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  # 任意の PVC 名
  name: my-pvc
spec:
  # アクセスモード:
  # - ReadWriteMany (複数ノード RW)
  # - ReadWriteOnce (単一ノード RW)
  # - ReadOnlyMany (複数ノード R)
  accessModes:
    - ReadWriteOnce
  # microk8s-hostpath を指定
  storageClassName: microk8s-hostpath
  resources:
    requests:
      # 利用したいストレージのサイズ
      storage: 1Gi

root@dlp:~#
microk8s kubectl create -f my-pvc.yml

persistentvolumeclaim/my-pvc created
root@dlp:~#
microk8s kubectl get pv

NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM            STORAGECLASS        REASON   AGE
pvc-66f92937-fc0e-4717-a666-8d0ab6a78cca   1Gi        RWO            Delete           Bound    default/my-pvc   microk8s-hostpath            4s

root@dlp:~#
microk8s kubectl get pvc

NAME     STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS        AGE
my-pvc   Bound    pvc-66f92937-fc0e-4717-a666-8d0ab6a78cca   1Gi        RWO            microk8s-hostpath   21s

# 例として nginx pod 作成

root@dlp:~#
vi nginx-mypv.yml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-mypv
  labels:
    name: nginx-mypv
spec:
  containers:
    - name: nginx-mypv
      image: fedora/nginx
      ports:
        - name: web
          containerPort: 80
      volumeMounts:
        - name: my-persistent-volume
          mountPath: /usr/share/nginx/html
  volumes:
    - name: my-persistent-volume
      persistentVolumeClaim:
        # 事前に定義した PVC 名を指定
        claimName: my-pvc

root@dlp:~#
microk8s kubectl create -f nginx-mypv.yml

pod/nginx-mypv created
root@dlp:~#
microk8s kubectl get pods -o wide

NAME                         READY   STATUS    RESTARTS   AGE     IP            NODE            NOMINATED NODE   READINESS GATES
nginx-mypv                   1/1     Running   0          22s     10.1.142.77   dlp.srv.world   <none>           <none>

# PVC の Path 確認

root@dlp:~#
microk8s kubectl describe -n kube-system pod/hostpath-provisioner-5c65fbdb4f-lbqc5 | grep PV_DIR

      PV_DIR:     /var/snap/microk8s/common/default-storage
root@dlp:~#
microk8s kubectl describe pvc/my-pvc | grep ^Volume:

Volume:        pvc-66f92937-fc0e-4717-a666-8d0ab6a78cca

# テストページ作成

root@dlp:~#
echo "Hello MicroK8s Storage" > /var/snap/microk8s/common/default-storage/default-my-pvc-pvc-66f92937-fc0e-4717-a666-8d0ab6a78cca/index.html
# アクセスして確認

root@dlp:~#
curl 10.1.142.77

Hello MicroK8s Storage
関連コンテンツ