K8S MySql configmap - 只读文件系统

K8S MySql configmap - Read-only file system

我正在尝试使用 kubernetes configmap 和 PV、PVC 传递 mysql 配置,但出现错误。

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

如果我没有 initContainers 使用 im 出现错误:

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

如您所见,我尝试使用 initContainers 但仍然出现相同的错误。

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

我只是想在 pod 启动时将此配置传递给它,所以 mysql pod 将包含我想要的数据库。

您正在挂载 configMap(cm2),k8s 以只读方式挂载 configMaps。您的意思是将 mysqlvolume 安装在 /var/lib/mysql/ 下并在其他地方安装 cm2 吗? Mysql /var/lib/mysql 是一个数据目录,MySQL 写入 tablespace 数据,而不是挂载 configMap

如果是:

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

这应该改为

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

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

您还需要在容器上执行命令mysql < ~/db_scripts/crud.sql来创建表。