Kubernetes-Pod具有未绑定的立即PersistentVolumeClaims

时间:2020-03-20 12:30:45

标签: kubernetes kubernetes-pod kubernetes-statefulset

我正在使用mysql kubernetes statefulset,我已将PV映射到主机目录(CentOS 8 VM),但是却收到“ pod具有未绑定的即时PersistentVolumeClaims”

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql-container
spec:
  serviceName: mysql
  replicas: 1 
  selector:
    matchLabels:
      app: mysql-container
  template:
    metadata:
      labels:
        app: mysql-container
    spec:
      containers:
      - name: mysql-container
        image: mysql:dev
        imagePullPolicy: "IfNotPresent"
        envFrom:
          - secretRef:
             name: prod-secrets
        ports:
        - containerPort: 3306
         # container (pod) path
        volumeMounts:
          - name: mysql-persistent-storage
            mountPath: /var/lib/mysql


      volumes:
        - name: mysql-persistent-storage
          persistentVolumeClaim:
           claimName: mysql-pvc
 volumeClaimTemplates:
    - metadata:
        name: data
      spec:

        storageClassName: localstorage
        accessModes: ["ReadWriteOnce"]
        resources:
          requests:
            storage: 3Gi
        selector:
           matchLabels:
            type: local

存储类为默认值,PV中无事件

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:

  name: localstorage

provisioner: kubernetes.io/no-provisioner
volumeBindingMode: Immediate
reclaimPolicy: Delete
allowVolumeExpansion: True


kind: PersistentVolume
apiVersion: v1
metadata:
  name: mysql-01
  labels:
    type: local
spec:
  storageClassName: localstorage
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/mysql01"

---
kind: PersistentVolume
apiVersion: v1
metadata:
  name: mysql-02
  labels:
    type: local
spec:
  storageClassName: localstorage
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/mysql02"

存储类别是默认类别

get sc
NAME                     PROVISIONER                    RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
localstorage (default)   kubernetes.io/no-provisioner   Delete          Immediate           true                   35m

PVC也没有显示任何事件:

    Name:          data-mysql-0
    Namespace:     default
    StorageClass:  localstorage
    Status:        Pending
    Volume:        mysql-storage
    Labels:        app=mysql
    Annotations:   <none>
    Finalizers:    [kubernetes.io/pvc-protection]
    Capacity:      0
    Access Modes:
    VolumeMode:    Filesystem
    Mounted By:    mysql-0
    Events:        <none>


Name:            mysql-01
Labels:          type=local
Annotations:     kubectl.kubernetes.io/last-applied-configuration:
                   {"apiVersion":"v1","kind":"PersistentVolume","metadata":{"annotations":{},"labels":{"type":"local"},"name":"mysql-01"},"spec":{"accessMode...
Finalizers:      [kubernetes.io/pv-protection]
StorageClass:    localstorage
Status:          Available
Claim:
Reclaim Policy:  Retain
Access Modes:    RWO
VolumeMode:      Filesystem
Capacity:        10Gi
Node Affinity:   <none>
Message:
Source:
    Type:          HostPath (bare host directory volume)
    Path:          /mnt/mysql01
    HostPathType:
Events:            <none>


Name:            mysql-02
Labels:          type=local
Annotations:     kubectl.kubernetes.io/last-applied-configuration:
                   {"apiVersion":"v1","kind":"PersistentVolume","metadata":{"annotations":{},"labels":{"type":"local"},"name":"mysql-02"},"spec":{"accessMode...
Finalizers:      [kubernetes.io/pv-protection]
StorageClass:    localstorage
Status:          Available
Claim:
Reclaim Policy:  Retain
Access Modes:    RWO
VolumeMode:      Filesystem
Capacity:        10Gi
Node Affinity:   <none>
Message:
Source:
    Type:          HostPath (bare host directory volume)
    Path:          /mnt/mysql02
    HostPathType:
Events:            <none>

Pod处于待处理状态:

>       Events:
>          Type     Reason            Age                From               Message
>          ----     ------            ----               ----               -------
>          Warning  FailedScheduling  27s (x2 over 27s)  default-scheduler  error while running >"VolumeBinding" filter plugin for pod "mysql-0": pod has unbound immediate PersistentVolumeClaims

有人可以指出在这里还应该做些什么,谢谢

2 个答案:

答案 0 :(得分:11)

如果不存在匹配的PersistentVolumeClaims

PersistentVolume将无限期保持未绑定状态。 PersistentVolumeaccessModescapacity匹配。在这种情况下,capacity的PV为10Gi,而PVC的capacity3Gi

PV中的capacity需要与声明中的相同,即3Gi才能解决unbound immediate PersistentVolumeClaims的问题。

答案 1 :(得分:1)

提到的错误可能是由多种原因引起的-以下是我遇到的一些选项。


示例1

persistentvolume-controller无法找到容量为{strong>等于或大于然后等于PV中指定的值的PVC。 / p>

因此,如果我们举这个例子:

  # PVC
  resources:
     requests:
       storage: 3Gi


  # PV
  capacity:
    storage: 10Gi 

所以:
如果PV capacity >= PVC capacity,则应将PVC绑定到PV。

如果否,那么在描述PVC时,我们会在pod级别收到unbound immediate PersistentVolumeClaims错误,而在描述PVC时会收到no volume plugin matched name


示例2

PVC的数量高于PV。

例如,如果仅创建一个PV(或删除其他PV):

$ kubectl get pv
NAME             CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                                      STORAGECLASS    REASON   AGE
mongo-local-pv   50Gi       RWO            Retain           Bound    default/mongo-persistent-storage-mongo-0   local-storage            106m

我们可以看到某些工作负载(Pods或有状态集)将停留在待处理状态:

$ kubectl get pods
NAME      READY   STATUS    RESTARTS   AGE
mongo-0   2/2     Running   0          3m38s
mongo-1   0/2     Pending   0          3m23s

$ kubectl get pvc
NAME                               STATUS    VOLUME           CAPACITY   ACCESS MODES   STORAGECLASS    AGE
mongo-persistent-storage-mongo-0   Bound     mongo-local-pv   50Gi       RWO            local-storage   80m
mongo-persistent-storage-mongo-1   Pending                                              local-storage   45m

在待处理资源上,我们将得到上述错误。


示例3

如果调度程序无法将节点与PV匹配。

使用本地卷时,PV的nodeAffinity是必需的 ,并且应为集群中现有节点的值:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: local-mongo-pv
  .
  .
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - node-which-doesnt-exists  # <----- Will lead to the error

示例4

集群中已经存在具有相同名称和不同配置的旧PV,并根据它们创建了新的PVC

使用本地卷时,管理员必须手动执行清理操作,并每次都重新设置本地卷以供重用。

(*)This本地静态资源调配程序是为帮助PV生命周期而创建的。