初始化部署在 AWS EKS 中的 MySQL 数据库

2021-11-27 00:00:00 kubernetes mysql amazon-eks

我的 AWS EKS 集群中有一个运行 MySQL:5.7 的 Pod.我有一个 SQL 文件,可以为其初始化并填充数据.在普通的 docker-compose 中,我将在我的 docker-compose 文件中为它使用一个挂载点:

卷:- ./:/etc/mysql/conf.d- ./:/var/lib/mysql- ./:/docker-entrypoint-initdb.d/init.sql

在 EKS 中,我可以创建一个存储类来保存 MySQL 数据.

如何使用我的 init.sql 文件(大约 8GB)来初始化在 EKS 集群中运行的 MySQL pods?

更新:如果您有一个小的 init 文件或有权访问 Kubernetes 主机,还有另一个问题解释了如何执行此操作:在Kubernetes上创建mysql容器时如何初始化?

不过,就我而言,我使用的是 AWS EKS 并且我的 init 文件有几 GB,因此我们将不胜感激.

解决方案

在 Kubernetes 中也很容易.您可以按照以下方式进行,

  1. 将您的 init.sql 文件放入 Kubernetes 卷.
  2. 将此卷挂载到 mysql pod 的 /docker-entrypoint-initdb.d 目录中.

这是一个示例 yaml 文件:mysql-init-demo.yaml>

您还可以使用 init-containerinit.sql 文件下载到挂载在 /docker-entrypoint-initdb.d 上的 Kubernetes 卷中 mysql容器目录.

UPD: 正如您在更新中指定的那样,您提到的解决方案对您不起作用.我认为 init-container 方法最适合你.在这里,您可以这样做:

  1. 创建一个 pvc.让它的名字是init-script.
  2. 现在,将 init-container 添加到您的 mysql pod.将此init-script pvc 挂载到此init-container 的某个目录中.配置你的 init-container 以便它下载你的 init.sql 文件到这个目录.

  3. /docker-entrypoint-initdb.d

  4. 的mysql容器中安装init-script

如果您需要任何带有 init-container 方法的示例,请告诉我.

UPD-2: 我为 init-container 方法添加了一个示例.您可以在此处找到它.

I have a pod in my AWS EKS Cluster that runs MySQL:5.7. I have an SQL file that initializes and populates the data for it. in a normal docker-compose, I will use a mount point for it in my docker-compose file:

volumes:
  - ./<path-to-my-config-directory>:/etc/mysql/conf.d
  - ./<path-to-my-persistence-directory>:/var/lib/mysql
  - ./<path-to-my-init.sql>:/docker-entrypoint-initdb.d/init.sql

In EKS, I can create a storage class in which to save MySQL data.

How can I use my init.sql file (about 8GB) to initialize the MySQL pods running in the EKS cluster?

Update: there is another question that explains how to do this if you have a small init file or have access to the Kubernetes host: How to initialize mysql container when created on Kubernetes?

In my case though, I am on AWS EKS and my init file is a few gigabytes, so any help would be greatly appreciated.

解决方案

It's easy in Kubernetes too. You can do it as follow,

  1. Put your init.sql file into a Kubernetes volume.
  2. Mount this volume into /docker-entrypoint-initdb.d directory of your mysql pod.

Here, is an sample yaml file: mysql-init-demo.yaml

You can also use an init-container to download init.sql file into a Kubernetes volume that is mounted on /docker-entrypoint-initdb.d directory of mysql container.

UPD: As you have specified in update that the solution you have referred won't work for you. I think init-container approach will be best suited for you. Here, is how you can do:

  1. Create a pvc. Let it's name is init-script.
  2. Now, add a init-container to your mysql pod. Mount this init-script pvc at some directory of this init-container. Configure your init-container such that it download your init.sql file on this directory.

  3. Mount the init-script in mysql container at /docker-entrypoint-initdb.d

Let me know if you need any sample with init-container approach.

UPD-2: I have added a sample for init-container approach. You can find it here.

相关文章