Home > OS >  How can i still use file after kubernetes mount using pv
How can i still use file after kubernetes mount using pv

Time:01-03

When I created grafana pod to use official image and mounted /var/lib/grafana, data is not hidden I don't know why? According to what I have been studying if pvc is mounted in /var/lib/grafana directory, every file is hidden and can't access.

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: grafana-statefulset
spec:
  serviceName: grafana-service
  selector:
    matchLabels:
      app: grafana
  replicas: 1
  template:
    metadata:
      labels:
        app: grafana
    spec:
      containers:
        - name: grafana
          image: grafana/grafana:latest
          volumeMounts:
          - mountPath: "/var/lib/grafana"
            name: grafana-var
      securityContext:
        runAsUser: 472
        fsGroup: 472
  volumeClaimTemplates:
    - metadata:
        name: grafana-var
      spec:
        accessModes: ["ReadWriteMany"]
        storageClassName: nks-nas-csi
        resources:
          requests:
            storage: 2Gi
[dev1-user@master-dev-kube-cluster migration]$ k exec -it grafana-statefulset-0 -- sh
/usr/share/grafana $
/usr/share/grafana $ ls -l /var/lib/grafana/
total 912
drwxr-x---    3 grafana  root          4096 Jan  2 08:00 alerting
drwx------    2 grafana  root          4096 Jan  2 08:00 csv
drwxr-x---    2 grafana  root          4096 Jan  2 08:00 file-collections
-rw-r-----    1 grafana  root        909312 Jan  3 01:20 grafana.db
drwxr-xr-x    2 grafana  root          4096 Jan  2 08:00 plugins
drwx------    2 grafana  root          4096 Jan  2 08:00 png

But i can see and access well /var/lib/grafana directory data

However when I created the image separately, the files in the directories I mounted were hidden and inaccessible.

### First Stage
FROM busybox:latest

RUN mkdir /var/aaaa

COPY ./main.go /

RUN mv main.go /var/aaaa
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: busybox
spec:
  serviceName: busybox
  selector:
    matchLabels:
      app: busybox
  replicas: 1
  template:
    metadata:
      labels:
        app: busybox
    spec:
      containers:
        - name: busybox
          image: busy/test:busybox
          imagePullPolicy: "Always"
          command:
          - sleep
          - "86400"
          volumeMounts:
          - mountPath: /var/aaaa
            name: www
  volumeClaimTemplates:
    - metadata:
        name: www
      spec:
        accessModes: ["ReadWriteMany"]
        storageClassName: nks-nas-csi
        resources:
          requests:
            storage: 2Gi
[dev1-user@master-dev-kube-cluster migration]$ k exec -it busybox-0 -- sh
/ #
/ #
/ # ls -l /var/aaaa/
total 0
/ #

There are no main.go file in /var/aaaa directory

The point of this article is not statefulset, it's just a question that came up while testing.

How can I keep using every directory data after mount like grafana official image and how grafana do that?

CodePudding user response:

You can use a file that has been mounted to a PersistentVolume (PV) in Kubernetes by accessing it through the path specified in the PV's mountPath field. For example, if your PV has a mountPath of /mnt/pv, you can access the file at /mnt/pv/.

Keep in mind that in order to access a PV, you will need to have a Pod that is using the PV. The Pod can then access the PV using the mountPath.

Here is an example of how you might use a PV in a Pod: apiVersion: v1 kind: Pod metadata: name: mypod spec: containers:

  • name: mycontainer image: myimage volumeMounts:
    • name: mypv mountPath: /mnt/pv volumes:
  • name: mypv persistentVolumeClaim: claimName: mypvclaim

In this example, the PV is mounted to the /mnt/pv directory in the container. You can then access the file in the PV by using the path /mnt/pv/.

CodePudding user response:

How can I keep using every directory data after mount like grafana official image and how grafana do that?

The official image only has plugin in /var/lib/grafana. Those additional directories that you saw; will automatically be created by grafana during start-up if not exist.

  • Related