简体   繁体   中英

Mounting a path creates a new directory in kubernetes

I am facing issues when mounting a path.My objective is to make the data persistent even when the pod restarts.But its creating a new directory which doesnt have any of my config files.

FROM centos:7
ENV DIR /binaries
ENV PASS admin
WORKDIR ${DIR}
COPY libstdc++-4.8.5-39.el7.x86_64.rpm ${DIR} 
COPY numactl-libs-2.0.12-3.el7.x86_64.rpm ${DIR}
COPY mysqlmonitor-8.0.18.1217-linux-x86_64-installer.bin ${DIR}
RUN yum install -y libaio && yum -y install gcc && yum -y install gcc-c++ && yum -y install compat-libstdc++-33 && yum -y install libstdc++-devel && yum -y install elfutils-libelf-devel && yum -y install glibc-devel && yum -y install libaio-devel && yum -y install sysstat
RUN yum install -y gcc && yum install -y make && yum install -y apr-devel && yum install -y openssl-devel && yum install -y java
RUN rpm -ivh numactl-libs-2.0.12-3.el7.x86_64.rpm
RUN useradd sql
RUN chown sql ${DIR}
RUN chmod 777 ${DIR}
RUN chmod 755 /home/sql
USER sql
WORKDIR ${DIR}
RUN ./mysqlmonitor-8.0.18.1217-linux-x86_64-installer.bin --installdir /home/sql/mysql/enterprise/monitor --mode unattended --tomcatport 18080 --tomcatsslport 18443 --adminpassword ### --dbport 13306
RUN rm -rf /binaries/*
VOLUME /home/sql/mysql/enterprise/monitor/mysql/data
ENTRYPOINT ["/bin/bash", "-c", "/home/sql/mysql/enterprise/monitor/mysqlmonitorctl.sh start && tail -f /home/sql/mysql/enterprise/monitor/apache-tomcat/logs/mysql-monitor.log"]
Deployment yaml file
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mypod
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mem     
  template:
    metadata:
      labels:
        app: mem
    spec:
      containers:
      - name: mem
        image: 22071997/mem
        command: 
        volumeMounts:
        - mountPath: /home/sql/mysql/enterprise/mysql/data
          name: volume
      volumes:
      - name: volume
        persistentVolumeClaim:
          claimName: mem-
Output:
[sql@mypod-67cb4f85b8-9km26 data]$ pwd
/home/sql/mysql/enterprise/mysql/data
[sql@mypod-67cb4f85b8-9km26 data]$ ls
[sql@mypod-67cb4f85b8-9km26 data]$

Expected output: I need all my data to be present.

Please check the following:

  • You use Dockerfile VOLUME for the purpose of persisting data from the container volume /home/sql/mysql/enterprise/monitor/mysql/data in the host volume /var/lib/docker/volumes/... (ie. for every node this data will be unique -- it's way there exists the PersistentVolume abstraction on Kubernetes)

If not:

  • There exists a PersistentVolume abstraction in the cluster (kubectl get pv). It has a valid PersistentVolume.spec.type (eg nfs, hostPath)
  • There exists a PersistentVolumeClaim abstraction (kubectl get pvc) in the cluster
  • There exists a Deployment abstraction (kubectl get deploy) in the cluster
  • PersistentVolumeClaim.spec.volumeName matches PersistentVolume.metadata.name (or PersistentVolume.spec.claimRef.name matches PersistentVolumeClaim.metadata.name)
  • Deployment.spec.template.spec.volumes[].persistentVolumeClaim.claimName matches PersistentVolumeClaim.metadata.name
  • Deployment.spec.template.spec.containers[].volumeMounts[].name matches Deployment.spec.template.spec.volumes[].name

Debugging:

  • Locate the PersistentVolume plugin mount path. Let's take my Minikube cluster as an example. I'm using:
$ kubectl get pv -o=jsonpath='{.items[0].spec.hostPath.path}'
/tmp/hostpath-provisioner/pvc-5e497ae8-943e-4651-86b9-4355c48d443dsv
  • Create a sample file:
$ touch /tmp/hostpath-provisioner/pvc-5e497ae8-943e-4651-86b9-4355c48d443d/1
$ ls /tmp/hostpath-provisioner/pvc-5e497ae8-943e-4651-86b9-4355c48d443d
1
  • Create deployment following the above mentioned checks:
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: test
  name: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: test
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: test
    spec:
      containers:
      - image: busybox
        command:
          - sleep
          - "10000"
        name: busybox
        resources: {}
        volumeMounts:
          - mountPath: /tmp/test_dir
            name: my-volume
      volumes:
      - name: my-volume
        persistentVolumeClaim:
            claimName: myclaim
  • Test whether the file exists in the designated directory:
$ kubectl exec test-6f857854db-57fsz -- ls /tmp/test_dir
1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM