{"id":5250,"date":"2023-10-28T20:30:45","date_gmt":"2023-10-28T18:30:45","guid":{"rendered":"http:\/\/miro.borodziuk.eu\/?p=5250"},"modified":"2024-02-13T18:06:35","modified_gmt":"2024-02-13T17:06:35","slug":"storage-in-kubernetes","status":"publish","type":"post","link":"http:\/\/miro.borodziuk.eu\/index.php\/2023\/10\/28\/storage-in-kubernetes\/","title":{"rendered":"Storage in Kubernetes"},"content":{"rendered":"<p>.<\/p>\n<p><!--more--><\/p>\n<p><span style=\"color: #3366ff;\">Using Pod Volumes<\/span><\/p>\n<ul>\n<li>Pod Volumes are a part of the Pod specification and have the storage reference hard coded in the Pod manifest<\/li>\n<li>This is not bad, but it doesn&#8217;t allow for flexible storage allocation<\/li>\n<li>Pod Volumes can be used for any storage type<\/li>\n<li>Also, the ConfigMap can be used to mount Pod Volumes<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">[root@k8s ~]# kubectl explain pod.spec.volumes\r\n\r\nKIND:       Pod\r\nVERSION:    v1\r\n\r\nFIELD: volumes &lt;[]Volume&gt;\r\n\r\nDESCRIPTION:\r\n    List of volumes that can be mounted by containers belonging to the pod. More\r\n    info: https:\/\/kubernetes.io\/docs\/concepts\/storage\/volumes\r\n    Volume represents a named volume in a pod that may be accessed by any\r\n    container in the pod.\r\n\r\nFIELDS:\r\n  awsElasticBlockStore  &lt;AWSElasticBlockStoreVolumeSource&gt;\r\n    awsElasticBlockStore represents an AWS Disk resource that is attached to a\r\n    kubelet's host machine and then exposed to the pod. More info:\r\n    https:\/\/kubernetes.io\/docs\/concepts\/storage\/volumes#awselasticblockstore\r\n\r\n  azureDisk     &lt;AzureDiskVolumeSource&gt;\r\n    azureDisk represents an Azure Data Disk mount on the host and bind mount to\r\n    the pod.\r\n\r\n  azureFile     &lt;AzureFileVolumeSource&gt;\r\n    azureFile represents an Azure File Service mount on the host and bind mount\r\n    to the pod.\r\n\r\n  cephfs        &lt;CephFSVolumeSource&gt;\r\n    cephFS represents a Ceph FS mount on the host that shares a pod's lifetime\r\n\r\n  cinder        &lt;CinderVolumeSource&gt;\r\n    cinder represents a cinder volume attached and mounted on kubelets host\r\n    machine. More info: https:\/\/examples.k8s.io\/mysql-cinder-pd\/README.md\r\n\r\n  configMap     &lt;ConfigMapVolumeSource&gt;\r\n    configMap represents a configMap that should populate this volume\r\n\r\n  csi   &lt;CSIVolumeSource&gt;\r\n    csi (Container Storage Interface) represents ephemeral storage that is\r\n    handled by certain external CSI drivers (Beta feature).\r\n\r\n  downwardAPI   &lt;DownwardAPIVolumeSource&gt;\r\n    downwardAPI represents downward API about the pod that should populate this\r\n    volume\r\n\r\n  emptyDir      &lt;EmptyDirVolumeSource&gt;\r\n    emptyDir represents a temporary directory that shares a pod's lifetime. More\r\n    info: https:\/\/kubernetes.io\/docs\/concepts\/storage\/volumes#emptydir\r\n\r\n  ephemeral     &lt;EphemeralVolumeSource&gt;\r\n    ephemeral represents a volume that is handled by a cluster storage driver.\r\n    The volume's lifecycle is tied to the pod that defines it - it will be\r\n    created before the pod starts, and deleted when the pod is removed.\r\n\r\n    Use this if: a) the volume is only needed while the pod runs, b) features of\r\n    normal volumes like restoring from snapshot or capacity\r\n       tracking are needed,\r\n    c) the storage driver is specified through a storage class, and d) the\r\n    storage driver supports dynamic volume provisioning through\r\n       a PersistentVolumeClaim (see EphemeralVolumeSource for more\r\n       information on the connection between this volume type\r\n       and PersistentVolumeClaim).\r\n\r\n    Use PersistentVolumeClaim or one of the vendor-specific APIs for volumes\r\n    that persist for longer than the lifecycle of an individual pod.\r\n\r\n    Use CSI for light-weight local ephemeral volumes if the CSI driver is meant\r\n    to be used that way - see the documentation of the driver for more\r\n    information.\r\n\r\n    A pod can use both types of ephemeral volumes and persistent volumes at the\r\n    same time.\r\n\r\n  fc    &lt;FCVolumeSource&gt;\r\n    fc represents a Fibre Channel resource that is attached to a kubelet's host\r\n    machine and then exposed to the pod.\r\n\r\n  flexVolume    &lt;FlexVolumeSource&gt;\r\n    flexVolume represents a generic volume resource that is provisioned\/attached\r\n    using an exec based plugin.\r\n\r\n  flocker       &lt;FlockerVolumeSource&gt;\r\n    flocker represents a Flocker volume attached to a kubelet's host machine.\r\n    This depends on the Flocker control service being running\r\n\r\n  gcePersistentDisk     &lt;GCEPersistentDiskVolumeSource&gt;\r\n    gcePersistentDisk represents a GCE Disk resource that is attached to a\r\n    kubelet's host machine and then exposed to the pod. More info:\r\n    https:\/\/kubernetes.io\/docs\/concepts\/storage\/volumes#gcepersistentdisk\r\n\r\n  gitRepo       &lt;GitRepoVolumeSource&gt;\r\n    gitRepo represents a git repository at a particular revision. DEPRECATED:\r\n    GitRepo is deprecated. To provision a container with a git repo, mount an\r\n    EmptyDir into an InitContainer that clones the repo using git, then mount\r\n    the EmptyDir into the Pod's container.\r\n\r\n  glusterfs     &lt;GlusterfsVolumeSource&gt;\r\n    glusterfs represents a Glusterfs mount on the host that shares a pod's\r\n    lifetime. More info: https:\/\/examples.k8s.io\/volumes\/glusterfs\/README.md\r\n\r\n  hostPath      &lt;HostPathVolumeSource&gt;\r\n    hostPath represents a pre-existing file or directory on the host machine\r\n    that is directly exposed to the container. This is generally used for system\r\n    agents or other privileged things that are allowed to see the host machine.\r\n    Most containers will NOT need this. More info:\r\n    https:\/\/kubernetes.io\/docs\/concepts\/storage\/volumes#hostpath\r\n\r\n  iscsi &lt;ISCSIVolumeSource&gt;\r\n    iscsi represents an ISCSI Disk resource that is attached to a kubelet's host\r\n    machine and then exposed to the pod. More info:\r\n    https:\/\/examples.k8s.io\/volumes\/iscsi\/README.md\r\n\r\n  name  &lt;string&gt; -required-\r\n    name of the volume. Must be a DNS_LABEL and unique within the pod. More\r\n    info:\r\n    https:\/\/kubernetes.io\/docs\/concepts\/overview\/working-with-objects\/names\/#names\r\n\r\n  nfs   &lt;NFSVolumeSource&gt;\r\n    nfs represents an NFS mount on the host that shares a pod's lifetime More\r\n    info: https:\/\/kubernetes.io\/docs\/concepts\/storage\/volumes#nfs\r\n\r\n  persistentVolumeClaim &lt;PersistentVolumeClaimVolumeSource&gt;\r\n    persistentVolumeClaimVolumeSource represents a reference to a\r\n    PersistentVolumeClaim in the same namespace. More info:\r\n    https:\/\/kubernetes.io\/docs\/concepts\/storage\/persistent-volumes#persistentvolumeclaims\r\n\r\n  photonPersistentDisk  &lt;PhotonPersistentDiskVolumeSource&gt;\r\n    photonPersistentDisk represents a PhotonController persistent disk attached\r\n    and mounted on kubelets host machine\r\n\r\n  portworxVolume        &lt;PortworxVolumeSource&gt;\r\n    portworxVolume represents a portworx volume attached and mounted on kubelets\r\n    host machine\r\n\r\n  projected     &lt;ProjectedVolumeSource&gt;\r\n    projected items for all in one resources secrets, configmaps, and downward\r\n    API\r\n\r\n  quobyte       &lt;QuobyteVolumeSource&gt;\r\n    quobyte represents a Quobyte mount on the host that shares a pod's lifetime\r\n\r\n  rbd   &lt;RBDVolumeSource&gt;\r\n    rbd represents a Rados Block Device mount on the host that shares a pod's\r\n    lifetime. More info: https:\/\/examples.k8s.io\/volumes\/rbd\/README.md\r\n\r\n  scaleIO       &lt;ScaleIOVolumeSource&gt;\r\n    scaleIO represents a ScaleIO persistent volume attached and mounted on\r\n    Kubernetes nodes.\r\n\r\n  secret        &lt;SecretVolumeSource&gt;\r\n    secret represents a secret that should populate this volume. More info:\r\n    https:\/\/kubernetes.io\/docs\/concepts\/storage\/volumes#secret\r\n\r\n  storageos     &lt;StorageOSVolumeSource&gt;\r\n    storageOS represents a StorageOS volume attached and mounted on Kubernetes\r\n    nodes.\r\n\r\n  vsphereVolume &lt;VsphereVirtualDiskVolumeSource&gt;\r\n    vsphereVolume represents a vSphere volume attached and mounted on kubelets\r\n    host machine\r\n<\/pre>\n<p>Example how simple share storage .<\/p>\n<pre class=\"lang:default decode:true \">[root@k8s cka]# vim morevolumes.yaml\r\n[root@k8s cka]# cat morevolumes.yaml\r\napiVersion: v1\r\nkind: Pod\r\nmetadata:\r\n  name: morevol\r\nspec:\r\n  containers:\r\n  - name: centos1\r\n    image: centos:7\r\n    command:\r\n      - sleep\r\n      - \"3600\"\r\n    volumeMounts:\r\n      - mountPath: \/centos1\r\n        name: test\r\n  - name: centos2\r\n    image: centos:7\r\n    command:\r\n      - sleep\r\n      - \"3600\"\r\n    volumeMounts:\r\n      - mountPath: \/centos2\r\n        name: test\r\n  volumes:\r\n    - name: test\r\n      emptyDir: {}\r\n[root@k8s cka]# kubectl apply -f morevolumes.yaml\r\npod\/morevol created\r\n\r\n[root@k8s cka]# kubectl get pods\r\nNAME                         READY   STATUS              RESTARTS         AGE\r\ndeploydaemon-zzllp           1\/1     Running             0                4h16m\r\nfirstnginx-d8679d567-249g9   1\/1     Running             0                29h\r\nfirstnginx-d8679d567-66c4s   1\/1     Running             0                29h\r\nfirstnginx-d8679d567-72qbd   1\/1     Running             0                29h\r\nfirstnginx-d8679d567-rhhlz   1\/1     Running             0                12h\r\ninit-demo                    1\/1     Running             0                14h\r\nmorevol                      0\/2     ContainerCreating   0                12s\r\nmydaemon-d4dcd               1\/1     Running             0                4h27m\r\nsleepy                       1\/1     Running             4 (27m ago)      15h\r\ntestpod                      1\/1     Running             0                29h\r\ntwo-containers               2\/2     Running             26 (6m22s ago)   12h\r\nweb-0                        1\/1     Running             0                17h\r\nweb-1                        1\/1     Running             0                4h27m\r\nweb-2                        1\/1     Running             0                4h27m\r\n\r\n[root@k8s cka]# kubectl describe morevol\r\nerror: the server doesn't have a resource type \"morevol\"\r\n[root@k8s cka]# kubectl describe pod morevol\r\nName:             morevol\r\nNamespace:        default\r\nPriority:         0\r\nService Account:  default\r\nNode:             k8s.netico.pl\/172.30.9.24\r\nStart Time:       Thu, 01 Feb 2024 20:26:03 -0500\r\nLabels:           &lt;none&gt;\r\nAnnotations:      &lt;none&gt;\r\nStatus:           Running\r\nIP:               10.244.0.18\r\nIPs:\r\n  IP:  10.244.0.18\r\nContainers:\r\n  centos1:\r\n    Container ID:  docker:\/\/7bd6b49a892aeedda9a4b829268e08bd83e924459e995d85c607da8abe9de1c5\r\n    Image:         centos:7\r\n    Image ID:      docker-pullable:\/\/centos@sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4\r\n    Port:          &lt;none&gt;\r\n    Host Port:     &lt;none&gt;\r\n    Command:\r\n      sleep\r\n      3600\r\n    State:          Running\r\n      Started:      Thu, 01 Feb 2024 20:26:18 -0500\r\n    Ready:          True\r\n    Restart Count:  0\r\n    Environment:    &lt;none&gt;\r\n    Mounts:\r\n      \/mnt\/centos1 from test (rw)\r\n      \/var\/run\/secrets\/kubernetes.io\/serviceaccount from kube-api-access-87m8c (ro)\r\n  centos2:\r\n    Container ID:  docker:\/\/6872581086a3f760e2511a373419f7be1136f3aa38828b7d767b86afe91a667f\r\n    Image:         centos:7\r\n    Image ID:      docker-pullable:\/\/centos@sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4\r\n    Port:          &lt;none&gt;\r\n    Host Port:     &lt;none&gt;\r\n    Command:\r\n      sleep\r\n      3600\r\n    State:          Running\r\n      Started:      Thu, 01 Feb 2024 20:26:19 -0500\r\n    Ready:          True\r\n    Restart Count:  0\r\n    Environment:    &lt;none&gt;\r\n    Mounts:\r\n      \/mnt\/centos2 from test (rw)\r\n      \/var\/run\/secrets\/kubernetes.io\/serviceaccount from kube-api-access-87m8c (ro)\r\nConditions:\r\n  Type              Status\r\n  Initialized       True\r\n  Ready             True\r\n  ContainersReady   True\r\n  PodScheduled      True\r\nVolumes:\r\n  test:\r\n    Type:       EmptyDir (a temporary directory that shares a pod's lifetime)\r\n    Medium:\r\n    SizeLimit:  &lt;unset&gt;\r\n  kube-api-access-87m8c:\r\n    Type:                    Projected (a volume that contains injected data from multiple sources)\r\n    TokenExpirationSeconds:  3607\r\n    ConfigMapName:           kube-root-ca.crt\r\n    ConfigMapOptional:       &lt;nil&gt;\r\n    DownwardAPI:             true\r\nQoS Class:                   BestEffort\r\nNode-Selectors:              &lt;none&gt;\r\nTolerations:                 node.kubernetes.io\/not-ready:NoExecute op=Exists for 300s\r\n                             node.kubernetes.io\/unreachable:NoExecute op=Exists for 300s\r\nEvents:\r\n  Type    Reason     Age   From               Message\r\n  ----    ------     ----  ----               -------\r\n  Normal  Scheduled  58s   default-scheduler  Successfully assigned default\/morevol to k8s.netico.pl\r\n  Normal  Pulling    58s   kubelet            Pulling image \"centos:7\"\r\n  Normal  Pulled     46s   kubelet            Successfully pulled image \"centos:7\" in 12.355s (12.355s including waiting)\r\n  Normal  Created    44s   kubelet            Created container centos1\r\n  Normal  Started    44s   kubelet            Started container centos1\r\n  Normal  Pulled     44s   kubelet            Container image \"centos:7\" already present on machine\r\n  Normal  Created    43s   kubelet            Created container centos2\r\n  Normal  Started    43s   kubelet            Started container centos2\r\n\r\n[root@k8s cka]# kubectl exec -it morevol -c centos1 -- touch \/centos1\/centos1file\r\n[root@k8s cka]# kubectl exec -it morevol -c centos2 -- ls \/centos2\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #3366ff;\">Managing Persistent Volumes<\/span><\/p>\n<ul>\n<li>PersistentVolumes (PV) are an API resource that represents specific storage<\/li>\n<li>PVs can be created manually, or automatically using StorageClass and storage provisioners<\/li>\n<li>Pods do not connect to PVs directly, but indirectly using PersistentVolumeClaim (PVC)<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">[root@k8s cka]# cat pv.yaml\r\nkind: PersistentVolume\r\napiVersion: v1\r\nmetadata:\r\n  name: pv-volume\r\n  labels:\r\n      type: local\r\nspec:\r\n  storageClassName: demo\r\n  capacity:\r\n    storage: 2Gi\r\n  accessModes:\r\n    - ReadWriteOnce\r\n  hostPath:\r\n    path: \"\/mnt\/mydata\"\r\n\r\n[root@k8s cka]# kubectl apply -f pv.yaml\r\npersistentvolume\/pv-volume created\r\n\r\n[root@k8s cka]# kubectl get pv\r\nNAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM               S                                                                TORAGECLASS   REASON   AGE\r\npv-volume                                  2Gi        RWO            Retain           Available                       d                                                                emo                    16s\r\npvc-3a0733ec-1795-47fe-88e1-efb340c7d90d   1Gi        RWX            Delete           Bound       default\/www-web-1   s                                                                tandard                4h41m\r\npvc-4cc89455-9cc1-4c27-b97a-e9c045a12744   1Gi        RWX            Delete           Bound       default\/www-web-2   s                                                                tandard                4h41m\r\npvc-e4dcae51-6d74-4535-93b9-40b020b120b1   1Gi        RWX            Delete           Bound       default\/www-web-0   s                                                                tandard                4h42m\r\n\r\n[root@k8s cka]# kubectl get pv -o yaml\r\napiVersion: v1\r\nitems:\r\n- apiVersion: v1\r\n  kind: PersistentVolume\r\n  metadata:\r\n    annotations:\r\n      kubectl.kubernetes.io\/last-applied-configuration: |\r\n        {\"apiVersion\":\"v1\",\"kind\":\"PersistentVolume\",\"metadata\":{\"annotations\":{},\"labels\":{\"type\":\"local\"},\"name\":\"pv-volume\"},\"spec\":{\"accessModes\":[\"ReadWriteOnce\"],\"capacity\":{\"storage\":\"2Gi\"},\"hostPath\":{\"path\":\"\/mnt\/mydata\"},\"storageClassName\":\"demo\"}}\r\n    creationTimestamp: \"2024-02-02T01:40:23Z\"\r\n    finalizers:\r\n    - kubernetes.io\/pv-protection\r\n    labels:\r\n      type: local\r\n    name: pv-volume\r\n    resourceVersion: \"42864\"\r\n    uid: 5a6fa4b7-ec31-490d-831a-3a7eca7df9e6\r\n  spec:\r\n    accessModes:\r\n    - ReadWriteOnce\r\n    capacity:\r\n      storage: 2Gi\r\n    hostPath:\r\n      path: \/mnt\/mydata\r\n      type: \"\"\r\n    persistentVolumeReclaimPolicy: Retain\r\n    storageClassName: demo\r\n    volumeMode: Filesystem\r\n  status:\r\n    phase: Available\r\n- apiVersion: v1\r\n  kind: PersistentVolume\r\n  metadata:\r\n    annotations:\r\n      hostPathProvisionerIdentity: 1d973bdb-d473-44e6-b861-681c66c35790\r\n      pv.kubernetes.io\/provisioned-by: k8s.io\/minikube-hostpath\r\n    creationTimestamp: \"2024-02-01T20:59:04Z\"\r\n    finalizers:\r\n    - kubernetes.io\/pv-protection\r\n    name: pvc-3a0733ec-1795-47fe-88e1-efb340c7d90d\r\n    resourceVersion: \"28808\"\r\n    uid: b45da20f-8c8b-4996-908c-9a3f4fe3b417\r\n  spec:\r\n    accessModes:\r\n    - ReadWriteMany\r\n    capacity:\r\n      storage: 1Gi\r\n    claimRef:\r\n      apiVersion: v1\r\n      kind: PersistentVolumeClaim\r\n      name: www-web-1\r\n      namespace: default\r\n      resourceVersion: \"28799\"\r\n      uid: 3a0733ec-1795-47fe-88e1-efb340c7d90d\r\n    hostPath:\r\n      path: \/tmp\/hostpath-provisioner\/default\/www-web-1\r\n      type: \"\"\r\n    persistentVolumeReclaimPolicy: Delete\r\n    storageClassName: standard\r\n    volumeMode: Filesystem\r\n  status:\r\n    phase: Bound\r\n- apiVersion: v1\r\n  kind: PersistentVolume\r\n  metadata:\r\n    annotations:\r\n      hostPathProvisionerIdentity: 1d973bdb-d473-44e6-b861-681c66c35790\r\n      pv.kubernetes.io\/provisioned-by: k8s.io\/minikube-hostpath\r\n    creationTimestamp: \"2024-02-01T20:59:07Z\"\r\n    finalizers:\r\n    - kubernetes.io\/pv-protection\r\n    name: pvc-4cc89455-9cc1-4c27-b97a-e9c045a12744\r\n    resourceVersion: \"28835\"\r\n    uid: 78ec7535-586f-4770-b508-b9d28128edd4\r\n  spec:\r\n    accessModes:\r\n    - ReadWriteMany\r\n    capacity:\r\n      storage: 1Gi\r\n    claimRef:\r\n      apiVersion: v1\r\n      kind: PersistentVolumeClaim\r\n      name: www-web-2\r\n      namespace: default\r\n      resourceVersion: \"28825\"\r\n      uid: 4cc89455-9cc1-4c27-b97a-e9c045a12744\r\n    hostPath:\r\n      path: \/tmp\/hostpath-provisioner\/default\/www-web-2\r\n      type: \"\"\r\n    persistentVolumeReclaimPolicy: Delete\r\n    storageClassName: standard\r\n    volumeMode: Filesystem\r\n  status:\r\n    phase: Bound\r\n- apiVersion: v1\r\n  kind: PersistentVolume\r\n  metadata:\r\n    annotations:\r\n      hostPathProvisionerIdentity: 1d973bdb-d473-44e6-b861-681c66c35790\r\n      pv.kubernetes.io\/provisioned-by: k8s.io\/minikube-hostpath\r\n    creationTimestamp: \"2024-02-01T20:58:27Z\"\r\n    finalizers:\r\n    - kubernetes.io\/pv-protection\r\n    name: pvc-e4dcae51-6d74-4535-93b9-40b020b120b1\r\n    resourceVersion: \"28669\"\r\n    uid: 4d0110fe-5376-40ad-bfad-32d22f8f5cf6\r\n  spec:\r\n    accessModes:\r\n    - ReadWriteMany\r\n    capacity:\r\n      storage: 1Gi\r\n    claimRef:\r\n      apiVersion: v1\r\n      kind: PersistentVolumeClaim\r\n      name: www-web-0\r\n      namespace: default\r\n      resourceVersion: \"20117\"\r\n      uid: e4dcae51-6d74-4535-93b9-40b020b120b1\r\n    hostPath:\r\n      path: \/tmp\/hostpath-provisioner\/default\/www-web-0\r\n      type: \"\"\r\n    persistentVolumeReclaimPolicy: Delete\r\n    storageClassName: standard\r\n    volumeMode: Filesystem\r\n  status:\r\n    phase: Bound\r\nkind: List\r\nmetadata:\r\n  resourceVersion: \"\"\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #3366ff;\">Configuring PersistentVolumeClaim<\/span><\/p>\n<ul>\n<li>PVCs allows Pods to connect to any type of storage that is provided at a<br \/>\nspecific site<\/li>\n<li>Site-specific storage needs to be created as a PersistentVolume, either manually or automatically using StorageClass<\/li>\n<li>Behind StorageClass a storage provisioner is required<\/li>\n<\/ul>\n<pre class=\"lang:default decode:true\">[root@k8s cka]# cat pvc2.yaml\r\nkind: PersistentVolumeClaim\r\napiVersion: v1\r\nmetadata:\r\n  name: pv-claim\r\nspec:\r\n  storageClassName: demo\r\n  accessModes:\r\n    - ReadWriteOnce\r\n  resources:\r\n    requests:\r\n      storage: 1Gi\r\n\r\n[root@k8s cka]# kubectl apply -f pvc2.yaml\r\npersistentvolumeclaim\/pv-claim created\r\n\r\n[root@k8s cka]# kubectl get pvc\r\nNAME        STATUS        VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE\r\npv-claim    Pending                                                                            demo           4s\r\n\r\n[root@k8s cka]# kubectl describe pvc pv-claim\r\nName:          pv-claim\r\nNamespace:     default\r\nStorageClass:  demo\r\nStatus:        Pending\r\nVolume:\r\nLabels:        &lt;none&gt;\r\nAnnotations:   &lt;none&gt;\r\nFinalizers:    [kubernetes.io\/pvc-protection]\r\nCapacity:\r\nAccess Modes:\r\nVolumeMode:    Filesystem\r\nUsed By:       &lt;none&gt;\r\nEvents:\r\n  Type     Reason              Age               From                         Message\r\n  ----     ------              ----              ----                         -------\r\n  Warning  ProvisioningFailed  9s (x2 over 21s)  persistentvolume-controller  storageclass.storage.k8s.io \"demo\" not found\r\n[root@k8s cka]#\r\n\r\n[root@k8s cka]# kubectl delete -f pvc2.yaml\r\npersistentvolumeclaim \"pv-claim\" deleted\r\n\r\n[root@k8s cka]# cat pvc1.yaml\r\nkind: PersistentVolumeClaim\r\napiVersion: v1\r\nmetadata:\r\n  name: pv-claim\r\nspec:\r\n  storageClassName: standard\r\n  accessModes:\r\n    - ReadWriteOnce\r\n  resources:\r\n    requests:\r\n      storage: 1Gi\r\n\r\n[root@k8s cka]# kubectl apply -f pvc1.yaml\r\npersistentvolumeclaim\/pv-claim created\r\n\r\n[root@k8s cka]# kubectl get pvc\r\nNAME        STATUS        VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE\r\npv-claim    Bound         pvc-3bd7c987-ee98-4d63-b8bb-93bb37ef9475   1Gi        RWO            standard       6s\r\n\r\n[root@k8s cka]# kubectl get pvc,pv\r\nNAME                              STATUS        VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE\r\npersistentvolumeclaim\/pv-claim    Bound         pvc-3bd7c987-ee98-4d63-b8bb-93bb37ef9475   1Gi        RWO            standard       3m24s\r\n\r\nNAME                                                        CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS     CLAIM               STORAGECLASS   REASON   AGE\r\npersistentvolume\/pv-volume                                  2Gi        RWO            Retain           Released   default\/pv-claim    demo                    39m\r\npersistentvolume\/pvc-3bd7c987-ee98-4d63-b8bb-93bb37ef9475   1Gi        RWO            Delete           Bound      default\/pv-claim    standard                3m24s\r\n\r\n<\/pre>\n<p>Now let&#8217;s use pv in the pod:<\/p>\n<pre class=\"lang:default decode:true\">[root@k8s cka]# cat pv-pod.yaml\r\nkind: Pod\r\napiVersion: v1\r\nmetadata:\r\n   name: pv-pod\r\nspec:\r\n  volumes:\r\n    - name: pv-storage\r\n      persistentVolumeClaim:\r\n        claimName: pv-claim\r\n  containers:\r\n    - name: pv-container\r\n      image: nginx\r\n      ports:\r\n        - containerPort: 80\r\n          name: \"http-server\"\r\n      volumeMounts:\r\n        - mountPath: \"\/usr\/share\/nginx\/html\"\r\n          name: pv-storage\r\n\r\n[root@k8s cka]# kubectl apply -f pv-pod.yaml\r\npod\/pv-pod created\r\n\r\n[root@k8s cka]# kubectl exec -it pv-pod -- touch \/usr\/share\/nginx\/html\/hellothere\r\n\r\n[root@k8s cka]# kubectl describe pv\r\nName:            pv-volume\r\nLabels:          type=local\r\nAnnotations:     pv.kubernetes.io\/bound-by-controller: yes\r\nFinalizers:      [kubernetes.io\/pv-protection]\r\nStorageClass:    demo\r\nStatus:          Released\r\nClaim:           default\/pv-claim\r\nReclaim Policy:  Retain\r\nAccess Modes:    RWO\r\nVolumeMode:      Filesystem\r\nCapacity:        2Gi\r\nNode Affinity:   &lt;none&gt;\r\nMessage:\r\nSource:\r\n    Type:          HostPath (bare host directory volume)\r\n    Path:          \/mnt\/mydata\r\n    HostPathType:\r\nEvents:            &lt;none&gt;\r\n\r\n\r\n[root@k8s cka]# kubectl get pods pv-pod -o wide\r\nNAME     READY   STATUS    RESTARTS   AGE     IP            NODE            NOMINATED NODE   READINESS GATES\r\npv-pod   1\/1     Running   0          4m41s   10.244.0.19   k8s.example.pl   &lt;none&gt;           &lt;none&gt;\r\n[<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #3366ff;\">StorageClass<\/span><\/p>\n<ul>\n<li>StorageClass is an API resource that allows storage to be automatically<br \/>\nprovisioned<\/li>\n<li>StorageClass can also be used as a property that connects PVC and PV without using an actual StorageClass resource<\/li>\n<li>Multiple StorageClass resources can co-exist in the same cluster to provide access to different types of storage<\/li>\n<li>For automatic working, one StorageClass must be set as default\n<ul>\n<li><code>kubectl patch storageclass mysc -p '{\"metadata\": {\"annotations\":{\"storageclass.kubernetes.io\/is-default-class\":\"true\"}}}'<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><span style=\"color: #3366ff;\">Using StorageClass<\/span><\/p>\n<ul>\n<li>To enable automatic provisioning, StorageClass needs a backing storage provisioner<\/li>\n<li>In the PV and PVC definition, a storageClass property can be set to connect to a specific StorageClass which is useful if multiple StorageClass resources are available<\/li>\n<li>If the storageClass property is not set, the PVC will get storage from the default StorageClass<\/li>\n<li>If also no default StorageClass is set, the PVC will get stuck in a status of Pending<\/li>\n<\/ul>\n<p><span style=\"color: #3366ff;\">Using an NFS Storage Provisioner<\/span><\/p>\n<ul>\n<li>The Storage Provisioner works with a StorageClass to automatically provide storage<\/li>\n<li>It runs as a Pod in the Kubernetes cluster, provided with access control configured through Roles, RoleBindings, and ServiceAccounts<\/li>\n<li>Once operational, you don&#8217;t have to manually create PersistentVolumes anymore<\/li>\n<\/ul>\n<p><span style=\"color: #3366ff;\">Requirements<\/span><\/p>\n<ul>\n<li>To create a storage provisioner, access permissions to the API are required<\/li>\n<li>Roles and RoleBindings are created to provide these permissions<\/li>\n<li>A ServiceAccount is created to connect the Pod to the appropriate RoleBinding<\/li>\n<\/ul>\n<p><span style=\"color: #3366ff;\">Configuring a Storage Provisioner<\/span><\/p>\n<ul>\n<li>On control: <code><\/code>\n<ul>\n<li>Ubuntu: <code>sudo apt install -y nfs-server<\/code><\/li>\n<li>RHEL\/Centos: <code>sudo dnf install -y nfs-utils<\/code><\/li>\n<\/ul>\n<\/li>\n<li>On other nodes:\n<ul>\n<li>Debian\/Ubuntu: <code>sudo apt install nfs-client<\/code><\/li>\n<li>RHEL\/Centos:\u00a0 <code>sudo dnf install nfs-utils nfs4-acl-tools\u00a0<\/code><\/li>\n<\/ul>\n<\/li>\n<li>On control:\n<ul>\n<li><code>sudo mkdir \/nfsexport <\/code><\/li>\n<li><code>sudo sh -c 'echo \"\/nfsexport *(rw,no_root_squash)\" &gt; \/etc\/exports' <\/code><\/li>\n<li><code>sudo systemctl restart nfs-server<\/code><\/li>\n<\/ul>\n<\/li>\n<li>On other nodes:<code> showmount -e control<\/code><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #3366ff;\">ConfigMap<\/span><\/p>\n<ul>\n<li>A ConfigMap is an API resource used to store site-specific data<\/li>\n<li>A Secret is a base64 encoded ConfigMap<\/li>\n<li>ConfigMaps are used to store either environment variables, startup parameters or configuration files<\/li>\n<li>When a Configuration File is used in a ConfigMap or Secret, it is mounted as a volume to provide access to its contents<\/li>\n<\/ul>\n<p><span style=\"color: #3366ff;\">Creating a ConfigMap<\/span><\/p>\n<ul>\n<li><code>echo \"hello world\" &gt; index.html<\/code><\/li>\n<li><code>kubectl create cm webindex --from-file=index.html<\/code><\/li>\n<li><code>kubectl describe cm webindex<\/code><\/li>\n<li><code>kubectl create deploy webserver --image=nginx<\/code><\/li>\n<li><code>kubectl edit deploy webserver<\/code><\/li>\n<\/ul>\n<pre class=\"lang:default decode:true \">spec.template.spec \r\nvolumes: \r\n- name: cmvol \r\n  configMap: \r\n    name: webindex \r\nspec.template.spec.containers \r\nvolumeMounts: \r\n- mountPath: \/usr\/share\/nginx\/html \r\n  name: cmvol<\/pre>\n<p>Let&#8217;s checkout how to use ConfigMap as a volume.<\/p>\n<pre class=\"lang:default decode:true\">[root@k8s cka]# echo hello world &gt; index.html\r\n\r\n[root@k8s cka]# kubectl create cm webindex --from-file=index.html\r\nconfigmap\/webindex created\r\n\r\n[root@k8s cka]# kubectl describe cm webindex\r\nName:         webindex\r\nNamespace:    default\r\nLabels:       &lt;none&gt;\r\nAnnotations:  &lt;none&gt;\r\n\r\nData\r\n====\r\nindex.html:\r\n----\r\nhello world\r\n\r\n\r\nBinaryData\r\n====\r\n\r\nEvents:  &lt;none&gt;\r\n\r\n[root@k8s cka]# kubectl create deployment webserver --image=nginx\r\ndeployment.apps\/webserver created\r\n\r\n[root@k8s cka]# kubectl edit deployments.apps webserver\r\n<\/pre>\n<p>Now edit the deployment webserver and\u00a0 add volume in the spec section in the same level as container section:<\/p>\n<pre class=\"lang:default mark:9-15 decode:true \">    spec:\r\n      containers:\r\n      - image: nginx\r\n        imagePullPolicy: Always\r\n        name: nginx\r\n        resources: {}\r\n        terminationMessagePath: \/dev\/termination-log\r\n        terminationMessagePolicy: File\r\n        volumeMounts:\r\n        - mountPath: \/usr\/share\/nginx\/html\r\n          name: cmvol\r\n      volumes:\r\n      - name: cmvol\r\n        configMap:\r\n          name: webindex\r\n      dnsPolicy: ClusterFirst\r\n      restartPolicy: Always\r\n      schedulerName: default-scheduler\r\n      securityContext: {}\r\n<\/pre>\n<p>Let&#8217;s test it:<\/p>\n<pre class=\"lang:default decode:true\">[root@k8s cka]# kubectl edit deployments.apps webserver\r\ndeployment.apps\/webserver edited\r\n\r\n[root@k8s cka]# kubectl get deploy\r\nNAME         READY   UP-TO-DATE   AVAILABLE   AGE\r\nfirstnginx   4\/4     4            4           41h\r\nwebserver    1\/1     1            1           14m\r\n\r\n[root@k8s cka]# kubectl get all\r\nNAME                             READY   STATUS    RESTARTS         AGE\r\n...\r\npod\/webserver-76d44586d-8gqhf    1\/1     Running   0                90s\r\n\r\nNAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE\r\nservice\/kubernetes   ClusterIP   10.96.0.1    &lt;none&gt;        443\/TCP   46h\r\nservice\/nginx        ClusterIP   None         &lt;none&gt;        80\/TCP    29h\r\n\r\nNAME                          DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR                                                   AGE\r\ndaemonset.apps\/deploydaemon   1         1         1       1            1           &lt;none&gt;                                                          16h\r\ndaemonset.apps\/mydaemon       1         1         1       1            1           &lt;none&gt;                                                          40h\r\n\r\nNAME                         READY   UP-TO-DATE   AVAILABLE   AGE\r\ndeployment.apps\/firstnginx   4\/4     4            4           41h\r\ndeployment.apps\/webserver    1\/1     1            1           15m\r\n\r\nNAME                                   DESIRED   CURRENT   READY   AGE\r\nreplicaset.apps\/firstnginx-d8679d567   4         4         4       41h\r\nreplicaset.apps\/webserver-667ddc69b6   0         0         0       15m\r\nreplicaset.apps\/webserver-76d44586d    1         1         1       90s\r\n\r\nNAME                   READY   AGE\r\nstatefulset.apps\/web   3\/3     29h\r\n\r\n[root@k8s cka]# kubectl exec pod\/webserver-76d44586d-8gqhf -- cat \/usr\/share\/nginx\/html\/index.html\r\nhello world\r\n<\/pre>\n<p>That proves that ConfigMap has sucessfully been mounted.<\/p>\n<p><span style=\"color: #3366ff;\">Lab: Setting up Storage<\/span><\/p>\n<ul>\n<li>Create a PersistentVolume, using the HostPath storage type to access the directory<code>\/storage<\/code><\/li>\n<li>Create a file <code>\/storage\/index.html<\/code>, containing the text <code>\"hello lab4\" <\/code><\/li>\n<li>Run a Pod that uses an Nginx image and mounts the HostPath storage on the directory <code>\/usr\/share\/nginx\/html<\/code><\/li>\n<li>On the running Pod, use <code>kubectl exec<\/code> to verify the existence of the file <code>\/usr\/share\/nginx\/html<\/code><\/li>\n<\/ul>\n<p>Solution:<\/p>\n<pre class=\"lang:default decode:true \">[root@k8s cka]# mkdir \/mnt\/storage\r\n[root@k8s cka]# vi \/mnt\/storage\/index.html\r\n[root@k8s cka]# cat \/mnt\/storage\/index.html\r\nhello\r\n\r\n[root@k8s cka]# vi lab4pv.yaml\r\n[root@k8s cka]# cat lab4pv.yaml\r\n\r\napiVersion: v1\r\nkind: PersistentVolume\r\nmetadata:\r\n  name: lab4-volume\r\n  labels:\r\n    type: local\r\nspec:\r\n  storageClassName: lab4\r\n  capacity:\r\n    storage: 10Gi\r\n  accessModes:\r\n    - ReadWriteOnce\r\n  hostPath:\r\n    path: \"\/mnt\/storage\"\r\n\r\n[root@k8s cka]# kubectl apply -f lab4pv.yaml\r\npersistentvolume\/lab4-volume created\r\n\r\n[root@k8s cka]# kubectl describe pv lab4-volume\r\nName:            lab4-volume\r\nLabels:          type=local\r\nAnnotations:     &lt;none&gt;\r\nFinalizers:      [kubernetes.io\/pv-protection]\r\nStorageClass:    lab4\r\nStatus:          Available\r\nClaim:\r\nReclaim Policy:  Retain\r\nAccess Modes:    RWO\r\nVolumeMode:      Filesystem\r\nCapacity:        10Gi\r\nNode Affinity:   &lt;none&gt;\r\nMessage:\r\nSource:\r\n    Type:          HostPath (bare host directory volume)\r\n    Path:          \/mnt\/storage\r\n    HostPathType:\r\nEvents:            &lt;none&gt;\r\n\r\n[root@k8s cka]# vi lab4pvc.yaml\r\n[root@k8s cka]# cat lab4pvc.yaml\r\napiVersion: v1\r\nkind: PersistentVolumeClaim\r\nmetadata:\r\nname: lab4-claim\r\nspec:\r\nstorageClassName: lab4\r\naccessModes:\r\n- ReadWriteOnce\r\nresources:\r\nrequests:\r\nstorage: 3Gi\r\n[root@k8s cka]# kubectl apply -f lab4pvc.yaml\r\npersistentvolumeclaim\/lab4-claim created\r\n\r\n[root@k8s cka]# kubectl get pv,pvc\r\nNAME                                                        CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS                                      SS   REASON   AGE\r\npersistentvolume\/lab4-volume                                10Gi       RWO            Retain           Bound                                                     3m5s\r\npersistentvolume\/pv-volume                                  2Gi        RWO            Retain           Releas                                                    12h\r\npersistentvolume\/pvc-3a0733ec-1795-47fe-88e1-efb340c7d90d   1Gi        RWX            Delete           Bound                                                     17h\r\npersistentvolume\/pvc-3bd7c987-ee98-4d63-b8bb-93bb37ef9475   1Gi        RWO            Delete           Bound                                                     12h\r\npersistentvolume\/pvc-4cc89455-9cc1-4c27-b97a-e9c045a12744   1Gi        RWX            Delete           Bound                                                     17h\r\npersistentvolume\/pvc-e4dcae51-6d74-4535-93b9-40b020b120b1   1Gi        RWX            Delete           Bound                                                     17h\r\n\r\nNAME                               STATUS        VOLUME                                     CAPACITY   ACCESS\r\npersistentvolumeclaim\/lab4-claim   Bound         lab4-volume                                10Gi       RWO\r\npersistentvolumeclaim\/pv-claim     Bound         pvc-3bd7c987-ee98-4d63-b8bb-93bb37ef9475   1Gi        RWO\r\npersistentvolumeclaim\/www-web-0    Terminating   pvc-e4dcae51-6d74-4535-93b9-40b020b120b1   1Gi        RWX\r\npersistentvolumeclaim\/www-web-1    Terminating   pvc-3a0733ec-1795-47fe-88e1-efb340c7d90d   1Gi        RWX\r\npersistentvolumeclaim\/www-web-2    Terminating   pvc-4cc89455-9cc1-4c27-b97a-e9c045a12744   1Gi        RWX\r\n\r\n[root@k8s cka]# vi lab4pod.yaml\r\n\r\n[root@k8s cka]# kubectl apply -f lab4pod.yaml\r\npod\/lab4-pod created\r\n\r\n[root@k8s cka]# cat lab4pod.yaml\r\napiVersion: v1\r\nkind: Pod\r\nmetadata:\r\nname: lab4-pod\r\nspec:\r\nvolumes:\r\n- name: task-pv-storage\r\npersistentVolumeClaim:\r\nclaimName: lab4-claim\r\ncontainers:\r\n- name: task-pv-container\r\nimage: nginx\r\nports:\r\n- containerPort: 80\r\nname: \"http-server\"\r\nvolumeMounts:\r\n- mountPath: \"\/usr\/share\/nginx\/html\"\r\nname: task-pv-storage\r\n\r\n[root@k8s cka]# kubectl exec lab4-pod -- cat \/usr\/share\/nginx\/html\/index.html\r\nhello\r\n<\/pre>\n<p>The example yaml files you can find in the kubernetes documentation:<\/p>\n<p>https:\/\/kubernetes.io\/docs\/home\/ -&gt; persistent volume -&gt; Configure a Pod to Use a PersistentVolume for Storage<\/p>\n","protected":false},"excerpt":{"rendered":"<p>.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[99],"tags":[],"_links":{"self":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5250"}],"collection":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/comments?post=5250"}],"version-history":[{"count":21,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5250\/revisions"}],"predecessor-version":[{"id":5474,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/posts\/5250\/revisions\/5474"}],"wp:attachment":[{"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/media?parent=5250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/categories?post=5250"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/miro.borodziuk.eu\/index.php\/wp-json\/wp\/v2\/tags?post=5250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}