0Pricing
Docker & Kubernetes for Developers · 课时

持久卷与持久卷声明

了解如何使用 PV 和 PVC 在 Kubernetes 中动态配置和使用存储。

持久卷与持久卷声明 是 CoddyKit 上的免费 Docker & Kubernetes for Developers 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Docker & Kubernetes for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Docker & Kubernetes for Developers 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Ephemeral Pods, Persistent Data

In Kubernetes, Pods are designed to be temporary and replaceable. If a Pod crashes or is rescheduled, any data stored directly within it is lost.

This ephemeral nature is great for stateless applications, but what about applications that need to store data persistently, like databases or file servers?

Introducing Persistent Volumes

A Persistent Volume (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically by Kubernetes.

It's an abstraction of the underlying storage (like a cloud disk, NFS share, or local disk), making it available for use by Pods without them needing to know the storage's specifics.

PV Capacity & Access Modes

Each PV has specific characteristics:

  • Capacity: The size of the storage, e.g., 10Gi.
  • Access Modes: How the storage can be mounted by Pods.
  • Reclaim Policy: What happens to the volume after a Pod is done using it.

These properties help Kubernetes match storage requests to available volumes.

How Pods Access Storage

PV Access Modes define how the volume can be mounted:

  • ReadWriteOnce (RWO): The volume can be mounted as read-write by a single node.
  • ReadOnlyMany (ROX): The volume can be mounted as read-only by many nodes.
  • ReadWriteMany (RWX): The volume can be mounted as read-write by many nodes.

Not all storage types support all modes. For example, a local disk typically only supports RWO.

Handling Released PVs

A PV's Reclaim Policy dictates what happens to the underlying storage when the PV is released from its claim:

  • Retain: Manual reclamation. Data remains, administrator must manually delete.
  • Delete: The underlying storage is automatically deleted along with the PV. This is common for dynamically provisioned volumes.
  • Recycle: (Deprecated) Wipes the volume and makes it available again.

Retain is useful for critical data that needs manual review before deletion.

Requesting Persistent Storage

A Persistent Volume Claim (PVC) is a request for storage by a user or application.

Think of it like a request for a specific type and size of storage. Instead of directly interacting with PVs, Pods request storage through PVCs.

PVCs provide a layer of abstraction, allowing developers to request storage without knowing the underlying infrastructure details.

Matching Storage Requests

When a PVC is created, Kubernetes tries to find a suitable PV to bind it to. This process is called binding.

It looks for PVs that meet the PVC's requirements:

  • Capacity (size)
  • Access Modes
  • Storage Class (if specified)

Once bound, the PV is exclusively reserved for that PVC.

Defining a Basic PV

Here's how you might define a simple hostPath Persistent Volume. This type uses a directory on the node's filesystem, primarily for single-node testing.

Try running this example (kubectl apply -f pv.yaml):

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 1Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  hostPath:
    path: "/mnt/data"

Requesting Storage with a PVC

Now, let's create a Persistent Volume Claim that requests 1Gi of storage with ReadWriteOnce access. It will bind to our my-pv if available.

Try running this example (kubectl apply -f pvc.yaml):

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Attaching PVC to a Pod

Finally, let's see how a Pod can use the storage provided by our PVC. We reference the PVC in the Pod's volumes section and then mount it into a container.

Try running this example (kubectl apply -f pod.yaml):

apiVersion: v1
kind: Pod
metadata:
  name: my-app-pod
spec:
  volumes:
    - name: my-storage
      persistentVolumeClaim:
        claimName: my-pvc
  containers:
    - name: my-container
      image: busybox
      command: ["sh", "-c", "echo 'Hello from CoddyKit!' > /data/message.txt && sleep 3600"]
      volumeMounts:
        - mountPath: "/data"
          name: my-storage

PV vs. PVC Roles

Understanding the distinct roles of Persistent Volumes (PVs) and Persistent Volume Claims (PVCs) is crucial for managing storage in Kubernetes.

Which statement accurately describes the primary role of a Persistent Volume (PV)?

Recap: Persistent Storage

Great job! You've learned how Kubernetes manages persistent storage:

  • Persistent Volumes (PVs) abstract physical storage resources.
  • Persistent Volume Claims (PVCs) are user requests for storage.
  • PVs have capacity, access modes (RWO, ROX, RWX), and reclaim policies (Retain, Delete).
  • PVCs bind to available PVs, and Pods then use PVCs to mount storage.

This system allows applications to reliably store data, even when Pods are recreated or moved.

常见问题解答

「持久卷与持久卷声明」课时是免费的吗?

是的 — 「持久卷与持久卷声明」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Docker & Kubernetes for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Docker & Kubernetes for Developers 课程共包含 4 节课。

「持久卷与持久卷声明」这节课中我会学到什么?

了解如何使用 PV 和 PVC 在 Kubernetes 中动态配置和使用存储。 你通过在浏览器中直接运行的动手代码来练习 Docker & Kubernetes for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Docker & Kubernetes for Developers 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Docker & Kubernetes for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「持久卷与持久卷声明」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Docker & Kubernetes for Developers 课中编写并运行代码吗?

能。每节 Docker & Kubernetes for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 持久卷与持久卷声明
  2. 使用 StatefulSets 管理有状态应用
  3. 使用 ConfigMaps 与 Secret 管理配置
  4. 存储类与动态供给
← 返回 Docker & Kubernetes for Developers