gpt4 book ai didi

docker - Kubernetes 无法在 WSL2 镜像中挂载卷

转载 作者:行者123 更新时间:2023-12-05 03:43:26 25 4
gpt4 key购买 nike

我正在尝试在我的 WSL2 Linux 发行版中设置一个 MSSQL 服务器,我在其中为我的 .mdf 安装了一个卷。 - 和 .ldf -文件。但是,我无法让 Kubernetes 看到我的包含上述文件的文件夹。

我的文件存储在 C:\WindowsFolder\data 中在我的主机 (Windows) 上,这允许 WSL2 在路径 /mnt/c/WindowsFolder/data 上看到它们(Linux)。

如果我运行以下 yaml 文件,kubectl设置所有我的数据 - 我将其装入的文件夹 (/data) 是空的。

apiVersion: apps/v1
kind: Deployment
metadata:
name: my-database
labels:
app.kubernetes.io/name: my-deployment
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: label-identifier
template:
metadata:
labels:
app.kubernetes.io/name: label-identifier
spec:
hostname: "database"
securityContext:
fsGroup: 0
containers:
- name: database
image: "mcr.microsoft.com/mssql/server:2019-latest"
imagePullPolicy: IfNotPresent
ports:
- containerPort: 1433
protocol: TCP
env:
- name: "ACCEPT_EULA"
value: "Y"
- name: "MSSQL_DATA_DIR"
value: /data
- name: "MSSQL_PID"
value: "Developer"
- name: "SA_PASSWORD"
value: "SuperSecret123!"
volumeMounts:
- name: "myvolume"
mountPath: /data

volumes:
- name: "myvolume"
hostPath:
path: "/mnt/c/windowsFolder/Database"

然后我尝试在我的 WSL2 中启动一个 docker 容器 - 它按预期工作,但从长远来看这不是一个好的解决方案:

wsl.exe #Enter WSL2

docker run -d --name sql-t1 -e "ACCEPT_EULA=Y" \
-e "SA_PASSWORD=SuperSecret123!" -p 1433:1433 \
-v /mnt/c/windowsFolder/Database:/data \
mcr.microsoft.com/mssql/server:2019-latest

docker ps #find my containerID

docker exec -it <containerId> bash #step into docker container

> ls /data #shows my files correctly

WSL2 可以通过 docker 在 Kubernetes 指定的相同路径上正确挂载,但它在 Kubernetes 中不起作用。

有什么建议吗?或者我可以尝试什么?

编辑 1:

我做了一个 docker inspect <Kubectl's WSL container>看看它是否有任何线索:

[
{
"MountLabel": "",
"HostConfig": {
"Binds": [
"/mnt/c/windowsFolder/Database:/data",
...
],
},
"VolumeDriver": "",
"VolumesFrom": null,
"Isolation": ""
},
"Mounts": [
{
"Type": "bind",
"Source": "/mnt/c/windowsFolder/Database",
"Destination": "/data",
"Mode": "",
"RW": true,
"Propagation": "rprivate"
}
],
"Config": {
...
"Volumes": null,
...
}
}
]

编辑 2:

我注意到该文件夹​​的 chmod 为 755 而不是 777。我通过添加 initContainer 来解决这个问题并删除安全组,但仍然没有帮助:

apiVersion: apps/v1
kind: Deployment
metadata:
name: my-database
labels:
app.kubernetes.io/name: my-deployment
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: label-identifier
template:
metadata:
labels:
app.kubernetes.io/name: label-identifier
spec:
hostname: "database"
containers:
- name: database
image: "mcr.microsoft.com/mssql/server:2019-latest"
imagePullPolicy: IfNotPresent
ports:
- containerPort: 1433
protocol: TCP
env:
- name: "ACCEPT_EULA"
value: "Y"
- name: "MSSQL_DATA_DIR"
value: /data
- name: "MSSQL_PID"
value: "Developer"
- name: "SA_PASSWORD"
value: "SuperSecret123!"
volumeMounts:
- name: "myvolume"
mountPath: /data

#this was added
initContainers:
- name: mssql-data-folder-permissions
image: "busybox:latest"
command: ["/bin/chmod","-R","777", "/data"]
volumeMounts:
- name: "myvolume"
mountPath: /data

