简体   繁体   English

将mysql数据导入kubernetes pod

[英]import mysql data to kubernetes pod

Does anyone know how to import the data inside my dump.sql file to a kubernetes pod either;有谁知道如何将我的 dump.sql 文件中的数据导入到 kubernetes pod?

Directly,same way as you dealing with docker containers:直接,与您处理 docker 容器的方式相同:

docker exec -i container_name mysql -uroot --password=secret database < Dump.sql

Or using the data stored in an existing docker container volume and pass it to the pod .或者使用存储在现有 docker 容器卷中的数据并将其传递给 pod 。

就像其他人正在寻找这个一样:

kubectl -n namespace exec -i my_sql_pod_name -- mysql -u user -ppassword < my_local_dump.sql

To answer your specific question:要回答您的具体问题:

You can kubectl exec into your container in order to run commands inside it.您可以kubectl exec进入您的容器,以便在其中运行命令。 You may need to first ensure that the container has access to the file, by perhaps storing it in a location that the cluster can access (network?) and then using wget/curl within the container to make it available.您可能需要首先确保容器可以访问该文件,方法可能是将其存储在集群可以访问的位置(网络?),然后在容器内使用 wget/curl 使其可用。 One may even open up an interactive session with kubectl exec .甚至可以使用kubectl exec打开一个交互式会话。

However, the ways to do this in increasing measure of generality would be:但是,在增加通用性方面这样做的方法是:

  1. Create a service that lets you access the mysql instance running on the pod from outside the cluster and connect your local mysql client to it.创建一个服务,让您可以从集群外部访问在 pod 上运行的 mysql 实例,并将您的本地 mysql 客户端连接到它。

  2. If you are executing this initialization operation every time such a mysql pod is being started, it could be stored on a persistent volume and you could execute the script within your pod when you start up.如果每次启动此类 mysql pod 时都执行此初始化操作,则它可以存储在持久卷上,并且可以在启动时在 pod 中执行脚本。

  3. If you have several pieces of data that you typically need to copy over when starting the pod, look at init containers for fetching that data.如果您在启动 Pod 时通常需要复制多条数据,请查看用于获取该数据的init 容器

TL;DR Using ConfigMaps and then use that ConfgMap as a mount into the /docker-entrypoint-initdb.d folder TL;DR 使用 ConfigMaps,然后使用该 ConfgMap 作为挂载到 /docker-entrypoint-initdb.d 文件夹

Code代码

MySQL Deployment MySQL 部署

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec: 
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  strategy:
    type: Recreate 
  template: 
    metadata: 
      labels: 
        app: mysql
    spec: 
      containers:
        - name: mysql
          image: mysql:5.6
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: dbpassword11
          ports:
            - containerPort: 3306
              name: mysql    
          volumeMounts:
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql    
            - name: usermanagement-dbcreation-script
              mountPath: /docker-entrypoint-initdb.d #https://hub.docker.com/_/mysql Refer Initializing a fresh instance                                            
      volumes: 
        - name: mysql-persistent-storage
          persistentVolumeClaim:
            claimName: ebs-mysql-pv-claim
        - name: usermanagement-dbcreation-script
          configMap:
            name: usermanagement-dbcreation-script

MySQL ConfigMap MySQL 配置映射

apiVersion: v1
kind: ConfigMap
metadata:
  name: usermanagement-dbcreation-script
data: 
  mysql_usermgmt.sql: |-
    DROP DATABASE IF EXISTS usermgmt;
    CREATE DATABASE usermgmt; 

Reference:参考:

https://github.com/stacksimplify/aws-eks-kubernetes-masterclass/blob/master/04-EKS-Storage-with-EBS-ElasticBlockStore/04-02-SC-PVC-ConfigMap-MySQL/kube-manifests/04-mysql-deployment.yml https://github.com/stacksimplify/aws-eks-kubernetes-masterclass/blob/master/04-EKS-Storage-with-EBS-ElasticBlockStore/04-02-SC-PVC-ConfigMap-MySQL/kube-manifests/ 04-mysql-deployment.yml

https://github.com/stacksimplify/aws-eks-kubernetes-masterclass/blob/master/04-EKS-Storage-with-EBS-ElasticBlockStore/04-02-SC-PVC-ConfigMap-MySQL/kube-manifests/03-UserManagement-ConfigMap.yml https://github.com/stacksimplify/aws-eks-kubernetes-masterclass/blob/master/04-EKS-Storage-with-EBS-ElasticBlockStore/04-02-SC-PVC-ConfigMap-MySQL/kube-manifests/ 03-UserManagement-ConfigMap.yml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM