首页 > 解决方案 > 用户的mysql自定义镜像和Minikube的奇怪行为

问题描述

苏普。我创建了我的服务、部署和持久卷声明,所以我的 mysql 应该在 minikube 中工作,但它没有。我无法弄清楚为什么 minikube 外部的 docker 容器可以正常工作,但是当我尝试在 minikube 集群中使用它时,我的数据库会以某种方式清除。这是我的 Dockerfile

FROM alpine:latest
RUN apk update && apk upgrade -a -U
RUN apk add mysql mysql-client openrc supervisor
RUN chown -R mysql:mysql /var/lib/mysql/
COPY ./my.cnf /etc/
COPY ./secure_config.sh /root
RUN rc default
RUN /etc/init.d/mariadb setup
RUN /etc/init.d/mariadb start
RUN chmod 755 /root/secure_config.sh
RUN /root/secure_config.sh
RUN sed -i "s|.*bind-address\s*=.*|bind-address=0.0.0.0|g" /etc/my.cnf
RUN sed -i "s|.*bind-address\s*=.*|bind-address=0.0.0.0|g" /etc/my.cnf.d/mariadb-server.cnf
RUN sed -i "s|.*skip-networking.*|skip-networking|g" /etc/my.cnf
RUN sed -i "s|.*skip-networking.*|skip-networking|g" /etc/my.cnf.d/mariadb-server.cnf
COPY ./wpdb.sh .
COPY ./sql_launch.sh .
RUN chmod 755 /wpdb.sh
RUN chmod 755 /sql_launch.sh
COPY ./supervisord.conf /etc/
EXPOSE 3306
CMD /sql_launch.sh

wpdb.sh

mysql -e "CREATE DATABASE wordpress;"
mysql -e "CREATE USER 'admin'@'localhost' IDENTIFIED BY 'admin';"
mysql -e "CREATE USER 'lchantel'@'localhost' IDENTIFIED BY 'lchantel';"
mysql -e "CREATE USER 'pstein'@'localhost' IDENTIFIED BY 'pstein'"
mysql -e "CREATE USER 'admins_mom'@'localhost' IDENTIFIED BY 'admins_mom'"
mysql -e "DELETE FROM mysql.user WHERE user = '';"
mysql -e "SET PASSWORD FOR 'admins_mom'@'localhost' = PASSWORD('123456');"
mysql -e "SET PASSWORD FOR 'admin'@'localhost' = PASSWORD('123456');"
mysql -e "SET PASSWORD FOR 'pstein'@'localhost' = PASSWORD('123456');"
mysql -e "SET PASSWORD FOR 'lchantel'@'localhost' = PASSWORD('123456');"
mysql -e "GRANT ALL PRIVILEGES ON wordpress.* TO 'admin'@'localhost' IDENTIFIED BY 'admin';"
mysql -e "FLUSH PRIVILEGES;"

sql_launch.sh

#!bin/sh

rc default
chmod 777 /wpdb.sh && /wpdb.sh
rc-service mariadb stop
/usr/bin/supervisord -c /etc/supervisord.conf

这是我在容器中的 mysql 输出

MariaDB [(none)]> SELECT user FROM mysql.user
    -> ;
+-------------+
| User        |
+-------------+
| admin       |
| admins_mom  |
| lchantel    |
| mariadb.sys |
| mysql       |
| pstein      |
| root        |
+-------------+
7 rows in set (0.006 sec)
MariaDB [(none)]>

这是我在 minikube pod 内的输出

# rc-status
Runlevel: default
 mariadb                                                                                                                                                                      [  stopped  ]
Dynamic Runlevel: hotplugged
Dynamic Runlevel: needed/wanted
Dynamic Runlevel: manual
/ # rc-service mariadb start
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/blkio/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpu/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpu,cpuacct/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpuacct/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/cpuset/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/devices/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/freezer/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/hugetlb/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/memory/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/net_cls/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/net_cls,net_prio/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/net_prio/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/perf_event/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/pids/tasks: Read-only file system
/lib/rc/sh/openrc-run.sh: line 100: can't create /sys/fs/cgroup/systemd/tasks: Read-only file system
 * Datadir '/var/lib/mysql/' is empty or invalid.
 * Run '/etc/init.d/mariadb setup' to create new database.
 * ERROR: mariadb failed to start

所以我猜问题出在部署的 mountPath 部分的 yaml 文件中。有yaml文件

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-mysql
spec:
  capacity:
    storage: 500Mi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/home/lchantel/pv_proj/"

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-claim-mysql
  labels:
    app: mysql
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 500Mi
apiVersion: v1
kind: Service
metadata:
  name: wildboar-mysql-service
  labels:
    app: mysql
