laitimes

Kubernetes Basic Self-Study Series | Volume explains

author:A communicator who loves programming

Video source: Station B "Trying to build Kubernetes at the end of 2021 to mastery - Happy Appetizers in 2022"

While learning, I sorted out the teacher's course content and test notes, and shared with you, infringement is deleted, thank you for your support!

Attached is a summary of the Kubernetes Basic Self-Study Series | A blog that summarizes _COCOgsta - the CSDN blog

The lifecycle of a file on a container disk is short,000, which makes it problematic to run important applications in containers. First, when the container crashes, the kubelet restarts it, but the files in the container are lost - the container restarts in a clean state (the original state of the image). Second, when multiple containers are running simultaneously in a Pod, files often need to be shared between those containers. The Volume abstraction in Kubernetes solves these problems well

background

A volume in Kubernetes has a definite lifespan — the same as the Pod that encapsulates it. Therefore, the life of the volume is longer than all containers in the Pod, and the data is still saved when the container is restarted. Of course, when the Pod no longer exists, the volume will no longer exist. Perhaps more importantly, Kubernetes supports multiple types of volumes, and Pods can use any number of volumes at the same time

The type of volume

Kubernetes supports the following types of volumes:

  • awsElasticBlockStore azureDisk azureFile cephfs csi downwardAPI emptyDir
  • fc flocker gcePersistentDisk gitRepo glusterfs hostPath iscsi local nfs
  • persistentVolumeClaim projected portworxVolume quobyte rbd scaleIO secret
  • storageos vsphereVolume

emptyDir

When a Pod is assigned to a node, an emptyDir volume is created first, and the volume exists for as long as the Pod is running on that node. As the name of the volume states, it was initially empty. A container in a Pod can read and write the same file in an emptyDir volume, although the volume can be mounted to the same or different path in each container. When a Pod is deleted from a node for any reason, the data in emptyDir is permanently deleted

Note: Container crashes do not remove pods from nodes, so data in an 'emptyDir' volume is safe in the event of a container crash

The usage of emptyDir is:

  • Scratch space, such as for disk-based merge sorting, as a checkpoint when calculating crash recovery over a long period of time
  • When the web server container provides data, the files extracted by the content organizer container are saved
apiVersion: batch/v1
kind: Job
metadata:
  name: jobs-empty
spec:
  template:
    spec:
      restartPolicy: Never
      initContainers:
        - name: job-1
          image: busybox:1.34.1
          command:
            - 'sh'
            - '-c'
            - >
              for i in 1 2 3;
              do
                echo "job-1 `date`";
                sleep 1s;
              done;
              echo job-1 GG > /srv/input/code
          volumeMounts:
            - mountPath: /srv/input/
              name: input
        - name: job-2
          image: busybox:1.34.1
          command:
            - 'sh'
            - '-c'
            - >
              for i in 1 2 3;
              do
                echo "job-2 `date`";
                sleep 1s;
              done;
              cat /srv/input/code &&
              echo job-2 GG  > /srv/input/output/file
          volumeMounts:
            - mountPath: /srv/input/
              name: input
            - mountPath: /srv/input/output/
              name: output
      containers:
        - name: job-3
          image: busybox:1.34.1
          command:
            - 'sh'
            - '-c'
            - >
              echo "job-1 and job-2 completed";
              sleep 3s;
              cat /srv/output/file
          volumeMounts:
            - mountPath: /srv/output/
              name: output
      volumes:
        - name: input
          emptyDir: {}
        - name: output
          emptyDir: {}
           

hostPath

HostPath volumes mount files or directories from the host node's file system to a cluster

HostPath is used for the following purposes:

  • Run a container that needs access to Docker internally; use the hostPath of /var/lib/docker
  • Run cAdvisor in the container; hostPath using /dev/cgroups
  • Allowing a pod specifies whether a given hostPath should exist before the pod runs, whether it should be created, and in what form it should exist

In addition to the required path attribute, the user can specify a type for the hostPath volume

value behavior
An empty string (default) is used for backward compatibility, which means that no checks are performed until the hostPath volume is mounted.
DirectoryOrCreate If nothing exists on the given path, an empty directory will be created there as needed, with permissions set to 0755, with the same group and ownership as kubelet.
Directory The directory must exist under the given path
FileOrCreate If nothing exists on the given path, an empty file is created as needed with permissions set to 0644, with the same group and ownership as the Kubelet.
File The file must exist under the given path
Socket A UNIX socket must exist under the given path
CharDevice A character device must exist under the given path
BlockDevice A block device must exist under the given path

Use this volume type to be aware because:

  • Because the files on each node are different, pods with the same configuration (for example, created from podTemplate) may behave differently on different nodes
  • When Kubernetes adds resource-aware scheduling on a schedule, the resources used by hostPath cannot 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 a privileged container, or modify file permissions on the host to write to the hostPath volume
apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
  - image: wangyanglinux/myapp:v1
    name: test-container
    volumeMounts:
    - mountPath: /test-pd
      name: test-volume
  volumes:
  - name: test-volume
    hostPath:
      # directory location on host
      path: /data
      # this field is optional
      type: Directory