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

oxf4rvwz  于 2023-08-03  发布在  Kubernetes
关注(0)|答案(2)|浏览(151)

在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所以就创造了这一个。(此步骤在statefulset部署之前执行)

---
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


默认存储类为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的名字?

h7appiyu

h7appiyu1#

你所做的,应该有用。确保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

字符串

qvk1mo1f

qvk1mo1f2#

对于使用existive multiple persistentvolume而不是storagecclass,请使用选择器,例如:

volumeClaimTemplates:
  - metadata:
      name: pvc-nginx
    spec:
      selector:
        matchLabels:
          app: nginx-tst-statefulset
      accessModes: [ "ReadWriteMany" ]
      resources:
        requests:
          storage: 10Mi
      storageClassName: ""

字符串

相关问题