SQL脚本未运行Kubernetes,但仅使用Docker即可运行良好 [英] SQL script isn't running Kubernetes, but runs fine using just Docker

查看:112
本文介绍了SQL脚本未运行Kubernetes,但仅使用Docker即可运行良好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个非常简单的test.sql:

SELECT 'CREATE DATABASE test_dev'
WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'test_dev')\gexec

\c test_dev

CREATE TABLE IF NOT EXISTS test_table (
  username varchar(255)
);

INSERT INTO test_table(username)
VALUES ('test name');

执行以下操作符合我的预期:

Doing the following does what I expected it to do:

Dockerfile.dev

FROM postgres:11-alpine
EXPOSE 5432
COPY ./db/*.sql /docker-entrypoint-initdb.d/

docker build -t testproj/postgres -f db/Dockerfile.dev .

docker run -p 5432:5432 testproj/postgres

这将创建数据库,切换到数据库,创建表并插入值.

This creates the database, switches to it, creates a table, and inserts the values.

现在我正在尝试使用Skaffold在Kubernetes中做同样的事情,但是似乎真的没有发生任何事情:没有错误消息,但是在postgres中没有任何改变

Now I'm trying to do the same in Kubernetes with Skaffold, but nothing really seems to happen: no error messages, but nothing changed in postgres

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: init-script
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 500Mi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgres-storage
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: postgres
  template:
    metadata:
      labels:
        component: postgres
    spec:
      volumes:
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: postgres-storage
        - name: init-script
          persistentVolumeClaim:
            claimName: init-script
      containers:
        - name: postgres
          image: postgres
          ports:
            - containerPort: 5432
          volumeMounts:
            - name: postgres-storage
              mountPath: /var/lib/postgresql/data
              subPath: postgres
            - name: init-script
              mountPath: /docker-entrypoint-initdb.d
---
apiVersion: v1
kind: Service
metadata:
  name: postgres-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    component: postgres
  ports:
    - port: 5432
      targetPort: 5432

我在这里做错了什么?

基本上尝试按照此处的答案进行操作,但没有成功.听起来我需要将.sql移到一个持久卷.

Basically tried to follow the answers here, but isn't panning out. Sounded like I needed to move the .sql to a persistent volume.

https://stackoverflow.com/a/53069399/3123109

推荐答案

coderanger指出了一个明显的错误,它使我朝正确的方向前进:我不是在指修改后的图像.

coderanger pointed out a glaring mistake that got me going in the right direction: I wasn't referring to the modified image.

我相应地更新:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      component: postgres
  template:
    metadata:
      labels:
        component: postgres
    spec:
      containers:
        - name: postgres
          image: testproject/postgres
          ports:
            - containerPort: 5432
          volumeMounts:
            - name: postgres-storage
              mountPath: /var/lib/postgresql/data
              subPath: postgres
      volumes:
        - name: postgres-storage
          persistentVolumeClaim:
            claimName: postgres-storage
---
apiVersion: v1
kind: Service
metadata:
  name: postgres-cluster-ip-service
spec:
  type: ClusterIP
  selector:
    component: postgres
  ports:
    - port: 5432
      targetPort: 5432

然后它开始相应地加载数据.

Then it started loading the data accordingly.

这篇关于SQL脚本未运行Kubernetes,但仅使用Docker即可运行良好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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