k8s Error: failed to prepare subPath for volumeMount "custom-logo" of container "grafana"

前言

使用 k8s 挂载卷文件时,使用了 hostPathtype: 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 文件。

posted @   牛奔  阅读(527)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
历史上的今天:
2020-05-23 laravel-echo-server 启动报错 [ioredis] Unhandled error event: ReplyError: NOAUTH Authentication required.
2020-05-23 laravel proc_get_status() has been disabled for security reasons
2020-05-23 laravel The Process class relies on proc_open, which is not available on your PHP installation.
2019-05-23 Laravel 虚拟开发环境 Homestead 密码
2019-05-23 优化mysql
2019-05-23 MySQL 整型
2019-05-23 MySQL索引原理以及查询优化
点击右上角即可分享
微信分享提示