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 9fb745e878..1fc6ff9330 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,30 @@ 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. 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` + +5. Expose the port for the Postgres server +`kubectl apply -f service.yaml` + +6. Run the job for executing the queries from sample.sql +`kubectl apply -f job.yaml` + +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 new file mode 100644 index 0000000000..16ab07bf03 --- /dev/null +++ b/code-samples/eventing/bookstore-sample-app/db/deployment.yaml @@ -0,0 +1,33 @@ +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 + volumeMounts: + - name: postgres-storage + mountPath: /var/lib/postgresql/data + volumes: + - name: postgres-storage + persistentVolumeClaim: + claimName: postgres-pvc \ 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