先说下问题,以WSL2作为后端服务,在Kubernetes中挂载卷的时候(比如在hostPath中),书写Windows的路径是个很麻烦的事。

    先看个示例:

    1. apiVersion: apps/v1
    2. kind: Deployment
    3. metadata:
    4. name: nginx
    5. namespace: nginx
    6. labels:
    7. nginx-app: nginx
    8. spec:
    9. replicas: 1
    10. selector:
    11. matchLabels:
    12. nginx-app: nginx
    13. template:
    14. metadata:
    15. labels:
    16. nginx-app: nginx
    17. spec:
    18. containers:
    19. - name: nginx
    20. image: nginx
    21. imagePullPolicy: IfNotPresent
    22. ports:
    23. - containerPort: 80
    24. volumeMounts:
    25. - mountPath: /usr/share/nginx/html
    26. name: nginx-data
    27. volumes:
    28. - name: nginx-data
    29. hostPath:
    30. path: D:/Users/quanzaiyu/.docker/datas/nginx/html
    31. type: Directory

    看到 hostPath.path 节点,我最开始写的是Windows的路径形式,不过很遗憾,没有成功。
    image.png
    看到报错信息,提示:

    1. Error: Error response from daemon: invalid mode: /usr/share/nginx/html

    说明路径解析有问题,于是换了种写法:

    1. volumes:
    2. - name: nginx-data
    3. hostPath:
    4. path: /d/Users/quanzaiyu/.docker/datas/nginx/html
    5. type: Directory

    结果这次报错:

    1. MountVolume.SetUp failed for volume "nginx-data" : hostPath type check failed: /d/Users/quanzaiyu/.docker/datas/nginx/html is not a directory

    image.png
    还是不成功啊。

    于是想着估计得用WSL中的路径,于是进入WSL,查看了Windows的路径:

    1. C:\Windows\System32>wsl -l
    2. 适用于 Linux Windows 子系统分发版:
    3. Ubuntu (默认)
    4. docker-desktop
    5. docker-desktop-data
    6. C:\Windows\System32>wsl -d docker-desktop
    7. DESKTOP-KR4DIJ4:/tmp/docker-desktop-root/mnt/host/c/Windows/System32# cd /
    8. DESKTOP-KR4DIJ4:/# ls
    9. bin lib run
    10. dev lost+found sbin
    11. docker-desktop-deploy-version media srv
    12. docker-desktop-proxy mnt sys
    13. etc opt tmp
    14. home proc usr
    15. init root var
    16. DESKTOP-KR4DIJ4:/# cd mnt
    17. DESKTOP-KR4DIJ4:/mnt# ls
    18. host version-pack
    19. DESKTOP-KR4DIJ4:/mnt# cd host
    20. DESKTOP-KR4DIJ4:/mnt/host# ls
    21. c d e f g wsl
    22. DESKTOP-KR4DIJ4:/mnt/host# cd d
    23. DESKTOP-KR4DIJ4:/mnt/host/d# ls
    24. $RECYCLE.BIN MinGW System Volume Information projects
    25. BOOT PHPWAMP_IN3 Users
    26. Document Program Files WSL
    27. Games Recovery Workplace

    看样子,Windows的磁盘都是挂载到WSL中的 /mnt/host 下。

    于是继续修改:

    1. volumes:
    2. - name: nginx-data
    3. hostPath:
    4. path: /mnt/host/d/Users/quanzaiyu/.docker/datas/nginx/html
    5. type: Directory

    结果很崩溃,仍然是报路径不存在。

    各种Google各种Stack Overflow,最终还是在GitHub中找到了相关的issue,花了整整一个下午,泪奔到崩溃😭,下面是解决方案。

    解决此问题,来自于一个issue:Kubernetes Volumes not correctly mounted with WSL2
    image.png
    可以看到,需要配置为如下路径:

    1. volumes:
    2. - name: nginx-data
    3. hostPath:
    4. path: /run/desktop/mnt/host/d/Users/quanzaiyu/.docker/datas/nginx/html
    5. type: Directory

    这次,终于成功启动了:
    image.png
    太不容易了,这种问题官方文档居然没有收录,还得在issue中寻找答案。

    在Windows对应的路径下创建文件:
    image.png
    访问 http://localhost:30002/ 可以看到 index.html 中的内容,说明Volume挂载成功:
    image.png

    完整的YAML如下:

    1. apiVersion: v1
    2. kind: Namespace
    3. metadata:
    4. name: nginx
    5. ---
    6. apiVersion: apps/v1
    7. kind: Deployment
    8. metadata:
    9. name: nginx
    10. namespace: nginx
    11. labels:
    12. nginx-app: nginx
    13. spec:
    14. replicas: 1
    15. selector:
    16. matchLabels:
    17. nginx-app: nginx
    18. template:
    19. metadata:
    20. labels:
    21. nginx-app: nginx
    22. spec:
    23. containers:
    24. - name: nginx
    25. image: nginx
    26. imagePullPolicy: IfNotPresent
    27. ports:
    28. - containerPort: 80
    29. volumeMounts:
    30. - mountPath: /usr/share/nginx/html
    31. name: nginx-data
    32. volumes:
    33. - name: nginx-data
    34. hostPath:
    35. path: /run/desktop/mnt/host/d/Users/quanzaiyu/.docker/datas/nginx/html
    36. type: Directory
    37. ---
    38. apiVersion: v1
    39. kind: Service
    40. metadata:
    41. name: nginx
    42. namespace: nginx
    43. spec:
    44. type: NodePort
    45. ports:
    46. - port: 80
    47. targetPort: 80
    48. nodePort: 30002
    49. selector:
    50. nginx-app: nginx

    弄完之后,整个人都崩了,无心coding,一直磨洋工到了下班😂。

    其他相关内容: