将 Hospath 添加到 Kubernetes Statefulset

Adding Hospath to a Kubernetes Statefulset

在 Kubernetes 中是否可以在 Statefulset 中添加 hostPath 存储。如果可以,有人可以帮我举个例子吗?

是的,但绝对是为了测试目的。

首先,您需要根据需要创建尽可能多的持久卷

kind: PersistentVolume
apiVersion: v1
metadata:
  name: hp-pv-001
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/tmp/data01"

kind: PersistentVolume
apiVersion: v1
metadata:
  name: hp-pv-002
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/tmp/data02"
...

然后,将此 VolumeClaimsTemplate 添加到您的 Statefulset

volumeClaimTemplates:
- metadata:
    name: my-hostpath-volume
  spec:
    storageClassName: manual
    accessModes: ["ReadWriteOnce"]
    resources:
      requests:
        storage: 5Gi
    selector:
      matchLabels:
        type: local

另一个解决方案是使用 hostpath dynamic provisioner。您不必提前创建 PV bin,但这仍然是 "proof-of-concept solution",您将必须在集群中构建和部署配置器。

StatefulSet 的 hostPath 卷只能在 single-node 集群中使用,例如为了发展。重新安排 pod 将不起作用。

相反,请考虑对此类用例使用 Local Persistent Volume

The biggest difference is that the Kubernetes scheduler understands which node a Local Persistent Volume belongs to. With HostPath volumes, a pod referencing a HostPath volume may be moved by the scheduler to a different node resulting in data loss. But with Local Persistent Volumes, the Kubernetes scheduler ensures that a pod using a Local Persistent Volume is always scheduled to the same node.

考虑使用 local static provisioner for this, the Getting Started 指南包含如何在不同环境中使用它的说明。