首页 > 解决方案 > 如何将容器卷(非 root 用户)挂载到 Kubernetes 主机上的 root 用户?

问题描述

当我尝试将应用程序日志卷从容器挂载到主机时出现错误:不允许操作

spec:
  securityContext:
    runAsUser: 1000
    runAsGroup: 3000
    fsGroup: 2000
  initContainers:
  - name: volume-mount-permission
    image: xx.xx.xx.xx/orchestration/credit-card
    command:
    - sh
    - -c
    - chown -R 1000:1000 /opt/payara/appserver/glassfish/logs/credit-card
    - chgrp 1000 /opt/payara/appserver/glassfish/logs/credit-card
    volumeMounts:
    - name: card-corp-logs
      mountPath: /opt/payara/appserver/glassfish/logs/credit-card
      readOnly: false

  containers:
  - name: credit-card
    image: xx.xx.xx.xx/orchestration/credit-card
    imagePullPolicy: Always
    securityContext:
      privileged: true
      runAsUser: 1000
    ports:
    - name: credit-card
      containerPort: 8080
    readinessProbe:
      httpGet:
         path: /
         port: 8080
      initialDelaySeconds: 10
      periodSeconds: 5
      successThreshold: 1
    volumeMounts:
    - name: override-setting-storage
      mountPath: /p/config
    - name: credit-card-teamsite
      mountPath: /var/credit-card/teamsite/card_corp

容器路径 - /opt/payara/appserver/glassfish/logs/credit-card 到 hostPath

任何人都可以帮我解决我在部署 yml 文件中出错的地方。

标签: kubernetes

解决方案


  securityContext:
    runAsUser: 1000
    runAsGroup: 3000

意味着您不能chown 1000:1000,因为该用户不是组的成员 1000

可能你会想要运行它initContainer:runAsUser: 0允许它执行任意chown操作

您还截断了您的 YAML,该 YAML 将指定volumes:您正在挂载的YAML volumeMounts:- 您有可能尝试挂载的卷类型 - 无论您的readOnly: false声明如何 - 都无法修改。ConfigMap, Secret, Downward API 和其他一些 API 也不会响应突变请求,即使root.


推荐阅读