From 9044355994a36d7bcd7364bee219f45d8c2f08db Mon Sep 17 00:00:00 2001 From: grego952 Date: Thu, 12 Feb 2026 13:46:29 +0100 Subject: [PATCH 1/4] Add database-postgres and delete database-mssql --- database-mssql/README.md | 76 ------------------- database-mssql/app/entrypoint.sh | 2 - database-mssql/app/init-db.sh | 5 -- database-mssql/app/setup.sql | 18 ----- database-mssql/docker/Dockerfile | 15 ---- database-mssql/k8s/deployment.yaml | 54 ------------- database-postgres/app/entrypoint.sh | 4 + database-postgres/app/init-db.sh | 11 +++ database-postgres/app/setup.sql | 13 ++++ database-postgres/docker/Dockerfile | 18 +++++ database-postgres/k8s/deployment.yaml | 58 ++++++++++++++ .../k8s/pvc.yaml | 4 +- .../k8s/secret.yaml | 8 +- 13 files changed, 110 insertions(+), 176 deletions(-) delete mode 100644 database-mssql/README.md delete mode 100644 database-mssql/app/entrypoint.sh delete mode 100644 database-mssql/app/init-db.sh delete mode 100644 database-mssql/app/setup.sql delete mode 100644 database-mssql/docker/Dockerfile delete mode 100644 database-mssql/k8s/deployment.yaml create mode 100644 database-postgres/app/entrypoint.sh create mode 100644 database-postgres/app/init-db.sh create mode 100644 database-postgres/app/setup.sql create mode 100644 database-postgres/docker/Dockerfile create mode 100644 database-postgres/k8s/deployment.yaml rename {database-mssql => database-postgres}/k8s/pvc.yaml (79%) rename {database-mssql => database-postgres}/k8s/secret.yaml (53%) diff --git a/database-mssql/README.md b/database-mssql/README.md deleted file mode 100644 index ee916ac84..000000000 --- a/database-mssql/README.md +++ /dev/null @@ -1,76 +0,0 @@ -# MS SQL database - -## Overview - -This sample provides the MS SQL database configured with a sample `DemoDB` database which contains one `Orders` table populated with two rows of sample data. The `app/setup.sql` file handles the generation of the database, table, and data. Within the `app/init-db.sh` file, you can also configure the database user and password. They must match the configuration of the Secret defined within the `k8s/deployment.yaml` file. - -This sample demonstrates how to: - -- Create a development Namespace in Kyma runtime. -- Configure and build the MS SQL database Docker image. -- Deploy the MS SQL database in Kyma runtime which includes: - - A Secret containing the database user and password. - - A PersistentVolumeClaim for the storage of the database data. - - A Deployment of the MS SQL image with the Secret and PersistentVolumeClaim configuration. - - A Service to expose the database to other Kubernetes resources. - -## Prerequisites - -- SAP BTP, Kyma runtime instance -- [Docker](https://www.docker.com/) -- [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/) configured to use the `KUBECONFIG` file downloaded from the Kyma runtime. - -## Deploy the database - -1. Create a new `dev` Namespace: - - ```shell - kubectl create namespace dev - kubectl label namespaces dev istio-injection=enabled - ``` - -2. Build and push the image to your Docker repository: - - ```shell - docker build -t {your-docker-account}/mssql -f docker/Dockerfile . - docker push {your-docker-account}/mssql - ``` - -3. Apply the PersistentVolumeClaim: - - ```shell - kubectl -n dev apply -f ./k8s/pvc.yaml - ``` - -4. Apply the Secret: - - ```shell - kubectl -n dev apply -f ./k8s/secret.yaml - ``` - -5. Apply the Deployment: - - ```shell - kubectl -n dev apply -f ./k8s/deployment.yaml - ``` - -6. Verify that the Pod is up and running: - - ```shell - kubectl -n dev get po - ``` - -The expected result shows that the Pod for the `mssql` Deployment is running: - -```shell -NAME READY STATUS RESTARTS AGE -mssql-6df65c689d-nf9dk 2/2 Running 0 93s -``` - -## Run the Docker image locally - -To run the Docker image locally, run this command: - -```shell -docker run -e ACCEPT_EULA=Y -e SA_PASSWORD=Yukon900 -p 1433:1433 -d {docker id}/mssql -``` diff --git a/database-mssql/app/entrypoint.sh b/database-mssql/app/entrypoint.sh deleted file mode 100644 index e2af379f5..000000000 --- a/database-mssql/app/entrypoint.sh +++ /dev/null @@ -1,2 +0,0 @@ -#start SQL Server, start the script to create the DB and import the data, start the app -/usr/src/app/init-db.sh & /opt/mssql/bin/sqlservr diff --git a/database-mssql/app/init-db.sh b/database-mssql/app/init-db.sh deleted file mode 100644 index ed5ac5555..000000000 --- a/database-mssql/app/init-db.sh +++ /dev/null @@ -1,5 +0,0 @@ -#wait for the SQL Server to come up -sleep 60s - -#run the setup script to create the DB and the schema in the DB -/opt/mssql-tools/bin/sqlcmd -S 127.0.0.1 -U sa -P "Yukon900" -d master -i setup.sql \ No newline at end of file diff --git a/database-mssql/app/setup.sql b/database-mssql/app/setup.sql deleted file mode 100644 index 2a092563a..000000000 --- a/database-mssql/app/setup.sql +++ /dev/null @@ -1,18 +0,0 @@ -CREATE DATABASE DemoDB; -GO -USE DemoDB; -GO -CREATE TABLE Orders -( - order_id nvarchar(50) NOT NULL PRIMARY KEY, - description nvarchar(255), - created DATETIME DEFAULT(getdate()), -); -GO -INSERT INTO Orders - (order_id, description) -VALUES("10000001", "Sample Order 1") -INSERT INTO Orders - (order_id, description) -VALUES("10000002", "Sample Order 2") -GO diff --git a/database-mssql/docker/Dockerfile b/database-mssql/docker/Dockerfile deleted file mode 100644 index 2d8ddb5bd..000000000 --- a/database-mssql/docker/Dockerfile +++ /dev/null @@ -1,15 +0,0 @@ -FROM mcr.microsoft.com/mssql/server:2017-CU24-ubuntu-16.04 - -# Create app directory -RUN mkdir -p /usr/src/app -WORKDIR /usr/src/app - -# Bundle app source -COPY app /usr/src/app - -# Grant permissions for the import-data script to be executable -RUN chmod +x /usr/src/app/init-db.sh - -EXPOSE 1433 - -CMD /bin/bash ./entrypoint.sh diff --git a/database-mssql/k8s/deployment.yaml b/database-mssql/k8s/deployment.yaml deleted file mode 100644 index c288329e8..000000000 --- a/database-mssql/k8s/deployment.yaml +++ /dev/null @@ -1,54 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: mssql - labels: - app: mssql -spec: - replicas: 1 - selector: - matchLabels: - app: mssql - template: - metadata: - labels: - app: mssql - spec: - terminationGracePeriodSeconds: 10 - containers: - - name: mssql - image: jcawley5/mssql #change it to your image - imagePullPolicy: Always - ports: - - containerPort: 1433 - env: - - name: MSSQL_PID - value: "Developer" - - name: ACCEPT_EULA - value: "Y" - - name: SA_PASSWORD - valueFrom: - secretKeyRef: - name: mssql - key: password - volumeMounts: - - name: mssqldb - mountPath: /var/opt/mssql - volumes: - - name: mssqldb - persistentVolumeClaim: - claimName: mssql-data ---- -apiVersion: v1 -kind: Service -metadata: - name: mssql - labels: - app: mssql -spec: - selector: - app: mssql - ports: - - protocol: TCP - port: 1433 - targetPort: 1433 diff --git a/database-postgres/app/entrypoint.sh b/database-postgres/app/entrypoint.sh new file mode 100644 index 000000000..fcdfb6826 --- /dev/null +++ b/database-postgres/app/entrypoint.sh @@ -0,0 +1,4 @@ +#!/bin/bash +# PostgreSQL uses docker-entrypoint-initdb.d for initialization +# This script is kept for compatibility but not needed +docker-entrypoint.sh postgres diff --git a/database-postgres/app/init-db.sh b/database-postgres/app/init-db.sh new file mode 100644 index 000000000..fcb2cc2c0 --- /dev/null +++ b/database-postgres/app/init-db.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# Wait for PostgreSQL to be ready +until pg_isready -U postgres; do + echo "Waiting for PostgreSQL to be ready..." + sleep 2 +done + +echo "PostgreSQL is ready!" + +# Run the setup script +psql -U postgres -d DemoDB -f /usr/src/app/setup.sql \ No newline at end of file diff --git a/database-postgres/app/setup.sql b/database-postgres/app/setup.sql new file mode 100644 index 000000000..6fb18c69e --- /dev/null +++ b/database-postgres/app/setup.sql @@ -0,0 +1,13 @@ +-- Database is created automatically by POSTGRES_DB environment variable + +-- Create Orders table +CREATE TABLE IF NOT EXISTS Orders ( + order_id VARCHAR(50) NOT NULL PRIMARY KEY, + description VARCHAR(255), + created TIMESTAMP DEFAULT CURRENT_TIMESTAMP +); + +-- Insert sample data +INSERT INTO Orders (order_id, description) +VALUES ('10000001', 'Sample Order 1'), + ('10000002', 'Sample Order 2'); diff --git a/database-postgres/docker/Dockerfile b/database-postgres/docker/Dockerfile new file mode 100644 index 000000000..acc5ede69 --- /dev/null +++ b/database-postgres/docker/Dockerfile @@ -0,0 +1,18 @@ +FROM postgres:16 + +# Create app directory +RUN mkdir -p /usr/src/app +WORKDIR /usr/src/app + +# Bundle app source +COPY app /usr/src/app + +# Grant permissions for the init-db script to be executable +RUN chmod +x /usr/src/app/init-db.sh + +EXPOSE 5432 + +# Copy initialization script to docker-entrypoint-initdb.d +RUN cp /usr/src/app/setup.sql /docker-entrypoint-initdb.d/ + +CMD ["postgres"] diff --git a/database-postgres/k8s/deployment.yaml b/database-postgres/k8s/deployment.yaml new file mode 100644 index 000000000..98f2dc9cb --- /dev/null +++ b/database-postgres/k8s/deployment.yaml @@ -0,0 +1,58 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: postgres + labels: + app: postgres +spec: + replicas: 1 + strategy: + type: Recreate + selector: + matchLabels: + app: postgres + template: + metadata: + labels: + app: postgres + spec: + terminationGracePeriodSeconds: 10 + containers: + - name: postgres + image: your-registry/postgres #change it to your registry + imagePullPolicy: Always + ports: + - containerPort: 5432 + env: + - name: POSTGRES_DB + value: "DemoDB" + - name: POSTGRES_USER + value: "postgres" + - name: POSTGRES_PASSWORD + valueFrom: + secretKeyRef: + name: postgres + key: password + - name: PGDATA + value: "/var/lib/postgresql/data/pgdata" + volumeMounts: + - name: postgresdb + mountPath: /var/lib/postgresql/data + volumes: + - name: postgresdb + persistentVolumeClaim: + claimName: postgres-data +--- +apiVersion: v1 +kind: Service +metadata: + name: postgres + labels: + app: postgres +spec: + selector: + app: postgres + ports: + - protocol: TCP + port: 5432 + targetPort: 5432 diff --git a/database-mssql/k8s/pvc.yaml b/database-postgres/k8s/pvc.yaml similarity index 79% rename from database-mssql/k8s/pvc.yaml rename to database-postgres/k8s/pvc.yaml index a1a7854d8..64d504783 100644 --- a/database-mssql/k8s/pvc.yaml +++ b/database-postgres/k8s/pvc.yaml @@ -1,9 +1,9 @@ kind: PersistentVolumeClaim apiVersion: v1 metadata: - name: mssql-data + name: postgres-data labels: - app: mssql + app: postgres spec: accessModes: - ReadWriteOnce diff --git a/database-mssql/k8s/secret.yaml b/database-postgres/k8s/secret.yaml similarity index 53% rename from database-mssql/k8s/secret.yaml rename to database-postgres/k8s/secret.yaml index 9b357dd7e..083dd7279 100644 --- a/database-mssql/k8s/secret.yaml +++ b/database-postgres/k8s/secret.yaml @@ -1,11 +1,11 @@ apiVersion: v1 kind: Secret metadata: - name: mssql + name: postgres labels: - app: api-mssql-go + app: postgres type: Opaque data: - #sa:Yukon900 - username: c2E= + #postgres:Yukon900 + username: cG9zdGdyZXM= password: WXVrb245MDA= From 7bca27134b8f566ff2b502d45e9cd019c48bf908 Mon Sep 17 00:00:00 2001 From: grego952 Date: Thu, 12 Feb 2026 13:54:14 +0100 Subject: [PATCH 2/4] Small change --- database-postgres/k8s/deployment.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database-postgres/k8s/deployment.yaml b/database-postgres/k8s/deployment.yaml index 98f2dc9cb..84c88e002 100644 --- a/database-postgres/k8s/deployment.yaml +++ b/database-postgres/k8s/deployment.yaml @@ -19,7 +19,7 @@ spec: terminationGracePeriodSeconds: 10 containers: - name: postgres - image: your-registry/postgres #change it to your registry + image: /postgres #change it to your image imagePullPolicy: Always ports: - containerPort: 5432 From d8b0102c60fd2456b82a5a54e5af3a9ed99c9e59 Mon Sep 17 00:00:00 2001 From: grego952 Date: Thu, 12 Feb 2026 15:15:04 +0100 Subject: [PATCH 3/4] Add README.md --- database-postgres/README.md | 100 ++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 database-postgres/README.md diff --git a/database-postgres/README.md b/database-postgres/README.md new file mode 100644 index 000000000..33999024e --- /dev/null +++ b/database-postgres/README.md @@ -0,0 +1,100 @@ + +# PostgreSQL database + +## Overview + +This sample provides a PostgreSQL database configured with a sample `DemoDB` database containing one `Orders` table populated with two rows of sample data. The `app/setup.sql` file handles the creation of the database, table, and data. In the `app/init-db.sh` file, you can also configure the database user and password. They must match the configuration of the Secret defined in the `k8s/secret.yaml` file. + +This sample demonstrates how to: + +- Create a development namespace in Kyma runtime. +- Configure and build the PostgreSQL database Docker image. +- Deploy the PostgreSQL database in Kyma runtime, which includes: + - A Secret containing the database user and password. + - A PersistentVolumeClaim for the storage of the database data. + - A Deployment of the PostgreSQL image with the Secret and PersistentVolumeClaim configuration. + - A Service to expose the database to other Kubernetes resources. + + +## Prerequisites + +- SAP BTP, Kyma runtime instance +- [Docker](https://www.docker.com/) with a valid public account +- [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/) configured to use the `KUBECONFIG` file downloaded from the Kyma runtime + + +## Deploy the database + +1. Create a new `dev` Namespace and enable Istio: + + ```shell + kubectl create namespace dev + kubectl label namespaces dev istio-injection=enabled + ``` + +2. Build and push the Docker image to your repository (replace `` with your Docker Hub ID): + + ```shell + docker build -t /postgres -f docker/Dockerfile . + docker push /postgres + ``` + +3. Apply the PersistentVolumeClaim: + + ```shell + kubectl -n dev apply -f ./k8s/pvc.yaml + ``` + +4. Apply the Secret: + + ```shell + kubectl -n dev apply -f ./k8s/secret.yaml + ``` + +5. In `k8s/deployment.yaml`, set the correct image (e.g. `/postgres`), then apply the Deployment: + + ```shell + kubectl -n dev apply -f ./k8s/deployment.yaml + ``` + +6. Verify that the Pod is up and running: + + ```shell + kubectl -n dev get po + ``` + +The expected result shows a Pod for the `postgres` Deployment running: + +```shell +NAME READY STATUS RESTARTS AGE +postgres-6df65c689d-xxxxx 2/2 Running 0 93s +``` + + +## Run the Docker image locally + +To run the Docker image locally (replace `` with your Docker Hub ID): + +```shell +docker run -e POSTGRES_DB=DemoDB -e POSTGRES_PASSWORD=Yukon900 -p 5432:5432 --name postgres1 -d /postgres +``` + +To open a bash shell in the container: + +```shell +docker exec -it postgres1 bash +``` + +Then start the psql client: + +```shell +psql -U postgres -d DemoDB +``` + +Example query: + +```shell +SELECT * FROM orders; +``` + +To exit psql, type `\q`, and to exit bash, type `exit`. \ No newline at end of file From 344180f79bdcf8d0ef13d8b526a44d4997c853c9 Mon Sep 17 00:00:00 2001 From: grego952 Date: Fri, 27 Feb 2026 15:11:20 +0100 Subject: [PATCH 4/4] Only three files needed --- database-postgres/README.md | 115 ++++++++++---------------- database-postgres/app/entrypoint.sh | 4 - database-postgres/app/init-db.sh | 11 --- database-postgres/app/setup.sql | 19 ++--- database-postgres/docker/Dockerfile | 18 ---- database-postgres/k8s/deployment.yaml | 58 ------------- database-postgres/k8s/pvc.yaml | 12 --- database-postgres/k8s/secret.yaml | 11 --- database-postgres/k8s/seed-job.yaml | 75 +++++++++++++++++ 9 files changed, 127 insertions(+), 196 deletions(-) delete mode 100644 database-postgres/app/entrypoint.sh delete mode 100644 database-postgres/app/init-db.sh delete mode 100644 database-postgres/docker/Dockerfile delete mode 100644 database-postgres/k8s/deployment.yaml delete mode 100644 database-postgres/k8s/pvc.yaml delete mode 100644 database-postgres/k8s/secret.yaml create mode 100644 database-postgres/k8s/seed-job.yaml diff --git a/database-postgres/README.md b/database-postgres/README.md index 33999024e..dfc6787e5 100644 --- a/database-postgres/README.md +++ b/database-postgres/README.md @@ -1,100 +1,73 @@ - -# PostgreSQL database +# PostgreSQL sample for Kyma ## Overview -This sample provides a PostgreSQL database configured with a sample `DemoDB` database containing one `Orders` table populated with two rows of sample data. The `app/setup.sql` file handles the creation of the database, table, and data. In the `app/init-db.sh` file, you can also configure the database user and password. They must match the configuration of the Secret defined in the `k8s/secret.yaml` file. +This sample seeds a managed PostgreSQL instance on SAP BTP with a small `orders` table. It assumes you already created a PostgreSQL Service Instance and Service Binding for your Kyma cluster. The Service Binding must produce a Kubernetes Secret containing the connection details (`hostname`, `port`, `dbname`, `username`, `password`, and optionally `sslmode`). -This sample demonstrates how to: +The sample demonstrates how to: -- Create a development namespace in Kyma runtime. -- Configure and build the PostgreSQL database Docker image. -- Deploy the PostgreSQL database in Kyma runtime, which includes: - - A Secret containing the database user and password. - - A PersistentVolumeClaim for the storage of the database data. - - A Deployment of the PostgreSQL image with the Secret and PersistentVolumeClaim configuration. - - A Service to expose the database to other Kubernetes resources. +- Prepare a Kyma namespace for consuming a BTP-managed PostgreSQL instance. +- Seed the database using a Kubernetes Job that runs `psql` against the bound instance. +The SQL used to create and seed the table is stored in `app/setup.sql`. The Job definition lives in `k8s/seed-job.yaml`. ## Prerequisites - SAP BTP, Kyma runtime instance -- [Docker](https://www.docker.com/) with a valid public account -- [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/) configured to use the `KUBECONFIG` file downloaded from the Kyma runtime - +- Existing PostgreSQL Service Instance and Service Binding that exposes a Secret in your Kyma cluster +- [`kubectl`](https://kubernetes.io/docs/tasks/tools/install-kubectl/) configured to use the `KUBECONFIG` file downloaded from the Kyma runtime -## Deploy the database +## Deploy the sample -1. Create a new `dev` Namespace and enable Istio: +1. Create and label a `dev` namespace if it does not exist: ```shell kubectl create namespace dev - kubectl label namespaces dev istio-injection=enabled - ``` - -2. Build and push the Docker image to your repository (replace `` with your Docker Hub ID): - - ```shell - docker build -t /postgres -f docker/Dockerfile . - docker push /postgres - ``` - -3. Apply the PersistentVolumeClaim: - - ```shell - kubectl -n dev apply -f ./k8s/pvc.yaml + kubectl label namespace dev istio-injection=enabled ``` -4. Apply the Secret: - - ```shell - kubectl -n dev apply -f ./k8s/secret.yaml - ``` +2. Make sure the PostgreSQL Service Binding Secret is available in the `dev` namespace. If it was created elsewhere, copy it into `dev` or recreate the binding in `dev`. Adjust the Secret name and key names in `k8s/seed-job.yaml` if they differ from your binding. -5. In `k8s/deployment.yaml`, set the correct image (e.g. `/postgres`), then apply the Deployment: +3. Apply the ConfigMap and Job that seeds the database: ```shell - kubectl -n dev apply -f ./k8s/deployment.yaml + kubectl -n dev apply -f ./k8s/seed-job.yaml ``` -6. Verify that the Pod is up and running: +4. Wait for the Job to finish: ```shell - kubectl -n dev get po + kubectl -n dev get jobs seed-postgresql ``` -The expected result shows a Pod for the `postgres` Deployment running: - -```shell -NAME READY STATUS RESTARTS AGE -postgres-6df65c689d-xxxxx 2/2 Running 0 93s -``` - - -## Run the Docker image locally - -To run the Docker image locally (replace `` with your Docker Hub ID): - -```shell -docker run -e POSTGRES_DB=DemoDB -e POSTGRES_PASSWORD=Yukon900 -p 5432:5432 --name postgres1 -d /postgres -``` - -To open a bash shell in the container: - -```shell -docker exec -it postgres1 bash -``` - -Then start the psql client: - -```shell -psql -U postgres -d DemoDB -``` - -Example query: +5. (Optional) Verify the data using a temporary `psql` client Pod. Replace `postgresql-credentials` with your binding Secret name if it differs: + + ```shell + kubectl -n dev apply -f - <<'EOF' + apiVersion: v1 + kind: Pod + metadata: + name: pg-client + spec: + restartPolicy: Never + containers: + - name: psql + image: postgres:15 + envFrom: + - secretRef: + name: postgresql-credentials + command: ["psql"] + args: ["-v", "ON_ERROR_STOP=1", "-c", "SELECT order_id, description, created FROM orders;"] + EOF + kubectl -n dev logs pod/pg-client + kubectl -n dev delete pod/pg-client + ``` + +## Cleanup + +Delete the seeding assets if you no longer need them: ```shell -SELECT * FROM orders; +kubectl -n dev delete job seed-postgresql +kubectl -n dev delete configmap postgresql-sample-sql ``` - -To exit psql, type `\q`, and to exit bash, type `exit`. \ No newline at end of file diff --git a/database-postgres/app/entrypoint.sh b/database-postgres/app/entrypoint.sh deleted file mode 100644 index fcdfb6826..000000000 --- a/database-postgres/app/entrypoint.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/bash -# PostgreSQL uses docker-entrypoint-initdb.d for initialization -# This script is kept for compatibility but not needed -docker-entrypoint.sh postgres diff --git a/database-postgres/app/init-db.sh b/database-postgres/app/init-db.sh deleted file mode 100644 index fcb2cc2c0..000000000 --- a/database-postgres/app/init-db.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash -# Wait for PostgreSQL to be ready -until pg_isready -U postgres; do - echo "Waiting for PostgreSQL to be ready..." - sleep 2 -done - -echo "PostgreSQL is ready!" - -# Run the setup script -psql -U postgres -d DemoDB -f /usr/src/app/setup.sql \ No newline at end of file diff --git a/database-postgres/app/setup.sql b/database-postgres/app/setup.sql index 6fb18c69e..0b94ef1cd 100644 --- a/database-postgres/app/setup.sql +++ b/database-postgres/app/setup.sql @@ -1,13 +1,10 @@ --- Database is created automatically by POSTGRES_DB environment variable - --- Create Orders table -CREATE TABLE IF NOT EXISTS Orders ( - order_id VARCHAR(50) NOT NULL PRIMARY KEY, - description VARCHAR(255), - created TIMESTAMP DEFAULT CURRENT_TIMESTAMP +CREATE TABLE IF NOT EXISTS orders ( + order_id varchar(50) PRIMARY KEY, + description varchar(255), + created timestamptz DEFAULT NOW() ); --- Insert sample data -INSERT INTO Orders (order_id, description) -VALUES ('10000001', 'Sample Order 1'), - ('10000002', 'Sample Order 2'); +INSERT INTO orders (order_id, description) VALUES + ('10000001', 'Sample Order 1'), + ('10000002', 'Sample Order 2') +ON CONFLICT (order_id) DO NOTHING; diff --git a/database-postgres/docker/Dockerfile b/database-postgres/docker/Dockerfile deleted file mode 100644 index acc5ede69..000000000 --- a/database-postgres/docker/Dockerfile +++ /dev/null @@ -1,18 +0,0 @@ -FROM postgres:16 - -# Create app directory -RUN mkdir -p /usr/src/app -WORKDIR /usr/src/app - -# Bundle app source -COPY app /usr/src/app - -# Grant permissions for the init-db script to be executable -RUN chmod +x /usr/src/app/init-db.sh - -EXPOSE 5432 - -# Copy initialization script to docker-entrypoint-initdb.d -RUN cp /usr/src/app/setup.sql /docker-entrypoint-initdb.d/ - -CMD ["postgres"] diff --git a/database-postgres/k8s/deployment.yaml b/database-postgres/k8s/deployment.yaml deleted file mode 100644 index 84c88e002..000000000 --- a/database-postgres/k8s/deployment.yaml +++ /dev/null @@ -1,58 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: postgres - labels: - app: postgres -spec: - replicas: 1 - strategy: - type: Recreate - selector: - matchLabels: - app: postgres - template: - metadata: - labels: - app: postgres - spec: - terminationGracePeriodSeconds: 10 - containers: - - name: postgres - image: /postgres #change it to your image - imagePullPolicy: Always - ports: - - containerPort: 5432 - env: - - name: POSTGRES_DB - value: "DemoDB" - - name: POSTGRES_USER - value: "postgres" - - name: POSTGRES_PASSWORD - valueFrom: - secretKeyRef: - name: postgres - key: password - - name: PGDATA - value: "/var/lib/postgresql/data/pgdata" - volumeMounts: - - name: postgresdb - mountPath: /var/lib/postgresql/data - volumes: - - name: postgresdb - persistentVolumeClaim: - claimName: postgres-data ---- -apiVersion: v1 -kind: Service -metadata: - name: postgres - labels: - app: postgres -spec: - selector: - app: postgres - ports: - - protocol: TCP - port: 5432 - targetPort: 5432 diff --git a/database-postgres/k8s/pvc.yaml b/database-postgres/k8s/pvc.yaml deleted file mode 100644 index 64d504783..000000000 --- a/database-postgres/k8s/pvc.yaml +++ /dev/null @@ -1,12 +0,0 @@ -kind: PersistentVolumeClaim -apiVersion: v1 -metadata: - name: postgres-data - labels: - app: postgres -spec: - accessModes: - - ReadWriteOnce - resources: - requests: - storage: 100Mi diff --git a/database-postgres/k8s/secret.yaml b/database-postgres/k8s/secret.yaml deleted file mode 100644 index 083dd7279..000000000 --- a/database-postgres/k8s/secret.yaml +++ /dev/null @@ -1,11 +0,0 @@ -apiVersion: v1 -kind: Secret -metadata: - name: postgres - labels: - app: postgres -type: Opaque -data: - #postgres:Yukon900 - username: cG9zdGdyZXM= - password: WXVrb245MDA= diff --git a/database-postgres/k8s/seed-job.yaml b/database-postgres/k8s/seed-job.yaml new file mode 100644 index 000000000..f6bce72d9 --- /dev/null +++ b/database-postgres/k8s/seed-job.yaml @@ -0,0 +1,75 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: postgresql-sample-sql + labels: + app: postgresql-sample +data: + setup.sql: |- + CREATE TABLE IF NOT EXISTS orders ( + order_id varchar(50) PRIMARY KEY, + description varchar(255), + created timestamptz DEFAULT NOW() + ); + + INSERT INTO orders (order_id, description) VALUES + ('10000001', 'Sample Order 1'), + ('10000002', 'Sample Order 2') + ON CONFLICT (order_id) DO NOTHING; +--- +apiVersion: batch/v1 +kind: Job +metadata: + name: seed-postgresql + labels: + app: postgresql-sample +spec: + backoffLimit: 1 + template: + spec: + restartPolicy: Never + containers: + - name: psql-client + image: postgres:15 + env: + - name: PGHOST + valueFrom: + secretKeyRef: + name: postgres-binding # change to your binding Secret name + key: hostname + - name: PGPORT + valueFrom: + secretKeyRef: + name: postgres-binding # change to your binding Secret name + key: port + - name: PGDATABASE + valueFrom: + secretKeyRef: + name: postgres-binding # change to your binding Secret name + key: dbname + - name: PGUSER + valueFrom: + secretKeyRef: + name: postgres-binding # change to your binding Secret name + key: username + - name: PGPASSWORD + valueFrom: + secretKeyRef: + name: postgres-binding # change to your binding Secret name + key: password + - name: PGSSLMODE + valueFrom: + secretKeyRef: + name: postgres-binding # change to your binding Secret name + key: sslmode + optional: true + volumeMounts: + - name: sql + mountPath: /init + command: ["psql"] + args: + ["-v", "ON_ERROR_STOP=1", "-f", "/init/setup.sql"] + volumes: + - name: sql + configMap: + name: postgresql-sample-sql