卷挂载不工作 Kubernetes 和 WSL 2 和 Docker

Ric*_*ane 11 windows docker kubernetes windows-subsystem-for-linux

我无法在 Docker 和 WSL 2 中运行的 Kubernetes 中使用 HostPath 正确挂载卷。在 Docker 中运行的 Kubernetes 中挂载卷时,这似乎是 WSL 2 问题。有人知道怎么修这个东西吗?

以下是步骤:

为我的应用程序将调试版本部署到 Kubernetes。使用 Kubernetes 扩展附加 Visual Studio Code 导航到我的应用程序的项目文件夹,该应用程序使用卷挂载 <= 问题就在这里

当您查看卷挂载时,什么也没有。

C:\Windows\System32>wsl -l -v

NAME STATE VERSION
Ubuntu Running 2
docker-desktop-data Running 2
docker-desktop Running 2
Docker Desktop v2.3.0.3
Kubernetes v1.16.5
Visual Studio Code v1.46.1
Run Code Online (Sandbox Code Playgroud)
====================================================================
Dockerfile
====================================================================
#
# Base image for deploying and running based on Ubuntu
#
# Support ASP.NET and does not include .NET SDK or NodeJs
# 
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-bionic AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
#
# Base image for building .NET based on Ubuntu
#
# 1. Uses .NET SDK image as the starting point 
# 2. Restore NuGet packages
# 3. Build the ASP.NET Core application
#
# Destination is /app/build which is copied to /app later on
#
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic AS build
WORKDIR /src
COPY ["myapp.csproj", "./"]
RUN dotnet restore "./myapp.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "myapp.csproj" -c Release -o /app

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic AS debug
RUN curl --silent --location https://deb.nodesource.com/setup_12.x | bash -
RUN apt-get install --yes nodejs
ENTRYPOINT [ "sleep", "infinity" ]

#
# Base image for building React based on Node/Ubuntu
#
# Destination is /app/ClientApp/build which is copied to /clientapp later
#
# NOTE: npm run build puts the output in the build directory
#
FROM node:12.18-buster-slim AS clientbuild
WORKDIR /src
COPY ./ClientApp /app/ClientApp
WORKDIR "/app/ClientApp"
RUN npm install
RUN npm run build
#
# Copy clientbuild:/app/ClientApp to /app/ClientApp
#
# Copy build:/app to /app
#
FROM base as final
WORKDIR /app/ClientApp
COPY --from=clientbuild /app/ClientApp .
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "myapp.dll"]

====================================================================
Kubernetes Manifest
====================================================================
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  selector:
    matchLabels:
      app: myapp
  replicas: 1
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: localhost:6000/myapp
        ports:
        - containerPort: 5001
        securityContext:
          privileged: true
        volumeMounts:
        - mountPath: /local
          name: local
        resources: {}        
      volumes:
      - name: local
        hostPath:
          path: /C/dev/myapp
          type: DirectoryOrCreate
      hostname: myapp
      restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
  name: myapp
spec:
  type: LoadBalancer
  ports:
  - name: http
    protocol: TCP
    port: 5001
    targetPort: 5001
  selector:
    app: myapp
Run Code Online (Sandbox Code Playgroud)

小智 36

根据以下线程,wsl2 尚未正式支持 hostPath 卷。他们确实建议了一种解决方法,尽管我无法让它发挥作用。我发现前置/run/desktop/mnt/host/c似乎对我有用。

// C:\someDir\volumeDir
hostPath:
  path: /run/desktop/mnt/host/c/someDir/volumeDir
  type: DirectoryOrCreate
Run Code Online (Sandbox Code Playgroud)

线程来源:https : //github.com/docker/for-win/issues/5325
来自线程的建议解决方法:https : //github.com/docker/for-win/issues/5325#issuecomment-567594291

  • 谢谢!我在那儿浪费了一个小时。在 Wsl distrib `/mnt/c/...` 上可以工作,但在 Windows (cmd/powershell) 上我很挣扎。 (2认同)
  • 天哪,我简直不敢相信,这是一些高级别的初始不一致,我还浪费了一个小时来找到你的答案,ty! (2认同)
  • 这是一个救星 (2认同)