在kubernetes statefulset中添加持久卷

时间:2018-04-09 09:16:33

标签: kubernetes persistent-volumes persistent-volume-claims

我是kubernetes的新手,我正在尝试在statefulset中添加PVC。 PV和PVC如下所示:

NAME            CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM                      STORAGECLASS   REASON    AGE
neo4j-backups   5Gi        RWO            Retain           Bound     default/backups-claim      manual                   1h

NAME            STATUS    VOLUME          CAPACITY   ACCESS MODES   STORAGECLASS   AGE
backups-claim   Bound     neo4j-backups   5Gi        RWO            manual         51m

基本上我希望statefulset的所有pod都能看到该卷的内容,因为备份文件存储在那里。

使用的Statefulset可以找到here

Minikube版本: minikube version: v0.25.2
Kubernetes版本:GitVersion:"v1.9.4"

1 个答案:

答案 0 :(得分:2)

如果您在volumeClaimTemplates k8中使用StatefulSet,则会进行动态配置&为每个吊舱创建一个PVC和相应的PV,因此每个吊舱都有自己的存储空间。

你想要的是创造一个PV&一个PVC并在Statefulset的所有副本中使用它。

以下是Kubernetes 1.10的示例,如何执行此操作,其中/var/www/html将由所有三个Pod共享,只需将/directory/on/host更改为计算机上的某个本地目录。我也在minikube v0.26.0上运行了这个例子

下面的例子只是一个例子来说明这个想法,但在一个真实的例子中,Pod中的流程应该知道同步访问共享存储。

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: example-pv
spec:
  capacity:
    storage: 100Gi
  # volumeMode field requires BlockVolume Alpha feature gate to be enabled.
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  storageClassName: local-storage
  local:
    path: /directory/on/host
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - minikube 
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: example-local-claim
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: local-storage
---
apiVersion: "apps/v1beta1"
kind: StatefulSet
metadata:
  name: nginx
spec:
  serviceName: nginx
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx-container 
        image: "nginx:1.12.2"
        imagePullPolicy: "IfNotPresent"
        volumeMounts: 
        - name: localvolume
          mountPath: /var/www/html
      volumes:
      - name: localvolume
        persistentVolumeClaim:
          claimName: example-local-claim