K8S MySql configmap - Read-only file system

12/29/2021

I'm trying to pass mysql configuration with kubernetes configmap and PV , PVC and i'm getting error.

chown: /var/lib/mysql/..data: Read-only file system
chown: /var/lib/mysql/crud.sql: Read-only file system
chown: /var/lib/mysql/..2021_12_29_12_32_17.053559530: Read-only file system
chown: /var/lib/mysql/..2021_12_29_12_32_17.053559530: Read-only file system
chown: /var/lib/mysql/: Read-only file system
chown: /var/lib/mysql/: Read-only file system

if im not initContainers using the im getting error:

2021-12-29 12:49:05+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.27-1debian10 started.
chown: changing ownership of '/var/lib/mysql/': Read-only file system
chown: changing ownership of '/var/lib/mysql/..data': Read-only file system
chown: changing ownership of '/var/lib/mysql/crud.sql': Read-only file system
chown: changing ownership of '/var/lib/mysql/..2021_12_29_12_43_00.339135384': Read-only file system
chown: changing ownership of '/var/lib/mysql/..2021_12_29_12_43_00.339135384/crud.sql': Read-only file system

Deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.deployment.mysql.name }}
  namespace: {{ .Values.namespace }}
spec:
  selector:
    matchLabels:
      app: {{ .Values.deployment.mysql.name }}
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: {{ .Values.deployment.mysql.name }}
    spec:
      # initContainers:
      # - name: chmod-er
      #   image: busybox:latest
      #   command:
      #   - /bin/chown
      #   - -R
      #   - "999:999" 
      #   - /var/lib/mysql
      #   volumeMounts:
      #   - name: cm2
      #     mountPath: /var/lib/mysql

      containers:
      - image: {{ .Values.deployment.mysql.image }}
        name: {{ .Values.deployment.mysql.name }}
        securityContext:
          runAsUser: 0
        env:
         - name: MYSQL_ROOT_PASSWORD
           valueFrom: 
             secretKeyRef:
              name: mysql-secret
              key: mysql-root-password
        ports:
        - containerPort: {{ .Values.deployment.mysql.port }}
          name: {{ .Values.deployment.mysql.name }} 
        volumeMounts:
        - name: cm2
          mountPath: /var/lib/mysql/
          readOnly: false
      volumes:
      - name: mysqlvolume
        persistentVolumeClaim:
          claimName: mysqlvolume
          readOnly: false
      - name: cm2
        configMap:
          name: cm2

as you can see i tried with initContainers but sill got the same error.

pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysqlvolume
spec:
  accessModes:
    - ReadWriteOnce
  capacity:
    storage: 1Gi
  hostPath:
    path: C:\Users\ib151w\.minikube\volume

pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysqlvolume
  namespace: {{ .Values.namespace }}
  namespace: app
spec:
  storageClassName: standard
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 50Mi

configmap.yaml

apiVersion: v1
data:
  crud.sql: |-
    /*!40101 SET NAMES utf8 */;
    /*!40014 SET FOREIGN_KEY_CHECKS=0 */;
    /*!40101 SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
    /*!40111 SET SQL_NOTES=0 */;
    CREATE DATABASE /*!32312 IF NOT EXISTS*/ crud /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
    USE crud;

    DROP TABLE IF EXISTS books;
    CREATE TABLE `books` (
      `id` int NOT NULL AUTO_INCREMENT,
      `first_name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
      `last_name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
      `email` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
      `phone` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
      `department` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
      `manager` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
      `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
      `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
kind: ConfigMap
metadata:
  creationTimestamp: "2021-12-29T10:24:41Z"
  name: cm2
  namespace: app
  resourceVersion: "6526"
  uid: 31ab7ef8-94a4-41d3-af19-ddfceb04e124

im just trying to pass this configuration to the pod when it starting soo the mysql pod will be with the databases i want .

-- ITBYD
configmap
kubernetes
mysql

1 Answer

12/29/2021

You're mounting configMap(cm2), and k8s mounts configMaps as readonly. Did you mean to mount mysqlvolume under /var/lib/mysql/ and mount cm2 somewhere else? Mysql /var/lib/mysql is a data directory where MySQL writes tablespace data and it's not where you mount configMap

If so:

volumeMounts:
        - name: cm2
        - mountPath: /var/lib/mysql/

This should be changed to

volumeMounts:
        - name: mysqlvolume
        - mountPath: /var/lib/mysql/

volumeMounts:
        - name: cm2
        - mountPath: ~/db_scripts ( or any other path)

You also need to execute the command mysql < ~/db_scripts/crud.sql on container to create tables.

-- Jaswanthi Kolla
Source: StackOverflow