Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 通过kubernetes作业创建或更新现有postgres db容器_Postgresql_Kubernetes_Kubernetes Jobs - Fatal编程技术网

Postgresql 通过kubernetes作业创建或更新现有postgres db容器

Postgresql 通过kubernetes作业创建或更新现有postgres db容器,postgresql,kubernetes,kubernetes-jobs,Postgresql,Kubernetes,Kubernetes Jobs,我有一个Postgres DB容器,它运行在Kubernetes集群中。我需要编写一个Kubernetes作业来连接到Postgres DB容器并从SQL文件运行脚本。我需要了解两件事 运行SQL脚本的命令 如何在Job.yaml文件中加载SQL文件 这是我的Kubernetes作业的yaml示例文件 apiVersion: batch/v1 kind: Job metadata: name: init-db spec: template: metadata: nam

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

  • 运行SQL脚本的命令
  • 如何在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  
    

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

    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
    
    要从文件执行命令,可以通过以下方式更改yaml上的命令参数:

    psql -a -f sqlCommand.sql
    
    需要使用您假装装载更多信息的文件创建configmap

    然后,您必须在作业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
    

    您应该首先为相同的文件创建一个docker文件,执行它并将相同的docker工作映像映射到kubernetes作业yaml文件

    您可以在docker文件中添加entrypoint.sh,您可以在其中放置要执行的脚本