In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
one。 Background
Storage resources play a very important role in all computing resources, and all kinds of storage resources may be used in most business scenarios. In Kubernetes, the system provides storage resources to containers in the cluster dynamically or statically through Volume. In general, we can assume that the life cycle of a container or Pod is short, and when the container is destroyed, the data inside the container is cleared at the same time. In order to persist the container data, Kubernetes introduces Volume, which is similar to Docker's Volume (Docker also has a concept of volumes, though it is somewhat looser and less managed. In Docker, a volume is simply a directory on disk or in another Container. Lifetimes are not managed and until very recently there were only local-disk-backed volumes. Docker now provides volume drivers, but the functionality is very limited for now). After the Volume is mounted by a Pod, all containers in the Pod can use the Volume. The volume types currently supported by Kubernetes can refer to the official materials at the end of the article.
two。 Examples of two kinds of Volume 2.1 emptyDir
EmptyDir: emptyDir is the most basic Volume type. Each emptyDir Volume is an empty directory on the host and can be shared by all containers in the Pod. It is persistent for containers, but not for Pod. Deleting the container does not affect it, it is deleted only when the entire Pod is deleted, and its life cycle is the same as the mounted Pod. In short, a Volume of type emptyDir is created when Pod is assigned to Node, and Kubernetes automatically allocates a directory on the Node host, so there is no need to specify the corresponding directory file on the Node host. The initial content of this directory is empty, and when Pod is removed from Node, the data in emptyDir is permanently deleted. EmptyDir is mainly used for data that does not need to be permanently retained, such as temporary directories, multi-container shared directories, and so on. Let's understand from an actual case that the yaml of Pod gysl is as follows:
ApiVersion: v1kind: Podmetadata: name: gyslspec: containers:-image: busybox name: gysl-01 volumeMounts:-mountPath: / gysl-01 name: gysl-volume args:-/ bin/sh-- c-echo "This is a test file." > / gysl-01/test.gysl Sleep 20000-image: busybox name: gysl-02 volumeMounts:-mountPath: / gysl-02 name: gysl-volume args:-/ bin/sh-- c-cat / gysl-02/test.gysl;sleep 20000 volumes:-name: gysl-volume emptyDir: {}
Create a Pod gysl and view the relevant information:
[root@k8s-m k8s-volumes] # kubectl apply-f emptyDir.yamlpod/gysl created [root@k8s-m k8s-volumes] # kubectl get podNAME READY STATUS RESTARTS AGEgysl 2 kubectl get podNAME READY STATUS RESTARTS AGEgysl 2 Running 0 10m [root@k8s-m k8s-volumes] # kubectl logs gysl gysl-02This is a test file.
In this example, we create a Pod named gysl, which contains two containers, gysl-01 and gysl-02, which simultaneously mount an emptyDir,gysl-01 named gysl-volume with the mount point / gysl-01,gysl-02 as gysl-02, and the container gysl-01 creates a test.gysl file with the content: "This is a test file." In the container gysl-02, the contents of the file created by gysl-01 are displayed successfully.
2.2 hostPath
HostPath: the main function of hostPath is to mount the files or directories of the host to the Pod container, so that the container can store data with good performance. Next, let's take Pod gysl-hostpath as an example to understand the related concepts of hostPath. The YAML file is as follows:
ApiVersion: v1kind: Podmetadata: name: gysl-hostpathspec: nodeSelector: role: test containers:-image: ubuntu:18.04 name: gysl-hostpath-container volumeMounts:-mountPath: / etc/gysl-test-01 name: gysl-02-mountPath: / etc/gysl-test-02 name: gysl-01-mountPath: / gysl-test-dir name: gysl-dir args:-/ bin/bash-- C-cat / etc/gysl-test-01 / etc/gysl-test-02 Ls-l / gysl-test-dir;sleep 3600 volumes:-hostPath: path: / root/gysl-01 name: gysl-01-hostPath: path: / root/gysl-02 name: gysl-02-hostPath: path: / root/gysl-dir name: gysl-dir
Create the following files and directories under the / root directory of Node k8s-n1:
[root@k8s-n1] # ll total usage 8 root root RW root@k8s-n1-1 root root 16 November 21 20:31 gysl-01-rw-r--r-- 1 root root 16 November 21 20:31 gysl-02drwxr-xr-x 2 root root 51 November 21 20:32 gysl-dir
The contents of the two files are as follows:
[root@k8s-n1 ~] # cat gysl-01This is gysl-01 [root@k8s-n1 ~] # cat gysl-02This is gysl-02
The contents of the directory gysl-dir are as follows:
[root@k8s-n1] # ll gysl-dir/ Total usage 12 root root RW root@k8s-n1-1 root root 16 November 21 20:32 gysl-01-rw-r--r-- 1 root root 16 November 21 20:32 gysl-02-rw-r--r-- 1 root root 16 November 21 20:32 gysl-03
Assign a label to Node k8s-n1 and apply the Pod:
[root@k8s-m k8s-Volumes] # kubectl label node k8s-n1 role=testnode/k8s-n1 labeled [root@k8s-m k8s-Volumes] # kubectl apply-f hostPath.yamlpod/gysl-hostpath created [root@k8s-m k8s-Volumes] # kubectl get pod-o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODEgysl-hostpath 1 k8s-n1 1 Running 0 17s 10.244.1.177 k8s-n1 [root@ K8s-m k8s-Volumes] # kubectl logs gysl-hostpathThis is gysl-02This is gysl-01total 12 root root Nov 21 12:32 gysl-01-rw-r--r-- 1 root root 16 Nov 21 12:32 gysl-02-rw-r--r-- 1 root root 16 Nov 21 12:32 gysl-03
In this example, I mount the hostPath file named gysl-02 to the container file / etc/gysl-test-01, the hostPath file named gysl-01 to the container file / etc/gysl-test-02, and the hostPath directory named gysl-dir to / gysl-test-dir. Through the logs command, it is not difficult to find that the goal has been achieved.
This mount is more persistent than emptyDir unless there is a failure in the Node. However, in addition to mounting some configuration files / binaries, this kind of mount is generally not used, because such a mount operation increases the coupling between Pod files and Node host files, which is not conducive to unified management.
three。 Summary
3.1 in the process of configuring volume, the specific mount path needs to be configured according to certain rules. For example, a file or directory needs to write an absolute path. If you do not configure according to the rules, the following error will occur:
Warning Failed 8s (x3 over 20s) kubelet, k8s-n1 Error: Error response from daemon: create ~ / gysl: "~ / gysl-dir" includes invalid characters for a local volume name, only "[a-zA-Z0-9] [a-zA-Z0-9]" are allowed. If you intended to pass a host directory, use absolute path
EmptyDir and hostPath are two common types of volume, which need to be configured on a case-by-case basis. Other types of volume can be configured by referring to the above two types and official documentation, which will be given at the end of the article.
four。 Related materials
5.1 basic concepts of Volumes
5.2 Volume types supported by Kubernetes
5.3 YAML files covered in the article
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.