From 551c3c15dd28b91120ad179492fd9894e82a56e8 Mon Sep 17 00:00:00 2001 From: scott Date: Thu, 14 Mar 2024 17:42:27 -0400 Subject: [PATCH 1/2] job, deployment and service yaml files complete --- .../bookstore-sample-app/db/README.md | 25 +++++++++++++++++- .../bookstore-sample-app/db/deployment.yaml | 26 +++++++++++++++++++ .../eventing/bookstore-sample-app/db/job.yaml | 24 +++++++++++++++++ .../bookstore-sample-app/db/sample.sql | 4 +-- .../bookstore-sample-app/db/service.yaml | 10 +++++++ 5 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 code-samples/eventing/bookstore-sample-app/db/deployment.yaml create mode 100644 code-samples/eventing/bookstore-sample-app/db/job.yaml create mode 100644 code-samples/eventing/bookstore-sample-app/db/service.yaml diff --git a/code-samples/eventing/bookstore-sample-app/db/README.md b/code-samples/eventing/bookstore-sample-app/db/README.md index 9fb745e878..8297a37afd 100644 --- a/code-samples/eventing/bookstore-sample-app/db/README.md +++ b/code-samples/eventing/bookstore-sample-app/db/README.md @@ -2,6 +2,7 @@ 1. Database Schema 2. Sample Data +3. Deployment on K8s Cluster ## 1. Database Schema @@ -23,4 +24,26 @@ The sample rows inserted for the BookReviews table are shown below: | 1 | 2020-01-01 00:00:00 | This book is great! | positive | | 2 | 2020-01-02 00:02:00 | This book is terrible! | negative | | 3 | 2020-01-03 00:01:30 | This book is okay. | neutral | -| 4 | 2020-01-04 00:00:00 | Meh | neutral | \ No newline at end of file +| 4 | 2020-01-04 00:00:00 | Meh | neutral | + +## 3. Deployment on K8s Cluster +1. Set up a Kubernetes Cluster +i. [minikube start](https://minikube.sigs.k8s.io/docs/start/) +ii. [kind quickstart](https://kind.sigs.k8s.io/docs/user/quick-start/) + +2. Set up the configmap to link the sample.sql file +`kubectl create configmap sql-configmap --from-file=sample.sql` + +3. Create the deployment pod for the Postgres server +`kubectl apply -f deployment.yaml` + +4. Expose the port for the Postgres server +`kubectl apply -f service.yaml` + +5. Run the job for executing the queries from sample.sql +`kubectl apply -f job.yaml` + +6. To interact with the Postgres server +i. `kubectl exec -it (deployment pod name) -- /bin/bash` +ii. `psql -h postgresql -U myuser -d mydatabase` +iii. Use `mypassword` when prompted \ No newline at end of file diff --git a/code-samples/eventing/bookstore-sample-app/db/deployment.yaml b/code-samples/eventing/bookstore-sample-app/db/deployment.yaml new file mode 100644 index 0000000000..6d4384b166 --- /dev/null +++ b/code-samples/eventing/bookstore-sample-app/db/deployment.yaml @@ -0,0 +1,26 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: postgresql +spec: + replicas: 1 + selector: + matchLabels: + app: postgresql + template: + metadata: + labels: + app: postgresql + spec: + containers: + - name: postgresql + image: quay.io/enterprisedb/postgresql + env: + - name: POSTGRES_DB + value: mydatabase + - name: POSTGRES_USER + value: myuser + - name: POSTGRES_PASSWORD + value: mypassword + ports: + - containerPort: 5432 \ No newline at end of file diff --git a/code-samples/eventing/bookstore-sample-app/db/job.yaml b/code-samples/eventing/bookstore-sample-app/db/job.yaml new file mode 100644 index 0000000000..5b6dc5ea15 --- /dev/null +++ b/code-samples/eventing/bookstore-sample-app/db/job.yaml @@ -0,0 +1,24 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: make-sample-table +spec: + template: + spec: + containers: + - name: postgres-client + image: postgres:latest + command: ["psql", "-h", "postgresql", "-U", "myuser", "-d", "mydatabase", "-f", "/tmp/sample.sql", "-p", "5432", "-W"] + env: + - name: PGPASSWORD + value: mypassword + volumeMounts: + - name: sql-files + mountPath: /tmp + readOnly: true + restartPolicy: Never + volumes: + - name: sql-files + configMap: + name: sample-sql-configmap + backoffLimit: 4 diff --git a/code-samples/eventing/bookstore-sample-app/db/sample.sql b/code-samples/eventing/bookstore-sample-app/db/sample.sql index c690b0c7bc..d08f462bf4 100644 --- a/code-samples/eventing/bookstore-sample-app/db/sample.sql +++ b/code-samples/eventing/bookstore-sample-app/db/sample.sql @@ -1,9 +1,9 @@ -CREATE TABLE IF NOT EXISTS book_reviews( +CREATE TABLE IF NOT EXISTS book_reviews ( id SERIAL PRIMARY KEY, post_time timestamp NOT NULL, content TEXT NOT NULL, sentiment TEXT, - CONSTRAINT sentiment_check CHECK (sentiment IN ('positive', 'negative', 'neutral')), + CONSTRAINT sentiment_check CHECK (sentiment IN ('positive', 'negative', 'neutral')) ); INSERT INTO book_reviews (post_time, content, sentiment) VALUES diff --git a/code-samples/eventing/bookstore-sample-app/db/service.yaml b/code-samples/eventing/bookstore-sample-app/db/service.yaml new file mode 100644 index 0000000000..c1d144a90b --- /dev/null +++ b/code-samples/eventing/bookstore-sample-app/db/service.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Service +metadata: + name: postgresql +spec: + ports: + - port: 5432 + selector: + app: postgresql + type: NodePort \ No newline at end of file From d7ff1918b37ea193cf5a25895d49a86ed607a4a0 Mon Sep 17 00:00:00 2001 From: scott Date: Fri, 15 Mar 2024 13:37:58 -0400 Subject: [PATCH 2/2] added persistent volume claim --- .../eventing/bookstore-sample-app/db/PVC.yaml | 10 ++++++++++ .../eventing/bookstore-sample-app/db/README.md | 18 +++++++++++------- .../bookstore-sample-app/db/deployment.yaml | 11 +++++++++-- 3 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 code-samples/eventing/bookstore-sample-app/db/PVC.yaml diff --git a/code-samples/eventing/bookstore-sample-app/db/PVC.yaml b/code-samples/eventing/bookstore-sample-app/db/PVC.yaml new file mode 100644 index 0000000000..5e4091d5ee --- /dev/null +++ b/code-samples/eventing/bookstore-sample-app/db/PVC.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: postgres-pvc +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 1Gi \ No newline at end of file diff --git a/code-samples/eventing/bookstore-sample-app/db/README.md b/code-samples/eventing/bookstore-sample-app/db/README.md index 8297a37afd..1fc6ff9330 100644 --- a/code-samples/eventing/bookstore-sample-app/db/README.md +++ b/code-samples/eventing/bookstore-sample-app/db/README.md @@ -34,16 +34,20 @@ ii. [kind quickstart](https://kind.sigs.k8s.io/docs/user/quick-start/) 2. Set up the configmap to link the sample.sql file `kubectl create configmap sql-configmap --from-file=sample.sql` -3. Create the deployment pod for the Postgres server +3. Stand up the Persistent Volume Claim for Postgres server +`kubectl apply -f PVC.yaml` + +4. Create the deployment pod for the Postgres server `kubectl apply -f deployment.yaml` -4. Expose the port for the Postgres server +5. Expose the port for the Postgres server `kubectl apply -f service.yaml` -5. Run the job for executing the queries from sample.sql +6. Run the job for executing the queries from sample.sql `kubectl apply -f job.yaml` -6. To interact with the Postgres server -i. `kubectl exec -it (deployment pod name) -- /bin/bash` -ii. `psql -h postgresql -U myuser -d mydatabase` -iii. Use `mypassword` when prompted \ No newline at end of file +7. To interact with the database +i. Get the deployment pod name: `kubectl get pods -l app=postgresql` +ii. `kubectl exec -it (deployment pod name) -- /bin/bash` +iii. `psql -h postgresql -U myuser -d mydatabase` +iv. Use `mypassword` when prompted for the password \ No newline at end of file diff --git a/code-samples/eventing/bookstore-sample-app/db/deployment.yaml b/code-samples/eventing/bookstore-sample-app/db/deployment.yaml index 6d4384b166..16ab07bf03 100644 --- a/code-samples/eventing/bookstore-sample-app/db/deployment.yaml +++ b/code-samples/eventing/bookstore-sample-app/db/deployment.yaml @@ -14,7 +14,7 @@ spec: spec: containers: - name: postgresql - image: quay.io/enterprisedb/postgresql + image: quay.io/enterprisedb/postgresql env: - name: POSTGRES_DB value: mydatabase @@ -23,4 +23,11 @@ spec: - name: POSTGRES_PASSWORD value: mypassword ports: - - containerPort: 5432 \ No newline at end of file + - containerPort: 5432 + volumeMounts: + - name: postgres-storage + mountPath: /var/lib/postgresql/data + volumes: + - name: postgres-storage + persistentVolumeClaim: + claimName: postgres-pvc \ No newline at end of file