spec:
  type: ClusterIP
  selector:
    app: mysql
  ports:
    - name: mysql
      port: 3306
      targetPort: 3306
      protocol: TCP
  clusterIP: None

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: wildboar-mysql-deploy
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: wildboar-mysql-pod
          image: wildboar.mysql:latest
          ports:
            - containerPort: 3306
              name: mysql
          volumeMounts:
            - name: mysqldb-storage
              mountPath: /var/lib/mysql/
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: root
          imagePullPolicy: Never
      volumes:
        - name: mysqldb-storage
          persistentVolumeClaim:
            claimName: pv-claim-mysql

谷歌没有帮助,根本不知道我应该做什么以及我应该从哪里开始。

标签: mysqldockerkubernetesmariadbminikube

解决方案


好吧,我用下一个方法解决了我的问题

  1. 我完全重新制作了我的 mysql Dockerfile。alpine wiki 建议配置 mariadb 的方式很复杂并且不起作用。VOLUME /var/lib/mysql/命令对于在那个目录中持久化数据非常有用。
  2. 我决定将 .sh 转换为 mysql 脚本。而且,就我设法将我的数据库用于 Wordpress 而言,没有理由在 mysql 数据库中创建多个用户。创建一个管理员就足够了。这是我的 Dockerfile
FROM alpine:latest
RUN apk update && apk upgrade -a -U
RUN apk add mysql mysql-client openrc supervisor
COPY ./mariadb-server.cnf /etc/my.cnf
RUN chmod 755 /etc/my.cnf 
RUN mkdir /sql_data
COPY ./wpdb.sql /sql_data
COPY ./launch.sh /
RUN chmod 755 ./launch.sh
RUN chown -R mysql:mysql /sql_data
VOLUME /var/lib/mysql/
RUN mkdir -p /run/mysqld/
EXPOSE 3306
CMD /launch.sh 

和sql脚本

CREATE DATABASE wordpress;
DELETE FROM mysql.user WHERE user = '';
GRANT ALL PRIVILEGES ON wordpress.* TO 'admin'@'%' IDENTIFIED BY 'admin';
SET PASSWORD FOR 'admin'@'%' = PASSWORD('admin');
FLUSH PRIVILEGES;
  1. 当您同时运行 2 个或更多服务(不包括监督者)时,Superviror 服务很有用。因此,使用带有 myslq 设置的脚本并将其作为守护进程启动就足够了:
#!/bin/sh

mysql_install_db --skip-test-db --user=mysql --datadir=/var/lib/mysql
mysqld --user=mysql --datadir=/var/lib/mysql --init-file=/sql_data/wpdb.sql
  1. yaml 文件有一些小的变化。我们创建的 admin 是 mysql 脚本,所以不需要在 yaml 文件中创建。
apiVersion: v1
kind: Service
metadata:
  name: mysql-service
  labels:
    app: mysql
spec:
  type: ClusterIP
  selector:
    app: mysql
  ports:
    - name: mysql
      port: 3306
      targetPort: 3306
      protocol: TCP
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pv-claim-mysql
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 500Mi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: mysql
    spec:
      restartPolicy: Always
      containers:
        - name: mysql
          image: mysql:latest
          ports:
            - containerPort: 3306
              name: mysql
          volumeMounts:
            - name: mysqldb-storage
              mountPath: "/var/lib/mysql/"
          imagePullPolicy: Never
      volumes:
        - name: mysqldb-storage
          persistentVolumeClaim:
            claimName: pv-claim-mysql
  1. my.cnf 配置文件有一些变化。
#
# These groups are read by MariaDB server.
# Use it for options that only the server (but not clients) should see
# this is read by the standalone daemon and embedded servers
[server]

# this is only for the mysqld standalone daemon
[mysqld]
user=root
port=3306
datadir=/var/lib/mysql
tmpdir=/tmp
skip-networking=false
socket=/run/mysqld/mysqld.sock
wait_timeout = 600
max_allowed_packet = 64M
# Galera-related settings
#[galera]
# Mandatory settings
#wsrep_on=ON
#wsrep_provider=
#wsrep_cluster_address=
#binlog_format=row
#default_storage_engine=InnoDB
#innodb_autoinc_lock_mode=2
#
# Allow server to accept connections on all interfaces.
#
bind-address=0.0.0.0
#
# Optional setting
#wsrep_slave_threads=1
#innodb_flush_log_at_trx_commit=0
# Disabling symbolic links is recommended to prevent assorted security risks
symbolic-links=0

# this is only for embedded server
[embedded]


推荐阅读