先说下问题,以WSL2作为后端服务,在Kubernetes中挂载卷的时候(比如在hostPath中),书写Windows的路径是个很麻烦的事。
先看个示例:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: nginx
labels:
nginx-app: nginx
spec:
replicas: 1
selector:
matchLabels:
nginx-app: nginx
template:
metadata:
labels:
nginx-app: nginx
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
volumeMounts:
- mountPath: /usr/share/nginx/html
name: nginx-data
volumes:
- name: nginx-data
hostPath:
path: D:/Users/quanzaiyu/.docker/datas/nginx/html
type: Directory
看到 hostPath.path
节点,我最开始写的是Windows的路径形式,不过很遗憾,没有成功。
看到报错信息,提示:
Error: Error response from daemon: invalid mode: /usr/share/nginx/html
说明路径解析有问题,于是换了种写法:
volumes:
- name: nginx-data
hostPath:
path: /d/Users/quanzaiyu/.docker/datas/nginx/html
type: Directory
结果这次报错:
MountVolume.SetUp failed for volume "nginx-data" : hostPath type check failed: /d/Users/quanzaiyu/.docker/datas/nginx/html is not a directory
还是不成功啊。
于是想着估计得用WSL中的路径,于是进入WSL,查看了Windows的路径:
C:\Windows\System32>wsl -l
适用于 Linux 的 Windows 子系统分发版:
Ubuntu (默认)
docker-desktop
docker-desktop-data
C:\Windows\System32>wsl -d docker-desktop
DESKTOP-KR4DIJ4:/tmp/docker-desktop-root/mnt/host/c/Windows/System32# cd /
DESKTOP-KR4DIJ4:/# ls
bin lib run
dev lost+found sbin
docker-desktop-deploy-version media srv
docker-desktop-proxy mnt sys
etc opt tmp
home proc usr
init root var
DESKTOP-KR4DIJ4:/# cd mnt
DESKTOP-KR4DIJ4:/mnt# ls
host version-pack
DESKTOP-KR4DIJ4:/mnt# cd host
DESKTOP-KR4DIJ4:/mnt/host# ls
c d e f g wsl
DESKTOP-KR4DIJ4:/mnt/host# cd d
DESKTOP-KR4DIJ4:/mnt/host/d# ls
$RECYCLE.BIN MinGW System Volume Information projects
BOOT PHPWAMP_IN3 Users
Document Program Files WSL
Games Recovery Workplace
看样子,Windows的磁盘都是挂载到WSL中的 /mnt/host
下。
于是继续修改:
volumes:
- name: nginx-data
hostPath:
path: /mnt/host/d/Users/quanzaiyu/.docker/datas/nginx/html
type: Directory
结果很崩溃,仍然是报路径不存在。
各种Google各种Stack Overflow,最终还是在GitHub中找到了相关的issue,花了整整一个下午,泪奔到崩溃😭,下面是解决方案。
解决此问题,来自于一个issue:Kubernetes Volumes not correctly mounted with WSL2
可以看到,需要配置为如下路径:
volumes:
- name: nginx-data
hostPath:
path: /run/desktop/mnt/host/d/Users/quanzaiyu/.docker/datas/nginx/html
type: Directory
这次,终于成功启动了:
太不容易了,这种问题官方文档居然没有收录,还得在issue中寻找答案。
在Windows对应的路径下创建文件:
访问 http://localhost:30002/ 可以看到 index.html 中的内容,说明Volume挂载成功:
完整的YAML如下:
apiVersion: v1
kind: Namespace
metadata:
name: nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: nginx
labels:
nginx-app: nginx
spec:
replicas: 1
selector:
matchLabels:
nginx-app: nginx
template:
metadata:
labels:
nginx-app: nginx
spec:
containers:
- name: nginx
image: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
volumeMounts:
- mountPath: /usr/share/nginx/html
name: nginx-data
volumes:
- name: nginx-data
hostPath:
path: /run/desktop/mnt/host/d/Users/quanzaiyu/.docker/datas/nginx/html
type: Directory
---
apiVersion: v1
kind: Service
metadata:
name: nginx
namespace: nginx
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30002
selector:
nginx-app: nginx
弄完之后,整个人都崩了,无心coding,一直磨洋工到了下班😂。
其他相关内容: