将 Windows 本地文件夹挂载到 pod 中

ngr*_*son 7 kubernetes kubernetes-pvc docker-desktop

我在本地 Kubernetes 环境中运行带有 SQL Server 的 Ubuntu 容器,并在 Windows 笔记本电脑上使用 Docker Desktop。现在我尝试将C:\data\sql包含数据库文件的本地文件夹 ( ) 安装到 pod 中。为此,我在 Kubernetes 中配置了持久卷和持久卷声明,但它似乎没有正确安装。我没有看到错误或任何内容,但是当我使用docker exec -it并检查数据文件夹进入容器时,它是空的。我希望本地文件夹中的文件出现在已安装的文件夹“数据”中,但事实并非如此。

PV、PVC 或 pod 中是否配置错误?

这是我的 yaml 文件:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: dev-customer-db-pv
  labels:
    type: local
    app: customer-db
    chart: customer-db-0.1.0
    release: dev
    heritage: Helm
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /C/data/sql
Run Code Online (Sandbox Code Playgroud)
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: dev-customer-db-pvc
  labels:
    app: customer-db
    chart: customer-db-0.1.0
    release: dev
    heritage: Helm
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi
Run Code Online (Sandbox Code Playgroud)
apiVersion: apps/v1
kind: Deployment
metadata:
  name: dev-customer-db
  labels:
    ufo: dev-customer-db-config
    app: customer-db
    chart: customer-db-0.1.0
    release: dev
    heritage: Helm
spec:
  selector:
    matchLabels:
      app: customer-db
      release: dev
  replicas: 1
  template:
    metadata:
      labels:
        app: customer-db
        release: dev
    spec:
      volumes:
        - name: dev-customer-db-pv
          persistentVolumeClaim:
            claimName: dev-customer-db-pvc
      containers:
      - name: customer-db
        image: "mcr.microsoft.com/mssql/server:2019-latest"
        imagePullPolicy: IfNotPresent
        volumeMounts:
          - name: dev-customer-db-pv
            mountPath: /data
        envFrom:
          - configMapRef:
              name: dev-customer-db-config
          - secretRef:
              name: dev-customer-db-secrets
Run Code Online (Sandbox Code Playgroud)

起初,我尝试在 pod 中定义一个没有 PV 和 PVC 的卷,但当我尝试从已安装的数据文件夹中读取文件时,出现访问被拒绝错误。

spec:
      volumes:
        - name: dev-customer-db-data
          hostPath:
            path: C/data/sql
      containers:
        ...        
        volumeMounts:
          - name: dev-customer-db-data
            mountPath: data
Run Code Online (Sandbox Code Playgroud)

我也尝试过安装 Helm,--set volumePermissions.enabled=true但这并没有解决访问被拒绝的错误。

小智 8

根据GitHub for Docker中的此信息,WSL 2 中不支持主机路径卷。

因此,可以使用下一个解决方法

我们只需附加/run/desktop/mnt/host到主机上的初始路径即可/c/data/sql。在这种情况下不需要 PersistentVolume 和 PersistentVolumeClaim - 只需删除它们即可。

spec.volumes根据Kubernetes站点上有关hostPath配置的信息对Deployment进行了更改:

volumes:
- name: dev-customer-db-pv
  hostPath:
    path: /run/desktop/mnt/host/c/data/sql
    type: Directory
Run Code Online (Sandbox Code Playgroud)

data应用这些更改后,可以在 pod 的文件夹中找到这些文件,因为mountPath: /data