Moment For Technology

Kubernetes storage volume

Posted on June 26, 2022, 3:54 a.m. by Anya Ratti
Category: The back-end Tag: kubernetes

Container file on disk life cycle is short, this makes the running in the important application in the container will appear some problems, first of all, when the container breakdown, kubelet will restart it, but the container file will be lost - container with a clean state mirror (initial state) restart, second, at the same time run multiple container is in the pod, These containers often need to share files with each other, and the Volume abstraction in Kubernetes solves these problems nicely.

background

Volumes in Kubernetes have an explicit lifetime -- the same as the Pod that encapsulates them. Therefore, volumes live longer than any container in Pod. Data is preserved when the container is restarted, and of course, volumes cease to exist when pods do not exist. Pod can use any number of volumes simultaneously.

The type of volume

Kubernetes supports the following types of volumes:

  • awsElasticBlockStore azureDisk azureFile cephfs csi downwardAPI enptyDir
  • fc flocker gcePersistenDisk gitRepo glusterfs hostPath iscsi local nfs
  • persistenVolumeClaim prejected portworxVolume quobyte rbd scaleIO secret
  • storageos vsphereVolume

empthDir

  • Temporary space, for example, for disk-based merge sort
  • Used as a checkpoint for calculating long crash recovery
  • When the Web server container serves the data, the files extracted by the content manager container are saved
apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: k8s.gcr.io/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /cache
      name: cache-volume
  - name: liveness-exec-container
    image: busybox
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh"."-c"."sleep 6000s"]
    voluemMounts:
    - mountPath: /test
      name: cache-volume
  volumes:
  - name: cahce-volume
    emptyDir: {}
Copy the code

hostPath

The hostPath volume mounts files or directories in the file system of the primary node to the cluster

HostPath is used as follows:

  • Run the container inside the Docker that you need to access; use/var/lib/dockerhostPath
  • Run cAdvisor in a container; use/dev/cgroupshostPath

In addition to the required path attribute, the user can also specify type for the hostPaht volume

value behavior
Empty strings (the default) are used for backward compatibility, which means that no checks are performed before the hostPath volume is mounted
DirectoryOrCreate If nothing exists on a given path, an empty directory is created there as needed, with permission set to 0755 and the same combined ownership as Kubelet
Directory A directory must exist under the given path
FileOrCreate If nothing exists on a given path, an empty file is created as needed, with permission set to 0644, with the same combined ownership as Kubelet
File A file must exist under the given path
Socket A UNIX socket must exist under the given path
CharDevice A character device must exist under a given path
BlockDevice A block device must exist under a given path

Be careful when using this type because:

  • Because the files on each node are different, a POD with the same configuration (such as one created from the podTemplate) might behave differently on different nodes
  • When Kubernetes adds resource-aware scheduling as planned, the resources used by hostPath will not be considered
  • Files or directories created on the underlying host can only be written by root. You need to run the process as root in the privileged container or change the file permissions on the host to write to the hostPath volume
apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: k8s.gcr.io/test-webserver
    name: test-container
    volumeMounts:
    - mountPath: /test-pd
      name: test-volume
  volumes:
  - name: cahce-volume
    hostPaht:
      # directory location on host
      path: /data
      # this field is optional
      type: Directory
Copy the code
Search
About
mo4tech.com (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.