简体   繁体   English

想通过dep.yaml文件转储data.sql文件,不想通过bin/bash手动

[英]Want to dump data.sql file through dep.yaml file, don't want to go manual through bin/bash

1. I Want to dump data file using YAML file 1. 我想使用 YAML 文件转储数据文件

data.sql file in the volume卷中的 data.sql 文件

the YAML file is YAML 文件是

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306
  selector:
    app: mysql
  clusterIP: None
---
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:
        volumes:
        - name: mydir
          hostPath: 
            path: /var/local/test
            type: DirectoryOrCreate
        - name: myfile
          hostPath:
            path: /var/local/test/data.sql
            type: FileOrCreate 

        containers:
        - image: mysql:5.6
          name: mysql
          env:
            # Use secret in real usage
        
          - name: MYSQL_ROOT_PASSWORD
            value: root
          - name: MYSQL_USERNAME
            value: root
          - name: MYSQL_PASSWORD
            value: root
          - name: MYSQL_DATABASE
            value: sample  
          ports:
          - containerPort: 3306
            name: mysql
          volumeMounts:
          - name: mydir
            mountPath: /var/local/test/data.sql
          command: ["/bin/sh","-c"]
          args: ["mysql -u root -proot sample < data.sql"] 

using a command, args concept but not possible使用命令,参数概念但不可能

If I access the pod it can be dump easily but I want dump action through YAML如果我访问 pod,它可以轻松转储,但我希望通过 YAML 进行转储操作

**2. **2. Restore the database file in local system **恢复本地系统中的数据库文件**

Also want to restore the dumped aur new data in my system or any other location but it will be accessible还想在我的系统或任何其他位置恢复转储的 aur 新数据,但它可以访问

Don't share the bash option kubectl exec -it mysql-598cbfc789-2jlt7 -- mysql -u root -proot sample < data.sql不要共享 bash 选项 kubectl exec -it mysql-598cbfc789-2jlt7 -- mysql -u root -proot sample < data.sql

Mostly you can't do this in the way you describe.大多数情况下,您无法按照您描述的方式执行此操作。 Kubernetes YAML isn't great for describing imperative actions like "restore a database dump". Kubernetes YAML 不太适合描述诸如“恢复数据库转储”之类的命令式操作。 If you need to do it there, you need to do it in a Job separate from the database proper.如果您需要在那里执行此操作,则需要在与数据库本身分开的作业中执行此操作。

The most portable way to do this is to not attempt it in Kubernetes YAML at all (so remove, for example, the override command: and args: in your Deployment).最便携的方法是根本不要在 Kubernetes YAML 中尝试它(例如,删除您部署中的 override command:args: )。 Run kubectl apply -f ... as normal, and once the database comes up, run in two separate terminal windows像往常一样运行kubectl apply -f ... ,一旦数据库出现,在两个单独的终端窗口中运行

kubectl port-forward service/mysql 12345:3306
mysql -u root -proot -H127.0.0.1 -P12345 sample < data.sql

The first establishes a connection to the running database;首先建立到正在运行的数据库的连接; the second uses the local mysql tool and your local filesystem, connecting to the forwarded port.第二个使用本地mysql工具和本地文件系统,连接到转发端口。 12345 can be any port number but needs to be the same in both commands. 12345 可以是任何端口号,但在两个命令中必须相同。

Some other alternatives (focusing on kubectl exec ) are described in How do I restore a dump file from mysqldump using kubernetes?其他一些替代品(重点kubectl exec )中描述了如何恢复从mysqldump的使用kubernetes转储文件? . .

There are a couple of conceptual issues with the layout you show.您展示的布局存在一些概念性问题。 The first is that a container runs only one command, and your sh -c 'mysql ...' runs instead of the MySQL daemon.第一个是容器只运行一个命令,并且你的sh -c 'mysql ...'运行而不是MySQL 守护进程。 A Kubernetes Pod also can't access your local filesystem, and if you use a hostPath volume like you show, you need to correctly guess which node the pod will run on and copy the data there; Kubernetes Pod 也无法访问您的本地文件系统,如果您使用如您所示的hostPath卷,您需要正确猜测 Pod 将在哪个节点上运行并将数据复制到那里; this isn't reliable and can lead to inconsistent results if you run it from different places.这是不可靠的,如果您从不同的地方运行它,可能会导致不一致的结果。


If you can get the dump data into Kubernetes somehow, you can run a separate Job to do the restore:如果您可以以某种方式将转储数据放入 Kubernetes,您可以运行一个单独的 作业来进行恢复:

apiVersion: batch/v1
kind: Job
metadata:
  name: mysql-restore
spec:
  template:
    spec:
      containers:
        - name: mysql-restore
          env: { same as in: the question }
          image: mysql:5.6
          volumeMounts: { ... }
          command: [sh, -c]
          args:
            - >-
                mysql sample < /var/local/test/data.sql

The question is what to put in the volumeMounts: .问题是在volumeMounts:放什么。 If the database dump is fairly small, you might be able to fit it into a ConfigMap and then mount the ConfigMap into the Pod , but creating the ConfigMap can be awkward and the size is limited to 1 MB.如果数据库转储相当小,您或许可以将其放入ConfigMap 中,然后将 ConfigMap 挂载到 Pod 中,但创建 ConfigMap 可能会很尴尬,并且大小限制为 1 MB。 If you have a way to directly access the volume content (maybe you're mounting an NFS mount you can also access out-of-cluster) then you can copy the dump there.如果您有办法直接访问卷内容(也许您正在挂载 NFS 挂载,您也可以访问集群外),那么您可以将转储复制到那里。 You also might be able to use network-accessible cloud storage like Amazon S3 or another HTTP-based service where you can curl the dump at startup time.您还可以使用网络可访问的云存储,例如 Amazon S3 或其他基于 HTTP 的服务,您可以在其中在启动时curl转储。

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

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