适用于Windows的Docker桌面-无法在本地硬盘上创建持久卷

时间:2020-05-15 14:18:40

标签: docker kubernetes

我想在Windows 10(C:\Postrges)上为postgres kubernetes pod创建持久卷

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

  name: localstorage

provisioner: docker.io/hostpath
volumeBindingMode: Immediate
reclaimPolicy: Delete
allowVolumeExpansion: True

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: database-persistent-volume-claim
spec:
  storageClassName: localstorage
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

---

# How do we want it implemented
apiVersion: v1
kind: PersistentVolume
metadata:
  name: local-storage
spec:
  storageClassName: localstorage
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete  
  hostPath:
    path: "/C/postgres"
    type: DirectoryOrCreate





$ kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM                                      STORAGECLASS   REASON   AGE  
local-storage                              2Gi        RWO            Delete           Available                                              localstorage            4m14s
pvc-7e1d810f-1114-4b7c-9160-2b07830c682f   2Gi        RWO            Delete           Bound       default/database-persistent-volume-claim   localstorage            4m14s

$ kubectl get pvc
NAME                               STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
database-persistent-volume-claim   Bound    pvc-7e1d810f-1114-4b7c-9160-2b07830c682f   2Gi        RWO            localstorage   5m36s

PV没有事件,对于PVC,它显示“正在等待外部供应商创建卷”

kubectl describe pvc
Name:          database-persistent-volume-claim
Namespace:     default
StorageClass:  localstorage
Status:        Bound
Volume:        pvc-7e1d810f-1114-4b7c-9160-2b07830c682f
Labels:        <none>
Annotations:   pv.kubernetes.io/bind-completed: yes
               pv.kubernetes.io/bound-by-controller: yes
               volume.beta.kubernetes.io/storage-provisioner: docker.io/hostpath
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:      2Gi
Access Modes:  RWO
VolumeMode:    Filesystem
Mounted By:    <none>
Events:
  Type    Reason                 Age    From                                                                         Message
  ----    ------                 ----   ----                                                                         -------
  Normal  ExternalProvisioning   7m24s  persistentvolume-controller                                                  waiting for a volume to be created, either by external provisioner "docker.io/hostpath" or manually created by system administrator
  Normal  Provisioning           7m24s  docker.io/hostpath_storage-provisioner_4bfa0399-b72d-4e03-ae9d-70ee4d8a7c33  External provisioner is provisioning volume for claim "default/database-persistent-volume-claim"
  Normal  ProvisioningSucceeded  7m24s  docker.io/hostpath_storage-provisioner_4bfa0399-b72d-4e03-ae9d-70ee4d8a7c33  Successfully provisioned volume pvc-7e1d810f-1114-4b7c-9160-2b07830c682f

但是在Windows资源管理器中,我看不到C盘上的postgres文件夹

PVC已分配给postgres容器

 volumes:
        - name: postgres-storage
          persistentVolumeClaim:
           claimName: database-persistent-volume-claim
      containers:
        - name: postgres
          image: postgres
          ports:
            - containerPort: 5432
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              subPath: postgres
              name: postgres-storage

Pod没有任何错误,在执行kubectl apply之前以管理员身份运行CMD,相同的配置在Linux上没有问题。有什么想法吗?

enter image description here

1 个答案:

答案 0 :(得分:1)

实际上,一切正常,Docker Desktop不会映射Windows本地存储,而是在安装Docker Desktop时创建的VM上回收空间。

可以按照this post

中的说明访问此VM。
kubectl get pvc
NAME                               STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
database-persistent-volume-claim   Bound    pvc-ceb8dfa1-37ca-48fe-b4bc-cc642faac6c4   2Gi        RWO            localstorage   33m

浏览Docker桌面虚拟机:

ls /var/lib/k8s-pvs/database-persistent-volume-claim/pvc-ceb8dfa1-37ca-48fe-b4bc-cc642faac6c4/postgres
PG_VERSION            pg_hba.conf           pg_replslot           pg_subtrans           postgresql.auto.conf
base                  pg_ident.conf         pg_serial             pg_tblspc             postgresql.conf
global                pg_logical            pg_snapshots          pg_twophase           postmaster.opts
pg_commit_ts          pg_multixact          pg_stat               pg_wal
pg_dynshmem           pg_notify             pg_stat_tmp           pg_xact

删除postgres部署后,数据库文件将保留

enter image description here