How to execute a sql script file in a Kubernetes Pod?(如何在 Kubernetes Pod 中执行 sql 脚本文件?)
问题描述
我想使用 SQL 脚本文件在 Kubernetes pod 中创建一个 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
.有两个:PostStart
和 PreStop
.
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.
您有两种可以实现的钩子处理程序:Exec
或 HTTP
You have two types of hook handlers that can be implemented: Exec
or HTTP
Exec
- 在容器的 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
后,您将使用 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 脚本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Kubernetes Pod 中执行 sql 脚本文件?
基础教程推荐
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- Sql Server 字符串到日期的转换 2021-01-01