How to Change the UNIX File Ownership and Permission for a File Copied to the Azure Blob Container?

25 Views Asked by At

I have a MySQL database dump file, and I want to copy it to a Kubernetes pod running a MySQL image in AKS, so that I can import the database.

I first created an Azure Blob Container, configured it as a Persistent Volume (PV), set up the Persistent Volume Claim (PVC) from the PV, and then mounted it to /var/lib/mysql in the pod that ran the MySQL image.

Since MySQL was very strict that /var/lib/mysql must be empty when MySQL started to create a new and empty database, I kept the Blob Container empty, and let the pod start (so that an empty database was created) before uploading the database dump file to the Blob Container (i.e. the dump file was copied to /var/lib/mysql of the MySQL pod). I then tried to login to the pod using:

kubectl exec -it <pod_name> -- /bin/bash

Next, I tried to import the database by running:

cd /var/lib/mysql
mysql -u root -p <db_name> < <database_dump_file_name>

But I got the permission denied error, because the database dump file had root user ownership and 640 permission, but the MySQL pod had to run as User ID 1000 and Group ID 999, meaning that I could not change the ownership and permission of the database dump file. What can I do to get rid of the permission denied error?

1

There are 1 best solutions below

0
GreenPenguin On

I found a clumsy solution, the essence is to run another pod as root without MySQL, and change the database dump file's ownership and permission there:

  1. Delete the MySQL pod and deployment using kubectl delete.
  2. Create another pod which will be run by root, and the same PVC is mounted to a directory, say /mnt/temp. The image can be a simple Ubuntu image, the important thing is that the pod should be doing something so it does not complete. To do so, this infinite loop may be added to the .yaml file for the pod:
        command:
          - '/bin/bash'
          - '-c'
          - |
            while true; do echo $(date); sleep 1; done
  1. Apply the pod using kubectl apply.
  2. When the pod is running, login to it using kubectl exec -it pod/<pod_name> -- /bin/bash
  3. Perform the following commands in the pod:
cd /mnt/dir
chmod 644 <dumpfile>
chown 1000:root <dumpfile>
  1. Delete the pod using kubectl delete.
  2. Start the MySQL pod again.
  3. When the MysSQL pod is running, login to the pod.
  4. Perform the following commands in the pod to import the DB:
cd /var/lib/mysql
mysql -u root -p <db_name> < <dumpfile>
rm <dumpfile>