如何在 kubernetes 中使用 statefulset 设置 pvc?

How to set pvc with statefulset in kubernetes?

在 GKE 上,我将 statefulset 资源设置为

---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
spec:
  serviceName: "redis"
  selector:
    matchLabels:
      app: redis
  updateStrategy:
    type: RollingUpdate
  replicas: 3
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis
        resources:
          limits:
            memory: 2Gi
        ports:
          - containerPort: 6379
        volumeMounts:
          - name: redis-data
            mountPath: /usr/share/redis
      volumes:
        - name: redis-data
          persistentVolumeClaim:
            claimName: redis-data-pvc

想用pvc所以做了这个。 (此步骤是在有状态集部署之前完成的)

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: redis-data-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

查看kubernetes中的资源时

kubectl get pvc
NAME             STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
redis-data-pvc   Bound    pvc-6163d1f8-fb3d-44ac-a91f-edef1452b3b9   10Gi       RWO            standard       132m

默认存储 Class 是 standard

kubectl get storageclass
NAME                 PROVISIONER
standard (default)   kubernetes.io/gce-pd

但是当检查statafulset 的部署状态时。总是错的。

# Describe its pod details
...
Events:
  Type     Reason                  Age                From                     Message
  ----     ------                  ----               ----                     -------
  Warning  FailedScheduling        22s                default-scheduler        persistentvolumeclaim "redis-data-pvc" not found
  Warning  FailedScheduling        17s (x2 over 20s)  default-scheduler        pod has unbound immediate PersistentVolumeClaims (repeated 2 times)
  Normal   Created                 2s (x2 over 3s)    kubelet                  Created container redis
  Normal   Started                 2s (x2 over 3s)    kubelet                  Started container redis
  Warning  BackOff                 0s (x2 over 1s)    kubelet                  Back-off restarting failed container

为什么找不到 redis-data-pvc 名称?

您所做的应该有效。确保 PersistentVolumeClaimStatefulSet 位于同一命名空间。


就是说,这是一个更简单的解决方案,可以让您更轻松地扩展到更多副本:

使用 StatefulSet 和 PersistentVolumeClaim 时,请改用 StatefulSet 中的 volumeClaimTemplates: 字段。

volumeClaimTemplates: 将用于为每个副本创建唯一的 PVC,并且它们具有以例如结尾的唯一命名。 -0 其中数字是用于 StatefulSet 中的副本的 序号

因此,改为使用像这样的 SatefuleSet 清单:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: redis
spec:
  serviceName: "redis"
  selector:
    matchLabels:
      app: redis
  updateStrategy:
    type: RollingUpdate
  replicas: 3
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis
        resources:
          limits:
            memory: 2Gi
        ports:
          - containerPort: 6379
        volumeMounts:
          - name: redis-data
            mountPath: /usr/share/redis
  volumeClaimTemplates:                     // this will be used to create PVC
  - metadata:
      name: redis-data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 10Gi