目录在 Kubernetes 中的主机路径挂载方法后显示为空

Directory shows empty after hostpath mounting method in Kubernetes

我正在尝试挂载位于 /mnt/rstudio 中的名为 rstudio 的目录。但是,当我尝试使用持久卷进行挂载时,会显示目录,但不会显示 rstudio 中的文件。这是我的部署文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: rsp-deployment
spec:
  selector:
    matchLabels:
      app: rsp
  replicas: 1
  strategy: {}
  template:
    metadata:
      labels:
        app: rsp
    spec:
      nodeSelector:
        kubernetes.io/hostname: testserver.local
      volumes:
       - name: rsp-persistent-storage
         persistentVolumeClaim:
           claimName: pv-claim-rsp
      containers:
      - env:
        - name: RSP_LICENSE
          value: MY LICENSE
        image: rstudio/rstudio-server-pro:latest
        name: rstudio-server-pro
        ports:
        - containerPort: 8787
        - containerPort: 5559
        volumeMounts:
         - name: rsp-persistent-storage
           mountPath: /tmp/rstudio
        resources: {}
        securityContext:
          privileged: true
      restartPolicy: Always
status: {}

---
kind: Service
apiVersion: v1
metadata:
  name: rstudio-server-pro
spec:
  selector:
    app: rsp
  ports:
  - protocol: TCP
    name: "8787"
    port: 8787
    targetPort: 8787
  type: NodePort

而我的pv和pvc文件如下

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-volume-rsp
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/rstudio"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-claim-rsp
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: ""
  resources:
    requests:
      storage: 5Gi

我的/mnt/rstudio里面有这么多文件。

[root@test-server rstudio]# ls
launcher.conf                      launcher-mounts          launcher.pub        rserver.conf  
launcher.kubernetes.conf           launcher-mounts_working  logging.conf        rsession.conf 
launcher.kubernetes.profiles.conf  launcher.pem             notifications.conf  r-versions

但是在 pod 启动后 运行,目录显示为空。知道为什么吗?提前致谢!

LGTM。如果我用 nginx 交换图像,我会得到文件。我会检查两件事:

  1. 权限:检查文件有什么权限。您可能必须更新您的权限或 UID 才能访问这些文件。
  2. rstudio 图像是否使用该路径?它可能在启动时正在处理该文件夹。尝试挂载到不同的路径,看看是否可以看到文件。

此外,请确保您在主机路径所在的节点上启动 pod。我假设 testserver.localtest-server 相同。

HTH

您的 PVC 是否使用您创建的 PV?用

检查一下
kubectl get pvc

尝试向 pv 添加标签并使用 matchLabels 选择器以确保 PVC 实际使用您刚创建的 PV:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-volume-rsp
  labels:
    name: pv-volume-rsp  # Added
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/rstudio"

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-claim-rsp
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: ""
  resources:
    requests:
      storage: 5Gi
  selector:
    matchLabels:
      name: pv-volume-rsp  # Added