Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to keep data persistent through Volume in K8s foundation

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article will explain in detail how to maintain data persistence through Volume on the basis of K8s, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

All writes to the file system by a running container occur at the writable layer of its hierarchical file system. Once the container is finished, all writes are discarded. If the data needs to be stored for a long time, then the container data needs to be persisted.

Kubernetes is similar to Docker in that it provides storage support through Volume. Volume is defined on Pod and can be mounted to the same or different paths by multiple containers in Pod. The concept of Volume in Kubernetes is similar to that of Volume in Docker, but not exactly the same. The specific differences are as follows:

The life cycle of Volume in Kubernetes is the same as that of Pod, but not related to the life cycle of the container. When the container terminates or restarts, the data in the Volume is not lost.

The Volume is cleaned only when the Pod is deleted. And whether data is lost depends on the specific type of Volume, for example, Volume data of type emptyDir will be lost, while data of type PV will not be lost.

Kubernetes currently supports a variety of Volume types, roughly as follows:

EmptyDir

Nfs

HostPath

GitRepo

PersistentVolumeClaim

Projected

PortworxVolume

Wait

Let's give a basic introduction to the common ones.

EmptryDir

If Pod is configured with EmpyDir data volume, it will exist during the life cycle of Pod. When Pod is assigned to Node, EmptyDir data volume will be created on Node and mounted to Pod container. EmpyDir data volumes exist as long as Pod exists (container deletion does not cause EmpyDir data volumes to lose data), but if the life cycle of Pod ends (Pod is deleted), EmpyDir data volumes are also deleted and permanently lost.

The example demonstrates:

ApiVersion: v1kind: Podmetadata: name: test-pod2spec: containers:-image: busybox name: test-emptydir2 command: ["sleep", "90"] volumeMounts:-mountPath: / data2 name: data-volume2 volumes:-name: data-volume2 emptyDir: {}

Kubectl create-f emptyDir2.yaml

Kubectl describe test-pod2

The k8s cluster creates EmptyDir data volumes on the node node

/ var/lib/kubelet/pods/61691e55-6740-11e9-b7fc-0050569360ba/volumes/kubernetes.io~empty-dir/data-volume2

Add a file to k8s-node1

Enter the pod container to view

Delete pod

Kubectl delete-f emptyDir2.yaml

The k8s-node1 temporary directory will be deleted

Enter the original directory, the temporary directory has been deleted

HostPath

The hostPath type maps files or directories from the node file system to pod. "when using storage volumes of type hostPath, you can also set the type field, which supports file, directory, File, Socket, CharDevice, and BlockDevice."

ApiVersion: v1kind: Podmetadata: name: test-pod2spec: containers:-image: busybox name: test-hostpath command: ["sleep", "3600"] volumeMounts:-mountPath: / test-data name: test-volume volumes:-name: test-volume hostPath: path: / data type: Directory

Kubectl create-f host_path.yaml

Go to the mounted / test-data directory and create a test file

Create a test file

View on the node on which pod is running

Ps is created in the directory of the node node and automatically generated in the container

Delete pod container kubectl delete-f host_path.yaml

Check that the file on the node node is still there

On the basis of K8s how to maintain data persistence through Volume to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report