Ubuntu 21.04
Sponsored Link

MicroK8s : Enable Storage
2021/05/13
 
To enable MicroK8s built-in Storage, Configure like follows.
[1] Enable built-in Storage on primary Node.
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 starts

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

NAMESPACE     NAME                                         READY   STATUS    RESTARTS   AGE
kube-system   metrics-server-8bbfb4bdb-sczq9               1/1     Running   0          9m53s
kube-system   calico-kube-controllers-847c8c99d-92dm5      1/1     Running   1          45m
kube-system   kubernetes-dashboard-7ffd448895-6cgrz        1/1     Running   0          9m49s
kube-system   calico-node-9wpkd                            1/1     Running   0          26m
kube-system   dashboard-metrics-scraper-6c4568dc68-5g2x2   1/1     Running   0          9m49s
kube-system   coredns-86f78bb79c-xcbkm                     1/1     Running   0          9m48s
kube-system   hostpath-provisioner-5c65fbdb4f-bvzbp        1/1     Running   0          13s
[2] This is an example to use storage on a Pod to define PVC (Persistent Volume Claim) object.
root@dlp:~#
vi my-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  # any PVC name
  name: my-pvc
spec:
  # access mode :
  # - ReadWriteMany (RW from multi nodes)
  # - ReadWriteOnce (RW from a node)
  # - ReadOnlyMany (R from multi nodes)
  accessModes:
    - ReadWriteOnce
  # specify microk8s-hostpath
  storageClassName: microk8s-hostpath
  resources:
    requests:
      # storage size to use
      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-abd1cd25-0ac2-4ae0-8f02-ce67063cbaeb   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-abd1cd25-0ac2-4ae0-8f02-ce67063cbaeb   1Gi        RWO            microk8s-hostpath   30s

# for example, create a 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:
        # specify PVC name you defined
        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          19s     10.1.142.78   dlp.srv.world   <none>           <none>

# onfirm PVC Path

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

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

Volume:        pvc-abd1cd25-0ac2-4ae0-8f02-ce67063cbaeb

# create a test page

root@dlp:~#
echo "Hello MicroK8s Storage" > /var/snap/microk8s/common/default-storage/default-my-pvc-pvc-abd1cd25-0ac2-4ae0-8f02-ce67063cbaeb/index.html
# access to verify

root@dlp:~#
curl 10.1.142.78

Hello MicroK8s Storage
Matched Content