使用 minikube 在 kubernetes 中将文件从主机挂载/复制到 Pod
Mount / copy a file from host to Pod in kubernetes using minikube
我正在编写一个 kubectl
配置来启动图像并将文件复制到容器中。
我需要 /
中的文件 Config.yaml
,因此 /Config.yaml
需要是一个有效文件。
我需要在 Pod 启动之前在 Pod 中放置该文件,因此 kubectl cp
不起作用。
我的本地文件夹中有 Config2.yaml
,我正在启动 pod,如下所示:
kubectl apply -f pod.yml
下面是我的 pod.yml 文件。
apiVersion: v1
kind: Pod
metadata:
name: python
spec:
containers:
- name: python
image: mypython
volumeMounts:
- name: config
mountPath: /Config.yaml
volumes:
- name: config
hostPath:
path: Config2.yaml
type: File
如果我尝试这样使用它也会失败:
- name: config-yaml
mountPath: /
subPath: Config.yaml
#readOnly: true
如果您只需要 config.yaml 中包含的信息从 Pod 创建时就出现在 Pod 中,请改用 configMap。
创建一个包含存储在 config.yaml 中的所有数据的 configMap,并将其装载到 pod 中的正确路径中。这对 read/write 不起作用,但对只读数据
非常有效
您可以在此处尝试 postStart
生命周期处理程序以在 pod 启动之前验证文件。
请参考here
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
volumeMounts:
- mountPath: /config.yaml
name: config
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "apt update && apt install yamllint -y && yamllint /config.yaml"]
volumes:
- name: config
hostPath:
path: /tmp/config.yaml
type: File
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
如果config.yaml
无效。 Pod 不会启动。
我正在编写一个 kubectl
配置来启动图像并将文件复制到容器中。
我需要 /
中的文件 Config.yaml
,因此 /Config.yaml
需要是一个有效文件。
我需要在 Pod 启动之前在 Pod 中放置该文件,因此 kubectl cp
不起作用。
我的本地文件夹中有 Config2.yaml
,我正在启动 pod,如下所示:
kubectl apply -f pod.yml
下面是我的 pod.yml 文件。
apiVersion: v1
kind: Pod
metadata:
name: python
spec:
containers:
- name: python
image: mypython
volumeMounts:
- name: config
mountPath: /Config.yaml
volumes:
- name: config
hostPath:
path: Config2.yaml
type: File
如果我尝试这样使用它也会失败:
- name: config-yaml
mountPath: /
subPath: Config.yaml
#readOnly: true
如果您只需要 config.yaml 中包含的信息从 Pod 创建时就出现在 Pod 中,请改用 configMap。
创建一个包含存储在 config.yaml 中的所有数据的 configMap,并将其装载到 pod 中的正确路径中。这对 read/write 不起作用,但对只读数据
非常有效您可以在此处尝试 postStart
生命周期处理程序以在 pod 启动之前验证文件。
请参考here
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
volumeMounts:
- mountPath: /config.yaml
name: config
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "apt update && apt install yamllint -y && yamllint /config.yaml"]
volumes:
- name: config
hostPath:
path: /tmp/config.yaml
type: File
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
如果config.yaml
无效。 Pod 不会启动。