gpt4 book ai didi

postgresql - 通过 kubernetes 作业创建或更新现有的 postgres 数据库容器

转载 作者:行者123 更新时间:2023-11-29 12:48:56 28 4
gpt4 key购买 nike

我有一个在 Kubernetes 集群中运行的 Postgres 数据库容器。我需要编写一个 Kubernetes 作业来连接到 Postgres DB 容器并从 SQL 文件运行脚本。我需要在这里了解两件事

  1. 运行SQL脚本的命令
  2. 如何在Job.yaml文件中加载SQL文件

这是我用于 Kubernetes 作业的示例 yaml 文件

apiVersion: batch/v1
kind: Job
metadata:
name: init-db
spec:
template:
metadata:
name: init-db
labels:
app: init-postgresdb
spec:
containers:
- image: "docker.io/bitnami/postgresql:11.5.0-debian-9-r60"
name: init-db
command:
- psql -U postgres
env:
- name: DB_HOST
value: "knotted-iguana-postgresql"
- name: DB_DATABASE
value: "postgres"
restartPolicy: OnFailure

最佳答案

您必须从 configmap 将 SQL 文件挂载为卷,并使用 psql cli 从挂载文件执行命令。

要从文件执行命令,您可以通过以下方式更改 yaml 上的命令参数:

psql -a -f sqlCommand.sql

需要使用您假装挂载的文件创建 configmap 更多信息 here

kubectl create configmap sqlCommands.sql --from-file=sqlCommands.sql

然后您必须在作业 yaml 中添加 configmap 和 mount 语句,并修改命令以使用挂载的文件。

apiVersion: batch/v1
kind: Job
metadata:
name: init-db
spec:
template:
metadata:
name: init-db
labels:
app: init-postgresdb
spec:
containers:
- image: "docker.io/bitnami/postgresql:11.5.0-debian-9-r60"
name: init-db
command: [ "bin/sh", "-c", "psql -a -f /sqlCommand.sql" ]
volumeMounts:
- name: sqlCommand
mountPath: /sqlCommand.sql
env:
- name: DB_HOST
value: "knotted-iguana-postgresql"
- name: DB_DATABASE
value: "postgres"
volumes:
- name: sqlCommand
configMap:
# Provide the name of the ConfigMap containing the files you want
# to add to the container
name: sqlCommand.sql
restartPolicy: OnFailure

关于postgresql - 通过 kubernetes 作业创建或更新现有的 postgres 数据库容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58449442/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com