volumes:
- name: "myvolume"
hostPath:
path: "/mnt/c/windowsFolder/Database"

编辑 3:

根据@ovidiu-buligan 的要求:

kubectl get events -A给出以下输出:

NAMESPACE   LAST SEEN   TYPE     REASON              OBJECT                                     MESSAGE
default 2m15s Normal Scheduled pod/myProject-database-7c477d65b8-mmh7h Successfully assigned default/myProject-database-7c477d65b8-mmh7h to docker-desktop
default 2m16s Normal Pulling pod/myProject-database-7c477d65b8-mmh7h Pulling image "mcr.microsoft.com/mssql/server:2019-latest"
default 88s Normal Pulled pod/myProject-database-7c477d65b8-mmh7h Successfully pulled image "mcr.microsoft.com/mssql/server:2019-latest" in 47.2350549s
default 88s Normal Created pod/myProject-database-7c477d65b8-mmh7h Created container database
default 87s Normal Started pod/myProject-database-7c477d65b8-mmh7h Started container database
default 2m16s Normal SuccessfulCreate replicaset/myProject-database-7c477d65b8 Created pod: myProject-database-7c477d65b8-mmh7h
default 2m16s Normal ScalingReplicaSet deployment/myProject-database Scaled up replica set myProject-database-7c477d65b8 to 1

kubectl describe pod myProject-database-7c477d65b8-mmh7h给出以下输出:

Name:         myProject-database-7c477d65b8-mmh7h
Namespace: default
Priority: 0
Node: docker-desktop/192.168.65.4
Start Time: Tue, 06 Apr 2021 13:03:18 +0200
Labels: app.kubernetes.io/name=StatefulSet-database
pod-template-hash=7c477d65b8
Annotations: <none>
Status: Running
IP: 10.1.0.10
IPs:
IP: 10.1.0.10
Controlled By: ReplicaSet/myProject-database-7c477d65b8
Containers:
database:
Container ID: docker://f768710e7436d4c813913fa22a20091cb3fb77e1ecfbe2232b0ec6037eef3dbb
Image: mcr.microsoft.com/mssql/server:2019-latest
Image ID: docker-pullable://mcr.microsoft.com/mssql/server@sha256:ec5492b0b3f9c0707fddd37f0bd3d47d3ebea94a3054afb8b50e9e746d1e5f37
Port: 1433/TCP
Host Port: 0/TCP
State: Running
Started: Tue, 06 Apr 2021 13:04:07 +0200
Ready: True
Restart Count: 0
Environment:
ACCEPT_EULA: Y
MSSQL_DATA_DIR: /data
MSSQL_LOG_DIR: /log
MSSQL_PID: Developer
SA_PASSWORD: SuperSecret123!
Mounts:
/data from storage-volume-claim (rw)
/var/run/secrets/kubernetes.io/serviceaccount from default-token-gcd5j (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
storage-volume-claim:
Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
ClaimName: database-pvc
ReadOnly: false
default-token-gcd5j:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-gcd5j
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 3m6s default-scheduler Successfully assigned default/myProject-database-7c477d65b8-mmh7h to docker-desktop
Normal Pulling 3m6s kubelet Pulling image "mcr.microsoft.com/mssql/server:2019-latest"
Normal Pulled 2m18s kubelet Successfully pulled image "mcr.microsoft.com/mssql/server:2019-latest" in 47.2350549s
Normal Created 2m18s kubelet Created container database
Normal Started 2m17s kubelet Started container database

最佳答案

已找到解决方案!

Docker Desktop 创建了一个文件夹来存储所有内容。

资源管理器中的以下路径,\\wsl$\docker-desktop-data\version-pack-data\community\kubelet\,等于/var/lib/ 在 .yaml 文件中。

这意味着,您可以编写以下内容:

...
hostPath:
path: "/var/lib/kubelet/myProject/"
type: DirectoryOrCreate
...

这将在 \\wsl$\docker-desktop-data\version-pack-data\community\kubelet\myProject 中提供一个文件夹。这将按照您的意愿行事。

您可能希望使用 Windows 开发人员模式创建指向此位置的符号链接(symbolic link)(设置 -> 更新和安全 -> 开发人员 -> 开发人员模式)。

关于docker - Kubernetes 无法在 WSL2 镜像中挂载卷,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66850830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com