简体   繁体   English

卷安装不起作用 Kubernetes 和 WSL 2 和 Docker

[英]Volume mounts not working Kubernetes and WSL 2 and Docker

I am unable to properly mount volumes using HostPath within Kubernetes running in Docker and WSL 2. This seems to be a WSL 2 issue when mounting volumes in Kubernetes running in Docker. I am unable to properly mount volumes using HostPath within Kubernetes running in Docker and WSL 2. This seems to be a WSL 2 issue when mounting volumes in Kubernetes running in Docker. Anyone know how to fix this?有人知道怎么修这个东西吗?

Here are the steps:以下是步骤:

Deploy debug build to Kubernetes for my app.为我的应用程序部署调试版本到 Kubernetes。 Attach Visual Studio Code using the Kubernetes extension Navigate to the project folder for my application that was attached using the volume mount <= Problem Right Here使用 Kubernetes 扩展附加 Visual Studio 代码导航到使用卷安装附加的我的应用程序的项目文件夹 <= 问题就在这里

When you go and look at the volume mount nothing is there.当您 go 并查看卷挂载时,什么都没有。

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

According to the following thread, hostPath volumes are not officially supported for wsl2, yet.根据以下线程,wsl2 尚未正式支持 hostPath 卷。 They do suggest a workaround, though I had trouble getting it to work.他们确实建议了一种解决方法,尽管我很难让它工作。 I have found that prepending /run/desktop/mnt/host/c seems to work for me.我发现预先添加/run/desktop/mnt/host/c似乎对我有用。

// C:\someDir\volumeDir
hostPath:
  path: /run/desktop/mnt/host/c/someDir/volumeDir
  type: DirectoryOrCreate

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

Using @RyanDarnell's excellent answer above , here is what worked for me使用@RyanDarnell上面的出色答案,这对我有用

Objective: get message-db with password using docker build --secret running in local docker-desktop kubernetes with StatefulSet and StorageClass using Skaffold目标:使用docker build --secret在本地 docker-desktop kubernetes 中运行,使用带有 StatefulSet 和 StorageClass 的Skaffold获取带有密码的 message-db

$kubectl get nodes -o wide
NAME             STATUS   ROLES                  AGE   VERSION   INTERNAL-IP    EXTERNAL-IP   OS-IMAGE         KERNEL-VERSION                   CONTAINER-RUNTIME
docker-desktop   Ready    control-plane,master   21h   v1.21.1   <internalip>   <none>        Docker Desktop   5.4.72-microsoft-standard-WSL2   docker://20.10.7
#./k8s/storage-class.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: local-storage
  labels:
    app: postgres-database
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
reclaimPolicy: Delete
#./k8s/persistent-volume.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: local-pv1
  labels:
    app: postgres-database
spec:
  capacity:
    storage: 128Mi
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  local:
    path: /run/desktop/mnt/host/c/kubernetes-mount-path # created this folder at C:\kubernetes-mount-path
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - docker-desktop
#./k8s/stateful-set.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres-database
spec:
  selector:
    matchLabels:
      app: postgres-database
  serviceName: postgres-service
  replicas: 1
  template:
    metadata:
      labels:
        app: postgres-database
    spec:
      containers:
      - name: message-db-container
        image: message-db-test
        volumeMounts:
        - name: postgres-disk
          mountPath: /var/lib/postgresql/data
        env:
        - name: PGDATA
          value: /var/lib/postgresql/data/pgdata
        - name: POSTGRES_PASSWORD
          value: <postgres-password>
  volumeClaimTemplates:
  - metadata:
      name: postgres-disk
      labels:
        app: postgres-database
    spec:
      selector:
        matchLabels:
          app: postgres-database
      storageClassName: local-storage
      accessModes: 
      - ReadWriteOnce
      resources:
        requests:
          storage: 128Mi
#./k8s/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: postgres-loadbalancer
spec:
  selector:
    app: postgres-database
  type: LoadBalancer
  ports:
    - port: 5432
      targetPort: 5432
#./Dockerfile
# syntax = docker/dockerfile:1.3
FROM postgres:13.3-alpine3.14

RUN apk add --no-cache curl tar
RUN mkdir -p /usr/src/eventide \
  && curl -L https://github.com/message-db/message-db/tarball/v1.2.6 -o /usr/src/eventide/message-db.tgz
RUN tar -xf /usr/src/eventide/message-db.tgz --directory /usr/src/eventide

# change message_store login password
RUN --mount=type=secret,id=message-db-pass sed -i "s/WITH LOGIN/WITH LOGIN ENCRYPTED PASSWORD '$(cat /run/secrets/message-db-pass)'/g" /usr/src/eventide/message-db-message-db-759a4f3/database/roles/message-store.sql

RUN echo -e "#!/bin/sh\ncd /usr/src/eventide/message-db-message-db-759a4f3/database\n./install.sh" > /docker-entrypoint-initdb.d/rundbscripts.sh 

RUN docker-entrypoint.sh postgres --version

ENTRYPOINT docker-entrypoint.sh postgres
#./secret.txt
<postgres-password>
#./skaffold.yaml
kind: Config
apiVersion: skaffold/v2beta20
build:
  artifacts:
    - image: message-db-test
      context: .
      docker:
        secret:
          id: message-db-pass
          src: secret.txt
  local: 
    useBuildkit: true
deploy:
  kubectl:
    manifests:
    - k8s/*.yaml

run skaffold debug -v debug to startup运行skaffold debug -v debug启动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM