Skip to content

Commit ab91cb8

Browse files
authored
Merge pull request #204 from vdice/docs/sqlite-provider
docs(topics): add sqlite provider tutorial
2 parents 0757ec0 + 0ea090d commit ab91cb8

File tree

1 file changed

+177
-0
lines changed

1 file changed

+177
-0
lines changed
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
---
2+
title: Connecting to a SQLite database
3+
description: Connect your Spin App to an external SQLite database
4+
date: 2024-07-17
5+
categories: [Spin Operator]
6+
tags: [Tutorials]
7+
weight: 13
8+
---
9+
10+
Spin applications can utilize a [standardized API for persisting data in a SQLite database](https://developer.fermyon.com/spin/v2/sqlite-api-guide). A default database is created by the Spin runtime on the local filesystem, which is great for getting an application up and running. However, this on-disk solution may not be preferable for an app running in the context of SpinKube, where apps are often scaled beyond just one replica.
11+
12+
Thankfully, Spin supports configuring an application with an [external SQLite database provider via runtime configuration](https://developer.fermyon.com/spin/v2/dynamic-configuration#libsql-storage-provider). External providers include any [libSQL](https://libsql.org/) databases that can be accessed over HTTPS.
13+
14+
## Prerequisites
15+
16+
To follow along with this tutorial, you'll need:
17+
18+
- A Kubernetes cluster running SpinKube. See the [Installation]({{< relref "install" >}}) guides for more information.
19+
- The [kubectl CLI](https://kubernetes.io/docs/tasks/tools/#kubectl)
20+
- The [spin CLI](https://developer.fermyon.com/spin/v2/install )
21+
22+
## Build and publish the Spin application
23+
24+
For this tutorial, we'll use the [HTTP CRUD Go SQLite](https://github.com/fermyon/enterprise-architectures-and-patterns/tree/main/http-crud-go-sqlite) sample application. It is a Go-based app implementing CRUD (Create, Read, Update, Delete) operations via the SQLite API.
25+
26+
First, clone the repository locally and navigate to the `http-crud-go-sqlite` directory:
27+
28+
```bash
29+
git clone [email protected]:fermyon/enterprise-architectures-and-patterns.git
30+
cd enterprise-architectures-and-patterns/http-crud-go-sqlite
31+
```
32+
33+
Now, build and push the application to a registry you have access to. Here we'll use [ttl.sh](https://ttl.sh):
34+
35+
```bash
36+
export IMAGE_NAME=ttl.sh/$(uuidgen):1h
37+
spin build
38+
spin registry push ${IMAGE_NAME}
39+
```
40+
41+
## Create a LibSQL database
42+
43+
If you don't already have a LibSQL database that can be used over HTTPS, you can follow along as we set one up via [Turso](https://turso.tech/).
44+
45+
Before proceeding, install the [turso CLI](https://docs.turso.tech/quickstart) and sign up for an account, if you haven't done so already.
46+
47+
Create a new database and save its HTTP URL:
48+
49+
```bash
50+
turso db create spinkube
51+
export DB_URL=$(turso db show spinkube --http-url)
52+
```
53+
54+
Next, create an auth token for this database:
55+
56+
```bash
57+
export DB_TOKEN=$(turso db tokens create spinkube)
58+
```
59+
60+
## Create a Kubernetes Secret for the database token
61+
62+
The database token is a sensitive value and thus should be created as a Secret resource in Kubernetes:
63+
64+
```bash
65+
kubectl create secret generic turso-auth --from-literal=db-token="${DB_TOKEN}"
66+
```
67+
68+
## Prepare the SpinApp manifest
69+
70+
You're now ready to assemble the SpinApp custom resource manifest.
71+
72+
- Note the `image` value uses the reference you published above.
73+
- All of the SQLite database config is set under `spec.runtimeConfig.sqliteDatabases`. See the [sqliteDatabases reference guide]({{< ref "docs/reference/spin-app#spinappspecruntimeconfigsqlitedatabasesindex" >}}) for more details.
74+
- Here we configure the `default` database to use the `libsql` provider type and under `options` supply the database URL and auth token (via its Kubernetes secret)
75+
76+
Plug the `$IMAGE_NAME` and `$DB_URL` values into the manifest below and save as `spinapp.yaml`:
77+
78+
```yaml
79+
apiVersion: core.spinoperator.dev/v1alpha1
80+
kind: SpinApp
81+
metadata:
82+
name: http-crud-go-sqlite
83+
spec:
84+
image: "$IMAGE_NAME"
85+
replicas: 1
86+
executor: containerd-shim-spin
87+
runtimeConfig:
88+
sqliteDatabases:
89+
- name: "default"
90+
type: "libsql"
91+
options:
92+
- name: "url"
93+
value: "$DB_URL"
94+
- name: "token"
95+
valueFrom:
96+
secretKeyRef:
97+
name: "turso-auth"
98+
key: "db-token"
99+
```
100+
101+
## Create the SpinApp
102+
103+
Apply the resource manifest to your Kubernetes cluster:
104+
105+
```bash
106+
kubectl apply -f spinapp.yaml
107+
```
108+
109+
The Spin Operator will handle the creation of the underlying Kubernetes resources on your behalf.
110+
111+
## Test the application
112+
113+
Now you are ready to test the application and verify connectivity and data storage to the configured SQLite database.
114+
115+
Configure port forwarding from your local machine to the corresponding Kubernetes `Service`:
116+
117+
```bash
118+
kubectl port-forward services/http-crud-go-sqlite 8080:80
119+
120+
Forwarding from 127.0.0.1:8080 -> 80
121+
Forwarding from [::1]:8080 -> 80
122+
```
123+
124+
When port forwarding is established, you can send HTTP requests to the http-crud-go-sqlite app from within an additional terminal session. Here are a few examples to get you started.
125+
126+
Get current items:
127+
128+
```bash
129+
$ curl -X GET http://localhost:8080/items
130+
[
131+
{
132+
"id": "8b933c84-ee60-45a1-848d-428ad3259e2b",
133+
"name": "Full Self Driving (FSD)",
134+
"active": true
135+
},
136+
{
137+
"id": "d660b9b2-0406-46d6-9efe-b40b4cca59fc",
138+
"name": "Sentry Mode",
139+
"active": true
140+
}
141+
]
142+
```
143+
144+
Create a new item:
145+
146+
```bash
147+
$ curl -X POST -d '{"name":"Engage Thrusters","active":true}' localhost:8080/items
148+
{
149+
"id": "a5efaa73-a4ac-4ffc-9c5c-61c5740e2d9f",
150+
"name": "Engage Thrusters",
151+
"active": true
152+
}
153+
```
154+
155+
Get items and see the newly added item:
156+
157+
```bash
158+
$ curl -X GET http://localhost:8080/items
159+
[
160+
{
161+
"id": "8b933c84-ee60-45a1-848d-428ad3259e2b",
162+
"name": "Full Self Driving (FSD)",
163+
"active": true
164+
},
165+
{
166+
"id": "d660b9b2-0406-46d6-9efe-b40b4cca59fc",
167+
"name": "Sentry Mode",
168+
"active": true
169+
},
170+
{
171+
"id": "a5efaa73-a4ac-4ffc-9c5c-61c5740e2d9f",
172+
"name": "Engage Thrusters",
173+
"active": true
174+
}
175+
]
176+
```
177+

0 commit comments

Comments
 (0)