如何为StatefulSet静态配置卷?

时间:2018-01-15 21:34:48

标签: kubernetes google-cloud-platform google-kubernetes-engine

我正在使用Google Kubernetes Engine并希望我的StatefulSet使用我之前创建的磁盘my-app-disk,而不是为集合中的每个副本动态创建新的永久磁盘。我的配置如下:

PersistentVolume yaml:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-app-data
  labels:
    app: my-app
spec:
  capacity:
    storage: 60Gi
  accessModes:
    - ReadWriteOnce
  gcePersistentDisk:
    pdName: my-app-disk
    fsType: ext4

PersistentVolumeClaim yaml:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-app-data
  labels:
    app: my-app
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 60Gi
  selector:
    matchLabels:
      app: my-app

StatefulSet yaml:

apiVersion: apps/v1beta2
kind: StatefulSet
metadata:
  name: my-stateful-set
spec:
  serviceName: my-stateful-set
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: gcr.io/projectname/my-app:0.0.3
        ports:
        - containerPort: 80
          name: http
        volumeMounts:
        - name: my-app-data
          mountPath: /data
  volumeClaimTemplates:
  - metadata:
      name: my-app-data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 50Gi

1 个答案:

答案 0 :(得分:5)

StatefulSet会为每个广告单元创建自己的PersistentVolumeClaim,因此您不必自己创建一个。 PersistentVolumePersistentVolumeClaim将仅one to one绑定。您的PVC与您的卷绑定,因此StatefulSet创建的任何PVC都无法绑定到您的卷,因此无法使用它。

在您的情况下,您的PersistentVolumeStatefulSet应该可以解决问题。请务必删除您创建的PersistentVolumeClaim,以便它不会绑定到您的PersistentVolume。另外,请确保您的PV和下面的volumeClaimTemplates上的StatefulSet下的存储类名称设置正确,或者StatefulSet生成的PVC可能不会绑定到您的卷。

PersistentVolume.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-app-data
  labels:
    app: my-app
spec:
  capacity:
    storage: 60Gi
  storageClassName: standard
  accessModes:
    - ReadWriteOnce
  gcePersistentDisk:
    pdName: my-app-disk
    fsType: ext4

StatefulSet.yaml

apiVersion: apps/v1beta2
kind: StatefulSet
metadata:
  name: my-stateful-set
spec:
  serviceName: my-stateful-set
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: gcr.io/projectname/my-app:0.0.3
        ports:
        - containerPort: 80
          name: http
        volumeMounts:
        - name: my-app-data
          mountPath: /data
  volumeClaimTemplates:
  - metadata:
      name: my-app-data
    spec:
      selector:
        matchLabels:
          app: my-app
      storageClassName: standard
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 50Gi