如何在Kubernetes Pod中执行SQL脚本文件? [英] How to execute a sql script file in a Kubernetes Pod?

查看:620
本文介绍了如何在Kubernetes Pod中执行SQL脚本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用SQL脚本文件在Kubernetes容器中创建一个SQL Server数据库.我有创建数据库并插入主数据的SQL脚本.由于我是Kubernetes的新手,所以我很难在pod中运行SQL脚本.我知道可以在单独的kubectl exec命令中手动执行SQL脚本,但是我希望可以在pod deploy yml文件本身中自动执行该脚本.

I wanted to create a SQL Server database in Kubernetes pod using a SQL script file. I have the SQL script which creates the database and inserts the master data. As I'm new to Kubernetes, I'm struggling to run the SQL script in a pod. I know the SQL script can be executed manually in a separate kubectl exec command, but I wanted it to be executed automatically in the pod deploy yml file itself.

是否可以将脚本文件安装到pod的卷中并在启动容器后运行它?

Is there a way to mount the script file into pod's volume and run it after starting the container?

推荐答案

在这种情况下,您可以使用kubernetes hooks.其中有两个:PostStartPreStop.

You could use kubernetes hooks for that case. There are two of them: PostStart and PreStop.

PostStart在创建容器后立即执行. 另一方面,PreStop在容器终止之前立即被调用.

PostStart executes immediately after a container is created. PreStop on other hand is called immediately before a container is terminated.

您可以执行两种类型的挂钩处理程序:ExecHTTP

You have two types of hook handlers that can be implemented: Exec or HTTP

Exec-在Container的cgroup和名称空间内执行特定命令,例如pre-stop.sh.该命令消耗的资源计入容器. HTTP-针对容器上的特定端点执行HTTP请求.

Exec - Executes a specific command, such as pre-stop.sh, inside the cgroups and namespaces of the Container. Resources consumed by the command are counted against the Container. HTTP - Executes an HTTP request against a specific endpoint on the Container.

PostStart是这里要使用的一个,但是请注意,该挂钩与主进程并行运行. 它不等待主要进程完全启动.在挂钩完成之前,容器将保持等待状态.

PostStart is the one to go with here, however please note that the hook is running in parallel with the main process. It does not wait for the main process to start up fully. Until the hook completes, the container will stay in waiting state.

您可以为此使用一些解决方法,并在脚本中添加sleep命令,以使其稍等片刻以创建主容器. 您的脚本文件可以存储在容器映像中,也可以使用ConfigMap挂载到与pod共享的卷上.这里是一些示例,说明了如何做到这一点:

You could use a little workaround for that and add a sleep command to your script in order to have it wait a bit for your main container creation. Your script file can be stored in the container image or mounted to volume shared with the pod using ConfigMap. Here`s some examples how to do that:

kind: ConfigMap
apiVersion: v1
metadata:
  namespace: <your-namespace> 
  name: poststarthook
data:
  poststart.sh: |
     #!/bin/bash
     echo "It`s done"

确保您的脚本没有超出ConfigMap的限制ConfigMap

Make sure your script does not exceed 1mb limit for ConfigMap

定义configMap后,将使用volumes进行安装:

After you define configMap you will have mount it using volumes:

spec:
      containers:
      - image: <your-image>
        name: example-container
        volumeMounts:
          - mountPath: /opt/poststart.sh
            subPath: poststart.sh
            name: hookvolume
      volumes:
      - name: hookvolume
        configMap:
          name: poststarthook
          defaultMode: 0755 #please remember to add proper (executable) permissions

然后您可以在规范中定义postStart:

And then you can define postStart in your spec:

spec:
  containers:
  - name: example-container
    image: <your-image> 
    lifecycle:
      postStart:
        exec:
          command: ["/bin/sh", "-c", /opt/poststart.sh ]

您可以阅读有关kubernetes中的钩子的更多信息文档和在此文章.让我知道是否有帮助.

You can read more about hooks in kubernetes documentation and in this article. Let me know if that was helpful.

这篇关于如何在Kubernetes Pod中执行SQL脚本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