how to mount a path as non root user in kubernetes

4/10/2020

I deployed a mysql monitor application image in kubernetes cluster which run as non root user. When I tried to mount a path to make the data persistent,its overriding the directory(creates a new directory by deleting everything inside that path) in which my application configuration files has to be present.Even I tried using init container still,i am not able to mount it.

my docker file:
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/mysql/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"]
my deployment 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/monitor/mysql/data
          name: volume
      volumes:
      - name: volume
        persistentVolumeClaim:
          claimName: mem-pvc1
      initContainers:
      - name: permissionsfix
        image: alpine:latest
        command: ["/bin/sh", "-c"]
        args:
          - chown -R 1000:1000 /home/sql/mysql/enterprise/monitor/ && chmod -R 777 /home/sql/mysql/enterprise/monitor/ ;
        volumeMounts:
        - name: volume
          mountPath: /home/sql/mysql/enterprise/monitor
output:
[sql@mypod-775764db45-bzs8n enterprise]$ cd monitor/mysql
[sql@mypod-775764db45-bzs8n mysql]$ ls
LICENSE      LICENSE.router  README.meb     bin   docs     lib  my-large.cnf   my-small.cnf  new  runtime  support-files  var
LICENSE.meb  README          README.router  data  include  man  my-medium.cnf  my.cnf        run  share    tmp
[sql@mypod-775764db45-bzs8n mysql]$ cd data
[sql@mypod-775764db45-bzs8n data]$ ls
mypod-775764db45-bzs8n.err
-- Sowmiya
kubernetes

1 Answer

4/10/2020

This doesn't seem related to mounting as a non-root user, but more so that mounting a volume over an existing directory will result in that directory looking as if it is empty (or containing whatever happens to be on the volume already). If you have configuration stored on a non-volume that you would like to be on the volume, then you will need to mount the volume to a different location (so it doesn't overwrite your local configuration) and copy that configuration to the mounted volume location. You can do this in an init container, but be careful not to overwrite the volume contents on every startup of the container.

-- snormore
Source: StackOverflow