Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample App: Database Migration #5913

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions 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
29 changes: 28 additions & 1 deletion code-samples/eventing/bookstore-sample-app/db/README.md
Expand Up @@ -2,6 +2,7 @@

1. Database Schema
2. Sample Data
3. Deployment on K8s Cluster
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently the README.md is not detailed enough, you can take a look at these 2 PRs, and see how doc is organzied. Please follow the same template, and make sure the steps are clear and well-explained. Also make sure you explain the reason behind each step.

Example 1: Sentiment Analysis Doc
Example 2: Camel-K slack sink Doc


## 1. Database Schema

Expand All @@ -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 |
| 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
33 changes: 33 additions & 0 deletions 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
24 changes: 24 additions & 0 deletions 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As stated in deployment.yaml, we are using the image from quay.io. I would suggest you to use the same to be consistent.

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
4 changes: 2 additions & 2 deletions 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
Expand Down
10 changes: 10 additions & 0 deletions 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