前言
使用 k8s
挂载卷文件时,使用了 hostPath
,type: File
volumeMounts:
- mountPath: /usr/share/grafana/public/img/grafana_icon.svg
name: custom-logo
subPath: grafana_icon.svg
volumes:
- hostPath:
path: /root/test/logo.svg
type: File
name: custom-logo
结果报错,kubectl describe pod
:
Error: failed to prepare subPath for volumeMount "volume-mount-name" of container "container-name"
报错原因为找不到 subPath
,随后使用了 initContainers
,在 busybox
镜像的 mkdir
命令初始化创建该目录:
apiVersion: apps/v1
kind: Deployment
metadata:
name: grafana
spec:
replicas: 1
selector:
matchLabels:
app: grafana
template:
metadata:
labels:
app: grafana
spec:
initContainers:
- name: create-directories
image: busybox
command: ['sh', '-c', 'mkdir -p /usr/share/grafana/public/img']
volumeMounts:
- name: custom-logo
mountPath: /usr/share/grafana/public/img
containers:
- name: grafana
image: grafana/grafana:latest
ports:
- containerPort: 3000
volumeMounts:
- name: custom-logo
mountPath: /usr/share/grafana/public/img/grafana_icon.svg
subPath: grafana_icon.svg
volumes:
- name: custom-logo
hostPath:
path: /root/test/logo.svg
这时 pod
状态为 Init:CrashLoopBackOff
,而 kubectl describe pod
,报错 Back-off restarting failed container
,索性就不使用 subPath
了。
最终写法
volumeMounts:
- mountPath: /usr/share/grafana/public/img/grafana_icon.svg
name: custom-logo
volumes:
- hostPath:
path: /root/test/logo.svg
type: File
name: custom-logo
成功挂载使用了 /root/test/logo.svg
文件。