愿所有的美好和期待都能如约而至

在 Kubernetes 上创建时如何初始化 mysql 容器?

发布时间:  来源:互联网  作者:匿名  标签:docker error How to initialize mysql container when created on Kubernetes? excep  热度:37.5℃

本文介绍了在 Kubernetes 上创建时如何初始化 mysql 容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在容器的 MySQL 上设置初始数据.在docker-compose.yml中,这样的代码可以在运行容器时创建初始数据.

I want to set initial data on MySQL of container.
In docker-compose.yml, such code can create initial data when running container.

volumes:
  - db:/var/lib/mysql
  - "./docker/mysql/conf.d:/etc/mysql/conf.d"
  - "./docker/mysql/init.d:/docker-entrypoint-initdb.d"

但是,如何在运行时在 Kubernetes 上创建初始数据?

However, how can I create initial data on Kubernetes when running?

推荐答案

根据 MySQL Docker 镜像 README,与容器启动时数据初始化相关的部分是确保您所有的初始化文件都挂载到容器的/docker-entrypoint-initdb.d文件夹中.

According to the MySQL Docker image README, the part that is relevant to data initialization on container start-up is to ensure all your initialization files are mount to the container’s /docker-entrypoint-initdb.d folder.

您可以在 ConfigMap 中定义您的初始数据,并像这样在您的 pod 中挂载相应的卷:

You can define your initial data in a ConfigMap, and mount the corresponding volume in your pod like this:

apiVersion: v1
kind: Pod
metadata:
  name: mysql
spec:
  containers:
  - name: mysql
    image: mysql        
    ports:
      - containerPort: 3306
    volumeMounts:
      - name: mysql-initdb
        mountPath: /docker-entrypoint-initdb.d
  volumes:
    - name: mysql-initdb
      configMap:
        name: mysql-initdb-config
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-initdb-config
data:
  initdb.sql: |
    CREATE TABLE friends (id INT, name VARCHAR(256), age INT, gender VARCHAR(3));
    INSERT INTO friends VALUES (1, 'John Smith', 32, 'm');
    INSERT INTO friends VALUES (2, 'Lilian Worksmith', 29, 'f');
    INSERT INTO friends VALUES (3, 'Michael Rupert', 27, 'm');

这篇关于在 Kubernetes 上创建时如何初始化 mysql 容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,

勇敢去编程!

勇敢的热爱编程,未来的你一定会大放异彩,未来的生活一定会因编程更好!

TOP