将Hospath添加到Kubernetes Statefulset

Din*_*esh 3 docker kubernetes

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

Jcs*_*Jcs 5

Yes but it is definitely for testing purposes.

First you need to create as many Persistent Volume as you need

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"
...
Run Code Online (Sandbox Code Playgroud)

Afterwards, add this VolumeClaimsTemplate to your Statefulset

volumeClaimTemplates:
- metadata:
    name: my-hostpath-volume
  spec:
    storageClassName: manual
    accessModes: ["ReadWriteOnce"]
    resources:
      requests:
        storage: 5Gi
    selector:
      matchLabels:
        type: local
Run Code Online (Sandbox Code Playgroud)

Another solution is using the hostpath dynamic provisioner. You do not have to create the PV bin advance but this remains a "proof-of-concept solution" as well and you will have to build and deploy the provisioner in your cluster.

  • 请注意,这仅适用于单节点集群。如果您在多节点集群中尝试此操作,您将遇到 pod 声明存在于完全不同节点上的主机路径卷的问题。 (2认同)