From 5d22785229391d2d294904a9193e9e469a067118 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Thu, 14 Mar 2024 17:01:02 -0400 Subject: [PATCH 01/12] Adding the db service --- .../db-service/create-deployment.yaml | 33 +++++++++++++++++++ .../db-service/create-job.yaml | 23 +++++++++++++ .../db-service/create-volume.yaml | 10 ++++++ .../db-service/expose-service.yaml | 10 ++++++ .../db-service/sample.sql | 14 ++++++++ 5 files changed, 90 insertions(+) create mode 100644 code-samples/eventing/bookstore-sample-app/db-service/create-deployment.yaml create mode 100644 code-samples/eventing/bookstore-sample-app/db-service/create-job.yaml create mode 100644 code-samples/eventing/bookstore-sample-app/db-service/create-volume.yaml create mode 100644 code-samples/eventing/bookstore-sample-app/db-service/expose-service.yaml create mode 100644 code-samples/eventing/bookstore-sample-app/db-service/sample.sql diff --git a/code-samples/eventing/bookstore-sample-app/db-service/create-deployment.yaml b/code-samples/eventing/bookstore-sample-app/db-service/create-deployment.yaml new file mode 100644 index 0000000000..e87c909247 --- /dev/null +++ b/code-samples/eventing/bookstore-sample-app/db-service/create-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: postgresql-storage + mountPath: /var/lib/postgresql/data + volumes: + - name: postgresql-storage + persistentVolumeClaim: + claimName: postgresql-pvc diff --git a/code-samples/eventing/bookstore-sample-app/db-service/create-job.yaml b/code-samples/eventing/bookstore-sample-app/db-service/create-job.yaml new file mode 100644 index 0000000000..91a5a115fe --- /dev/null +++ b/code-samples/eventing/bookstore-sample-app/db-service/create-job.yaml @@ -0,0 +1,23 @@ +apiVersion: batch/v1 +kind: Job +metadata: + name: postgresql-job +spec: + template: + spec: + containers: + - name: postgresql-client + image: quay.io/enterprisedb/postgresql + command: ["psql", "-h", "postgresql", "-U", "myuser", "-d", "mydatabase", "-f", "/sql/sample.sql"] + env: + - name: PGPASSWORD + value: mypassword + volumeMounts: + - name: sql-volume + mountPath: /sql + restartPolicy: Never + volumes: + - name: sql-volume + configMap: + name: sql-configmap + backoffLimit: 4 diff --git a/code-samples/eventing/bookstore-sample-app/db-service/create-volume.yaml b/code-samples/eventing/bookstore-sample-app/db-service/create-volume.yaml new file mode 100644 index 0000000000..3895aad019 --- /dev/null +++ b/code-samples/eventing/bookstore-sample-app/db-service/create-volume.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: postgresql-pvc +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 1Gi diff --git a/code-samples/eventing/bookstore-sample-app/db-service/expose-service.yaml b/code-samples/eventing/bookstore-sample-app/db-service/expose-service.yaml new file mode 100644 index 0000000000..61979523fe --- /dev/null +++ b/code-samples/eventing/bookstore-sample-app/db-service/expose-service.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Service +metadata: + name: postgresql +spec: + ports: + - port: 5432 + selector: + app: postgresql + type: NodePort diff --git a/code-samples/eventing/bookstore-sample-app/db-service/sample.sql b/code-samples/eventing/bookstore-sample-app/db-service/sample.sql new file mode 100644 index 0000000000..b7c50147fb --- /dev/null +++ b/code-samples/eventing/bookstore-sample-app/db-service/sample.sql @@ -0,0 +1,14 @@ +CREATE DATABASE bookstore; +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')) + ); + +INSERT INTO book_reviews (post_time, content, sentiment) VALUES + ('2020-01-01 00:00:00', 'This book is great!', 'positive'), + ('2020-01-02 00:02:00', 'This book is terrible!', 'negative'), + ('2020-01-03 00:01:30', 'This book is okay.', 'neutral'), + ('2020-01-04 00:00:00', 'Meh', 'neutral'); \ No newline at end of file From d614db82cd79a229fceda2927f9db00b6f46098d Mon Sep 17 00:00:00 2001 From: Leo Li Date: Wed, 27 Mar 2024 09:34:48 -0400 Subject: [PATCH 02/12] Add the readme --- .../bookstore-sample-app/db/README.md | 94 +++++++++++++++++-- 1 file changed, 86 insertions(+), 8 deletions(-) diff --git a/code-samples/eventing/bookstore-sample-app/db/README.md b/code-samples/eventing/bookstore-sample-app/db/README.md index 9fb745e878..ce77e668c9 100644 --- a/code-samples/eventing/bookstore-sample-app/db/README.md +++ b/code-samples/eventing/bookstore-sample-app/db/README.md @@ -1,11 +1,18 @@ -# Bookstore Database +# Database Service for Bookstore +In order to run the Bookstore sample application, you need to create a database and populate it with sample data. This document provides the schema and sample data for the database. -1. Database Schema -2. Sample Data +In this tutorial, we will create a PostgreSQL database and populate it with sample data. We will then create **a k8s deployment** that connects to the PostgreSQL database. -## 1. Database Schema +We will be discussing when we should use Knative Service and what benefit it can bring to us. +## What Knative features will we learn about? +- Knative Service -### BookReviews Table +## What does the final deliverable look like? +A k8s deployment file that creates a Knative Service that connects to a PostgreSQL database contains the sample data we specified in the SQL file. + +## Overview + +### The Database Schema The BookReviews table contains all reviews made on the bookstore website. See the columns of the BookReviews table below: @@ -14,13 +21,84 @@ See the columns of the BookReviews table below: * content (text) - The contents of the comment * sentiment (text) - The sentiment results (currently, the values it could take on are 'positive' or 'neutral' or 'negative') -## 2. Sample Data -### BookReviews Table +### The Sample Data The sample rows inserted for the BookReviews table are shown below: | id | post_time | content | sentiment | |----|---------------------|------------------------------|-----------| | 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 | + + + + + +## Implementation +### Step 1: Create a ConfigMap for SQL Configuration + +Use the following command to create a ConfigMap named `sql-configmap` from your `sample.sql` file. This ConfigMap will be used to store your SQL script. + +```bash +kubectl create configmap sql-configmap --from-file=sample.sql +``` + +### Step 2: Set Up Persistent Storage + +Persistent Volume Claims (PVCs) provide a way to request storage for your database that persists beyond the lifecycle of a pod. Apply your PVC configuration to ensure your PostgreSQL database has the necessary storage. + +```bash +kubectl apply -f PVC.yaml +``` + +### Step 3: Deploy the PostgreSQL Server + +Deploy your PostgreSQL server as a pod within your Kubernetes cluster. This deployment will utilize the PVC created in the previous step for storage. + +```bash +kubectl apply -f deployment.yaml +``` + +### Step 4: Expose PostgreSQL Service + +Expose your PostgreSQL server within the Kubernetes cluster to allow connections to the database. + +```bash +kubectl apply -f service.yaml +``` + +### Step 5: Initialize the Database + +Execute the SQL commands from your `sample.sql` file by running a Kubernetes job. This job ensures your database schema and initial data are set up according to your specifications. + +```bash +kubectl apply -f job.yaml +``` + +### Step 6: Interact with Your Database + +After setting up your database, you may want to interact with it to run queries or manage data. + +1. Retrieve the name of your PostgreSQL deployment pod: + +```bash +kubectl get pods -l app=postgresql +``` + +2. Enter the pod’s shell: + +```bash +kubectl exec -it -- /bin/bash +``` + +3. Connect to your PostgreSQL database: + +```bash +psql -h postgresql -U myuser -d mydatabase +``` +Use `mypassword` when prompted for the password. + +## Conclusion + +By following this guide, you have successfully deployed a PostgreSQL server on a Kubernetes cluster, set up persistent storage, and initialized your database using a Kubernetes job. Congratulations! Your bookstore now has the database service. \ No newline at end of file From c24329ebf94909ab5e349c8b016f8906c6f805a6 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Thu, 28 Mar 2024 10:19:25 -0400 Subject: [PATCH 03/12] Add the yaml file to create the config map --- .../db-service/100-create-config-map.yaml | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 code-samples/eventing/bookstore-sample-app/db-service/100-create-config-map.yaml diff --git a/code-samples/eventing/bookstore-sample-app/db-service/100-create-config-map.yaml b/code-samples/eventing/bookstore-sample-app/db-service/100-create-config-map.yaml new file mode 100644 index 0000000000..4031f3eabd --- /dev/null +++ b/code-samples/eventing/bookstore-sample-app/db-service/100-create-config-map.yaml @@ -0,0 +1,20 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: sql-configmap +data: + sample.sql: | + CREATE DATABASE bookstore; + 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')) + ); + + INSERT INTO book_reviews (post_time, content, sentiment) VALUES + ('2020-01-01 00:00:00', 'This book is great!', 'positive'), + ('2020-01-02 00:02:00', 'This book is terrible!', 'negative'), + ('2020-01-03 00:01:30', 'This book is okay.', 'neutral'), + ('2020-01-04 00:00:00', 'Meh', 'neutral'); From 36f88f32396de972703cdeaf2ab9a4ec1db7e615 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Thu, 28 Mar 2024 10:19:36 -0400 Subject: [PATCH 04/12] Change to use the statefulSet --- .../{create-deployment.yaml => 200-create-postgre.yaml} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename code-samples/eventing/bookstore-sample-app/db-service/{create-deployment.yaml => 200-create-postgre.yaml} (97%) diff --git a/code-samples/eventing/bookstore-sample-app/db-service/create-deployment.yaml b/code-samples/eventing/bookstore-sample-app/db-service/200-create-postgre.yaml similarity index 97% rename from code-samples/eventing/bookstore-sample-app/db-service/create-deployment.yaml rename to code-samples/eventing/bookstore-sample-app/db-service/200-create-postgre.yaml index e87c909247..0056950986 100644 --- a/code-samples/eventing/bookstore-sample-app/db-service/create-deployment.yaml +++ b/code-samples/eventing/bookstore-sample-app/db-service/200-create-postgre.yaml @@ -1,5 +1,5 @@ apiVersion: apps/v1 -kind: Deployment +kind: StatefulSet metadata: name: postgresql spec: From 1c659ec67667d53b7f31891fae7f1587804ecd13 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Thu, 28 Mar 2024 10:50:52 -0400 Subject: [PATCH 05/12] Delete the finished job pod after 50 seconds --- .../db-service/100-create-secret.yaml | 9 +++++++++ ...eate-volume.yaml => 100-create-volume.yaml} | 0 .../db-service/200-create-postgre.yaml | 15 ++++++++++++--- ...se-service.yaml => 300-expose-service.yaml} | 0 .../{create-job.yaml => 400-create-job.yaml} | 18 ++++++++++++++++-- 5 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 code-samples/eventing/bookstore-sample-app/db-service/100-create-secret.yaml rename code-samples/eventing/bookstore-sample-app/db-service/{create-volume.yaml => 100-create-volume.yaml} (100%) rename code-samples/eventing/bookstore-sample-app/db-service/{expose-service.yaml => 300-expose-service.yaml} (100%) rename code-samples/eventing/bookstore-sample-app/db-service/{create-job.yaml => 400-create-job.yaml} (52%) diff --git a/code-samples/eventing/bookstore-sample-app/db-service/100-create-secret.yaml b/code-samples/eventing/bookstore-sample-app/db-service/100-create-secret.yaml new file mode 100644 index 0000000000..c05365ffb8 --- /dev/null +++ b/code-samples/eventing/bookstore-sample-app/db-service/100-create-secret.yaml @@ -0,0 +1,9 @@ +apiVersion: v1 +kind: Secret +metadata: + name: postgresql-secret +type: Opaque +data: + POSTGRES_DB: bXlkYXRhYmFzZQ== + POSTGRES_USER: bXl1c2Vy + POSTGRES_PASSWORD: bXlwYXNzd29yZA== diff --git a/code-samples/eventing/bookstore-sample-app/db-service/create-volume.yaml b/code-samples/eventing/bookstore-sample-app/db-service/100-create-volume.yaml similarity index 100% rename from code-samples/eventing/bookstore-sample-app/db-service/create-volume.yaml rename to code-samples/eventing/bookstore-sample-app/db-service/100-create-volume.yaml diff --git a/code-samples/eventing/bookstore-sample-app/db-service/200-create-postgre.yaml b/code-samples/eventing/bookstore-sample-app/db-service/200-create-postgre.yaml index 0056950986..8384d2ed94 100644 --- a/code-samples/eventing/bookstore-sample-app/db-service/200-create-postgre.yaml +++ b/code-samples/eventing/bookstore-sample-app/db-service/200-create-postgre.yaml @@ -17,11 +17,20 @@ spec: image: quay.io/enterprisedb/postgresql env: - name: POSTGRES_DB - value: mydatabase + valueFrom: + secretKeyRef: + name: postgresql-secret + key: POSTGRES_DB - name: POSTGRES_USER - value: myuser + valueFrom: + secretKeyRef: + name: postgresql-secret + key: POSTGRES_USER - name: POSTGRES_PASSWORD - value: mypassword + valueFrom: + secretKeyRef: + name: postgresql-secret + key: POSTGRES_PASSWORD ports: - containerPort: 5432 volumeMounts: diff --git a/code-samples/eventing/bookstore-sample-app/db-service/expose-service.yaml b/code-samples/eventing/bookstore-sample-app/db-service/300-expose-service.yaml similarity index 100% rename from code-samples/eventing/bookstore-sample-app/db-service/expose-service.yaml rename to code-samples/eventing/bookstore-sample-app/db-service/300-expose-service.yaml diff --git a/code-samples/eventing/bookstore-sample-app/db-service/create-job.yaml b/code-samples/eventing/bookstore-sample-app/db-service/400-create-job.yaml similarity index 52% rename from code-samples/eventing/bookstore-sample-app/db-service/create-job.yaml rename to code-samples/eventing/bookstore-sample-app/db-service/400-create-job.yaml index 91a5a115fe..eb8148a0ff 100644 --- a/code-samples/eventing/bookstore-sample-app/db-service/create-job.yaml +++ b/code-samples/eventing/bookstore-sample-app/db-service/400-create-job.yaml @@ -3,6 +3,7 @@ kind: Job metadata: name: postgresql-job spec: + ttlSecondsAfterFinished: 50 template: spec: containers: @@ -11,7 +12,20 @@ spec: command: ["psql", "-h", "postgresql", "-U", "myuser", "-d", "mydatabase", "-f", "/sql/sample.sql"] env: - name: PGPASSWORD - value: mypassword + valueFrom: + secretKeyRef: + name: postgresql-secret + key: POSTGRES_PASSWORD + - name: PGUSER + valueFrom: + secretKeyRef: + name: postgresql-secret + key: POSTGRES_USER + - name: PGDATABASE + valueFrom: + secretKeyRef: + name: postgresql-secret + key: POSTGRES_DB volumeMounts: - name: sql-volume mountPath: /sql @@ -20,4 +34,4 @@ spec: - name: sql-volume configMap: name: sql-configmap - backoffLimit: 4 + backoffLimit: 5 From 255759af3122cfa8a488ccd657ddccaeefe223f0 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Thu, 28 Mar 2024 12:26:56 -0400 Subject: [PATCH 06/12] Simplify the deployment tutorial for the database service --- ...fig-map.yaml => 100-create-configmap.yaml} | 0 .../bookstore-sample-app/db/README.md | 99 +++++++++---------- 2 files changed, 46 insertions(+), 53 deletions(-) rename code-samples/eventing/bookstore-sample-app/db-service/{100-create-config-map.yaml => 100-create-configmap.yaml} (100%) diff --git a/code-samples/eventing/bookstore-sample-app/db-service/100-create-config-map.yaml b/code-samples/eventing/bookstore-sample-app/db-service/100-create-configmap.yaml similarity index 100% rename from code-samples/eventing/bookstore-sample-app/db-service/100-create-config-map.yaml rename to code-samples/eventing/bookstore-sample-app/db-service/100-create-configmap.yaml diff --git a/code-samples/eventing/bookstore-sample-app/db/README.md b/code-samples/eventing/bookstore-sample-app/db/README.md index ce77e668c9..bb0d10f0b5 100644 --- a/code-samples/eventing/bookstore-sample-app/db/README.md +++ b/code-samples/eventing/bookstore-sample-app/db/README.md @@ -1,14 +1,15 @@ # Database Service for Bookstore -In order to run the Bookstore sample application, you need to create a database and populate it with sample data. This document provides the schema and sample data for the database. +To successfully launch the Bookstore sample application, it's essential to set up a dedicated database populated with specific sample data. This guide provides both the schema for the database and the initial data you'll need to get started. -In this tutorial, we will create a PostgreSQL database and populate it with sample data. We will then create **a k8s deployment** that connects to the PostgreSQL database. +In this tutorial, we'll embark on creating a PostgreSQL database using Kubernetes (K8s) StatefulSets and populating it with the sample data provided. + +You might wonder, "Why not leverage Knative Serving to dynamically scale the database service in response to traffic demands?" We'll delve into the optimal scenarios for employing Knative Serving and when it's advantageous for our database service. -We will be discussing when we should use Knative Service and what benefit it can bring to us. ## What Knative features will we learn about? -- Knative Service +- Appropriate Use Cases for Knative Service ## What does the final deliverable look like? -A k8s deployment file that creates a Knative Service that connects to a PostgreSQL database contains the sample data we specified in the SQL file. +Our goal is to deploy a PostgreSQL pod within Kubernetes, loaded with the sample data outlined in the accompanying SQL file. This pod will serve as the foundational database service for our bookstore application. ## Overview @@ -16,10 +17,10 @@ A k8s deployment file that creates a Knative Service that connects to a PostgreS The BookReviews table contains all reviews made on the bookstore website. See the columns of the BookReviews table below: -* ID (serial) - Primary Key -* post_time (datetime) - Posting time of the comment -* content (text) - The contents of the comment -* sentiment (text) - The sentiment results (currently, the values it could take on are 'positive' or 'neutral' or 'negative') +* `ID (serial)` - Primary Key +* `post_time (datetime)` - Posting time of the comment +* `content (text)` - The contents of the comment +* `sentiment (text)` - The sentiment results (currently, the values it could take on are 'positive' or 'neutral' or 'negative') ### The Sample Data @@ -34,70 +35,62 @@ The sample rows inserted for the BookReviews table are shown below: - ## Implementation -### Step 1: Create a ConfigMap for SQL Configuration - -Use the following command to create a ConfigMap named `sql-configmap` from your `sample.sql` file. This ConfigMap will be used to store your SQL script. -```bash -kubectl create configmap sql-configmap --from-file=sample.sql -``` +### Step 1: Acquire Necessary Files from the Repository +The essential files for setting up your database are located within the `db` directory of our repository. Please download these files to proceed. -### Step 2: Set Up Persistent Storage +### Step 2: Deploying the PostgreSQL Database +To deploy the PostgreSQL database and populate it with the provided sample data, you'll apply a series of Kubernetes deployment files. Ensure you're positioned in the `code-sample` directory and not within the `db` subdirectory for this operation. -Persistent Volume Claims (PVCs) provide a way to request storage for your database that persists beyond the lifecycle of a pod. Apply your PVC configuration to ensure your PostgreSQL database has the necessary storage. +Within this directory, you will find 6 YAML files, each serving a distinct purpose in the setup process: +- `100-create-configmap.yaml`: Generates a ConfigMap including the SQL file for database initialization. +- `100-create-secret.yaml`: Produces a Secret holding the PostgreSQL database password. +- `100-create-volume.yaml`: Creates both a PersistentVolume and a PersistentVolumeClaim for database storage. +- `200-create-postgre.yaml`: Establishes the StatefulSet for the PostgreSQL database. +- `300-expose-service.yaml`: Launches a Service to expose the PostgreSQL database externally. +- `400-create-job.yaml`: Executes a Job that populates the database with the sample data. +Execute the command below to apply all configuration files located in the `db` directory: ```bash -kubectl apply -f PVC.yaml +kubectl apply -f db ``` +The filenames prefixed with numbers dictate the application order, ensuring Kubernetes orchestrates the resource setup accordingly. -### Step 3: Deploy the PostgreSQL Server - -Deploy your PostgreSQL server as a pod within your Kubernetes cluster. This deployment will utilize the PVC created in the previous step for storage. - +### Step 3: Confirming the Deployment +Following the application of the deployment files, initialization of the database may require some time. Monitor the deployment's progress by executing: ```bash -kubectl apply -f deployment.yaml +kubectl get pods -n=default ``` - -### Step 4: Expose PostgreSQL Service - -Expose your PostgreSQL server within the Kubernetes cluster to allow connections to the database. - +A successful deployment is indicated by the `Running` state of the `postgresql-0` pod, as shown below: ```bash -kubectl apply -f service.yaml +NAMESPACE NAME READY STATUS RESTARTS AGE +default postgresql-0 1/1 Running 0 1m ``` - -### Step 5: Initialize the Database - -Execute the SQL commands from your `sample.sql` file by running a Kubernetes job. This job ensures your database schema and initial data are set up according to your specifications. - +Upon observing the pod in a `Running` state, access the pod using the command: ```bash -kubectl apply -f job.yaml +kubectl exec -it postgresql-0 -n=default -- /bin/bash ``` - -### Step 6: Interact with Your Database - -After setting up your database, you may want to interact with it to run queries or manage data. - -1. Retrieve the name of your PostgreSQL deployment pod: - +Inside the pod, connect to the database with: ```bash -kubectl get pods -l app=postgresql +psql -U myuser -d mydatabase ``` - -2. Enter the pod’s shell: - +A successful connection will present you with: ```bash -kubectl exec -it -- /bin/bash +mydatabase=# ``` - -3. Connect to your PostgreSQL database: - +To verify the initialization of the `BookReviews` table, execute: +``` +mydatabase=# \dt +``` +If the output lists the `BookReviews` table as follows, your database has been correctly initialized: ```bash -psql -h postgresql -U myuser -d mydatabase + List of relations + Schema | Name | Type | Owner +--------+--------------+-------+-------- + public | book_reviews | table | myuser +(1 row) ``` -Use `mypassword` when prompted for the password. ## Conclusion From 4ae59cbff32d46f8ddec4869ab00fec268de8d08 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Thu, 28 Mar 2024 12:36:22 -0400 Subject: [PATCH 07/12] Explain why we don't use Knative Service --- .../bookstore-sample-app/db/README.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/code-samples/eventing/bookstore-sample-app/db/README.md b/code-samples/eventing/bookstore-sample-app/db/README.md index bb0d10f0b5..9f563f9ac9 100644 --- a/code-samples/eventing/bookstore-sample-app/db/README.md +++ b/code-samples/eventing/bookstore-sample-app/db/README.md @@ -92,6 +92,26 @@ If the output lists the `BookReviews` table as follows, your database has been c (1 row) ``` +## Question & Discussion +1. Why did we choose to deploy our PostgreSQL database using a StatefulSet instead of a Knative Service? + + +We use [StatefulSet](https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/) for databases instead of Knative Service mainly because databases need to remember data (like a notebook that keeps your notes). StatefulSets are good at remembering things because they can save data and have a special name and place where they live. This is very important for databases. + +Knative Services are more like notebooks that you use and then throw away when you're done. They're great for tasks that don't need to keep data for a long time. You can make them go away when you don't need them and come back when you do. But databases need to always remember information, so they can't just disappear and come back. + +Also, databases often talk in their own special language, not the usual web language (HTTP) that Knative Services are really good at understanding. Because of this, Knative Services aren't the best choice for databases. That's why we choose StatefulSet for databases in Kubernetes. + +--- +Note box: However, Knative Service supports Volumes and Persistent Volumes, which can be used to store data. You can read more [here](https://knative.dev/docs/serving/services/storage/) about how to use Volumes and Persistent Volumes with Knative Services specially for your use case. + +--- + +2. When should I use Knative Service, and what would be the best use case for it? + +You can read more about the best use cases for Knative Service [here](https://knative.dev/docs/serving/samples/)! + + ## Conclusion By following this guide, you have successfully deployed a PostgreSQL server on a Kubernetes cluster, set up persistent storage, and initialized your database using a Kubernetes job. Congratulations! Your bookstore now has the database service. \ No newline at end of file From 2e4a32a136d6346947f74e2efff4c041d9932f04 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Thu, 28 Mar 2024 13:09:49 -0400 Subject: [PATCH 08/12] Remove the unnessary empty lines in the file --- code-samples/eventing/bookstore-sample-app/db/README.md | 8 -------- 1 file changed, 8 deletions(-) diff --git a/code-samples/eventing/bookstore-sample-app/db/README.md b/code-samples/eventing/bookstore-sample-app/db/README.md index 9f563f9ac9..a656db8f38 100644 --- a/code-samples/eventing/bookstore-sample-app/db/README.md +++ b/code-samples/eventing/bookstore-sample-app/db/README.md @@ -12,7 +12,6 @@ You might wonder, "Why not leverage Knative Serving to dynamically scale the dat Our goal is to deploy a PostgreSQL pod within Kubernetes, loaded with the sample data outlined in the accompanying SQL file. This pod will serve as the foundational database service for our bookstore application. ## Overview - ### The Database Schema The BookReviews table contains all reviews made on the bookstore website. @@ -22,7 +21,6 @@ See the columns of the BookReviews table below: * `content (text)` - The contents of the comment * `sentiment (text)` - The sentiment results (currently, the values it could take on are 'positive' or 'neutral' or 'negative') - ### The Sample Data The sample rows inserted for the BookReviews table are shown below: | id | post_time | content | sentiment | @@ -32,9 +30,6 @@ The sample rows inserted for the BookReviews table are shown below: | 3 | 2020-01-03 00:01:30 | This book is okay. | neutral | | 4 | 2020-01-04 00:00:00 | Meh | neutral | - - - ## Implementation ### Step 1: Acquire Necessary Files from the Repository @@ -95,7 +90,6 @@ If the output lists the `BookReviews` table as follows, your database has been c ## Question & Discussion 1. Why did we choose to deploy our PostgreSQL database using a StatefulSet instead of a Knative Service? - We use [StatefulSet](https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/) for databases instead of Knative Service mainly because databases need to remember data (like a notebook that keeps your notes). StatefulSets are good at remembering things because they can save data and have a special name and place where they live. This is very important for databases. Knative Services are more like notebooks that you use and then throw away when you're done. They're great for tasks that don't need to keep data for a long time. You can make them go away when you don't need them and come back when you do. But databases need to always remember information, so they can't just disappear and come back. @@ -111,7 +105,5 @@ Note box: However, Knative Service supports Volumes and Persistent Volumes, whic You can read more about the best use cases for Knative Service [here](https://knative.dev/docs/serving/samples/)! - ## Conclusion - By following this guide, you have successfully deployed a PostgreSQL server on a Kubernetes cluster, set up persistent storage, and initialized your database using a Kubernetes job. Congratulations! Your bookstore now has the database service. \ No newline at end of file From 22e48c6e4bdd3596d2719092897a1e37104a7c17 Mon Sep 17 00:00:00 2001 From: Leo Li Date: Thu, 4 Apr 2024 17:01:15 -0400 Subject: [PATCH 09/12] Update code-samples/eventing/bookstore-sample-app/db/README.md Co-authored-by: Pierangelo Di Pilato --- code-samples/eventing/bookstore-sample-app/db/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/code-samples/eventing/bookstore-sample-app/db/README.md b/code-samples/eventing/bookstore-sample-app/db/README.md index a656db8f38..d09ae179dd 100644 --- a/code-samples/eventing/bookstore-sample-app/db/README.md +++ b/code-samples/eventing/bookstore-sample-app/db/README.md @@ -64,7 +64,8 @@ default postgresql-0 1/1 Running 0 1m ``` Upon observing the pod in a `Running` state, access the pod using the command: ```bash -kubectl exec -it postgresql-0 -n=default -- /bin/bash +kubectl exec -it postgresql-0 -- /bin/bash + ``` Inside the pod, connect to the database with: ```bash From 99aa14ca878de59934529b02b3e24732f7d81efb Mon Sep 17 00:00:00 2001 From: Leo Li Date: Thu, 4 Apr 2024 17:02:57 -0400 Subject: [PATCH 10/12] Update code-samples/eventing/bookstore-sample-app/db-service/sample.sql Co-authored-by: Pierangelo Di Pilato --- .../eventing/bookstore-sample-app/db-service/sample.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code-samples/eventing/bookstore-sample-app/db-service/sample.sql b/code-samples/eventing/bookstore-sample-app/db-service/sample.sql index b7c50147fb..d7960ccc49 100644 --- a/code-samples/eventing/bookstore-sample-app/db-service/sample.sql +++ b/code-samples/eventing/bookstore-sample-app/db-service/sample.sql @@ -11,4 +11,4 @@ INSERT INTO book_reviews (post_time, content, sentiment) VALUES ('2020-01-01 00:00:00', 'This book is great!', 'positive'), ('2020-01-02 00:02:00', 'This book is terrible!', 'negative'), ('2020-01-03 00:01:30', 'This book is okay.', 'neutral'), - ('2020-01-04 00:00:00', 'Meh', 'neutral'); \ No newline at end of file + ('2020-01-04 00:00:00', 'Meh', 'neutral'); From 24fde1f1824e0539269776ffc83059bae2ed456d Mon Sep 17 00:00:00 2001 From: Leo Li Date: Thu, 4 Apr 2024 17:04:43 -0400 Subject: [PATCH 11/12] Update code-samples/eventing/bookstore-sample-app/db/README.md Co-authored-by: Pierangelo Di Pilato --- code-samples/eventing/bookstore-sample-app/db/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/code-samples/eventing/bookstore-sample-app/db/README.md b/code-samples/eventing/bookstore-sample-app/db/README.md index d09ae179dd..be1d4c774e 100644 --- a/code-samples/eventing/bookstore-sample-app/db/README.md +++ b/code-samples/eventing/bookstore-sample-app/db/README.md @@ -55,7 +55,8 @@ The filenames prefixed with numbers dictate the application order, ensuring Kube ### Step 3: Confirming the Deployment Following the application of the deployment files, initialization of the database may require some time. Monitor the deployment's progress by executing: ```bash -kubectl get pods -n=default +kubectl get pods + ``` A successful deployment is indicated by the `Running` state of the `postgresql-0` pod, as shown below: ```bash From cc0b169931c0402282904cd662db4b29ce987f4e Mon Sep 17 00:00:00 2001 From: Leo HC Li <36619969+Leo6Leo@users.noreply.github.com> Date: Sun, 7 Apr 2024 22:52:40 -0400 Subject: [PATCH 12/12] Fix the review comment --- code-samples/eventing/bookstore-sample-app/db/README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/code-samples/eventing/bookstore-sample-app/db/README.md b/code-samples/eventing/bookstore-sample-app/db/README.md index be1d4c774e..26a8de577d 100644 --- a/code-samples/eventing/bookstore-sample-app/db/README.md +++ b/code-samples/eventing/bookstore-sample-app/db/README.md @@ -5,9 +5,6 @@ In this tutorial, we'll embark on creating a PostgreSQL database using Kubernete You might wonder, "Why not leverage Knative Serving to dynamically scale the database service in response to traffic demands?" We'll delve into the optimal scenarios for employing Knative Serving and when it's advantageous for our database service. -## What Knative features will we learn about? -- Appropriate Use Cases for Knative Service - ## What does the final deliverable look like? Our goal is to deploy a PostgreSQL pod within Kubernetes, loaded with the sample data outlined in the accompanying SQL file. This pod will serve as the foundational database service for our bookstore application.